Reactivity
Listening for changes is the core purpose of observables, so Legend-State provides many options. You can listen to changes at any level in an object’s hierarchy and it will be notified by changes in any children.
Observing contexts
The core power of Legend-State is the “observing contexts”. Calling get()
within an observing context will track changes in that node, and re-run itself whenever it changes.
Most functions in Legend-State take what we call a “Selector”, which is either a single observable or a function that calls get()
on some observables and returns a value.
Most functions in Legend-State are observing contexts, including computed observables, observe
, when
, linked/synced get
functions, as well as observer
and Reactive components in React. When you call get()
on an observable inside an observing context it will track it for changes and re-run whenever it changes.
What tracks
get()
is the primary way to access observables and track for changes, but there are actually a few ways:
- Call
get()
on an observable:settings.get()
- Array looping functions (shallow listener):
arr.map(settings.accounts, () => ...)
- Accessing array length (shallow listener):
if (arr.length > 0) ...
- Object.keys (shallow listener):
Object.keys(settings)
- Object.values (shallow listener):
Object.values(settings)
These operation do not track:
- Accessing through an observable:
state$.settings
- Call
peek()
on an observable:settings.peek()
Observing examples
The automatic behavior can be modified with two observable functions:
Function | Tracked |
---|---|
get() | yes |
peek() | no |
get(true) | shallow |
arr$.map(...) | shallow |
arr$.length | shallow |
Object.keys(state$) | shallow |
Object.values(state$) | shallow |
get()
get
returns the raw data of an observable and tracks it, so you can work with it without doing any further tracking. You may want to use get()
to:
- Get the value of an observable wrapper of a primitive
- Track this object and not its individual fields. Minimizing the number of listeners is better for performance.
Shallow tracking
get()
observes recursively by default, so any child changing will cause an update. You can modify it to be a shallow listener by just adding a true
parameter. This can be useful when a component only needs to re-render if an object’s keys or an array’s items change. Array and Object functions also track shallowly - see What tracks above.
Selectors
Many of the functions in Legend-State take a Selector, which can be either an observable or a function that returns a value based on observables. The selector is run in an observing context so that get()
tracks an observable for changes. Whenever an observable changes, it re-runs the function.
Using when
as an example of using Selectors:
observe
observe
can run arbitrary code when observables change, and automatically tracks the observables accessed while running, so it will update whenever any accessed observable changes.
This can be useful to use multiple observables at once, for the benefit of cleanup effects, or if you just like it more than onChange.
The callback parameter has some useful properties:
num
: How many times it’s run. Use this to do something only the first time or not the first time.previous
: The previous value, which will be undefined on the first run and set to the return valuecancel
: Set totrue
to stop tracking the observables when you are done observingonCleanup
: A function to call before running the selector again
observe
has an optional second reaction
parameter which will run after the selector, and does not track changes. This can be useful for observing an event
or a single observable
.
Or use the second parameter to run a reaction when a selector changes. It has an additional value
parameter, which contains the value of the selector.
when
when
runs the given callback only once when the Selector returns a truthy value, and automatically tracks the observables accessed while running the Selector so it will update whenever one of them changes. When the value becomes truthy it will call the callback function and dispose the listeners.
It also returns a Promise that resolves when the Selector returns a truthy value that can be used instead of the callback function.
whenReady
whenReady
is the same as when
except it waits for objects and arrays to not be empty.
onChange
onChange
listens to an observable for any changes anywhere within it. Use this as specifically as possible because it will fire notifications for every change recursively up the tree.
onChange
has some extra options for more advanced use:
getPrevious
: Function to compare with the previous value. It is a function to let you opt into getting the previous value if needed, because it has some performance cost in cloning the object to compute the previous value.changes
: Array of all of the changes to this observable in the latest batch. This is intended mainly for internal usage by the persistence plugins to know what to sync/update and the history plugin to track all changes, but it may be good for other uses too.trackingType
: Whether to track only shallow changesinitial
: Whether to run the callback immediately with the current valueimmediate
: Whether to run the callback immediately instead of within a batch. This is used internally bycomputed
to make sure its value is always correct, but it may be useful for other specific uses.
Dispose of listeners
Listening to an observable returns a dispose function to stop listening. Just call it when you want to stop listening.
Batching
You may want to modify multiple observables at once without triggering callbacks for each change. Batching postpones renders and listeners until the end of the batch.
Batching can be done in two ways, wrapping between beginBatch()
and endBatch()
or in a callback with batch(callback)
.
As we all know, you generally shouldn’t optimize pre-emptively. observable
functions like assign
already batch changes under the hood, so listeners don’t get called until the full change is complete. In many cases like setting unrelated observables you don’t need to worry about it.
Batching is important in a few key situations:
When observables depend on each other
Use batch
to delay computations/renders until all dependent changes are complete or you might get weird intermediary states.
To prevent excessive renders
Making multiple changes in a row can cause React components and observers to re-run multiple times when they should wait until changes are complete.
When persisting
If you are using synced
or syncObservable
to automatically persist your changes, you can prevent excessive writes by delaying persistence until changes are complete. Pushing to an array 1000 times could save to storage 1000 times, which could be very slow!