Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fhirpath.js compatible with reactivity libraries (Jotai, Angular Signals, ...) #161

Open
axelv opened this issue Jan 1, 2025 · 2 comments

Comments

@axelv
Copy link

axelv commented Jan 1, 2025

TLDR: I'm trying to make fhirpath.js compatible with jotai, a React state manager, to implement calculatedExpressions from the FHIR Structured Data Capturing IG. But some adaptations to fhirpath.js are needed.

Context

I'm trying to implement calculated expressions to enable real-time calculations in my form library. The challenges to implement such reactive calculations are:

  1. keeping track of the dependencies (the environment variables that are being used) in a specific FHIRPath expression
  2. automatic re-evaluation of the FHIRPath expressions when one of the dependency variables has changed.

Luckily, libraries like Jotai, Angular Signals, or Solid.js have already solved the reactivity part. The remaining challenge is integrating this with fhirpath.js.

Desired API

A common pattern in reactivity libraries is that you need a getter function to resolve the current value of a variable. So, to make fhirpath.js compatible, we would need to inject that getter function somehow.

Below is an example of how the API could look like using Jotai:
[update] In the end, no API changes were necessary

a = atom(0) // variables defined as Jotai Atoms to make them reactive.
b = atom(0)
c = atom((get)=> // a 'derived' atom consuming the variables
        evaluate(undefined,  // Resource is not necessary 
                 "%a + %b", // Expression
                 {a, b}, // Environment variables
                 model, 
                 { variableGetter: get} // getter to resolve the variables 
       ))

I have a prototype of this library with a modified version of fhirpath.js here: https://github.com/Tiro-health/fhirpath-jotai. The current implementation uses a Proxy to sneak in the getter function.

@axelv
Copy link
Author

axelv commented Jan 2, 2025

After some experimentation, I found a solution that doesn't need any changes to the signature of evaluate() #162

The modifications in the PR introduce one new assumption for the context object. Variables are only read when they are consumed. This allows for lazy evaluation of the variables.

@yuriy-sedinkin
Copy link
Collaborator

Thank you! I like your latest changes. I think we will consider using them in the next patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants