The type of the projector's value.
Construct and set up a new Projector. This first calls the BaseAtom constructor with the initial projected value by grabbing the current value of all atoms passed in and the calling the projection function. This ensures the Projector always has a value. Then it initializes all the instance variables and splits all the atoms passed in into regular and readonly atoms before finally subscribing to all the atoms (excluding readonly atoms).
The list of input atoms that the projector will subscribe to and whose values will be projected with the help fo the projection function.
The projection function receives, as arguments, the values of the set of atoms passed in and returns a new value for the projector based on the atom's input values. These should be treated as readonly as the projection function should not modify the input values at all.
This maintains the list of all atoms that the projector projects. These will be iterated and mapped to their values before being passed to the projection function.
The projection function is a function that will be executed with the values of the provided atoms and is expected to return the new value of the projector.
This is the list of readonly atoms that will only be subscribed to when the projector has subscribers.
This list contains all non-readonly atoms that will be subscribed to at all times.
This is a flag that denotes whether or not the projector is currently subscribed to readonly atoms. This is used to prevents multiple subscriptions to readonly atoms which would cause the readonly atoms to continue running as if they has a subscriber when in reality it would be a duplicate and no a valid subscriber.
The list of active subscribers listening or value changes on this atom. Subscribers are called anytime the value is updated.
The value of the atom, BaseAtom does not provide any means for updating this after the object is created.
This readonly property will return the current active value of the atom.
The current value of the atom.
An internal test that determines if any subscribers are currently subscribed to this atom's value changes.
A boolean representing if there are any subscribers currently subscribed to this atom.
An internal helper that will fire all the subscribers passing this
to
them, this is typically only triggered in response to value changes.
An internal helper that will update the value of the atom. Note that it's protected, there is no public way to modify a BaseAtom's value this is for subclasses to used.
The new value for the atom's value field.
This just triggers a subscription to readonly atoms (if it's the first subscriber) when a subscriber subscribes otherwise this simply calls the BaseAtom subscribe function.
The subscriber function wishing to be called when the projectors value changes.
This will subscribe to all regular atoms, and if there is at least one subscriber then all readonly atoms as well.
Subscribe to readonly atoms if there are subscribers, and there are readonly atoms and the projector is not already subscribed to readonly atoms.
This just triggers unsubscription (if it's the last subscriber) when a subscriber chooses to unsubscribe. Otherwise this simple calls BaseAtoms unsubscribe function.
The subscriber that no longer wishes to be called when the projectors value changes.
This will unsubscribe from every atom passed in, including readonly atoms. It is intended to be used internally and/or before a projector whose input atoms outlive it fall out of scope to prevent memory leaks.
Unsubscribe from readonly atoms only if we're subscribed to readonly atoms.
This function is passed, as subscriber, to all atoms passed to the projector and simply maps the current atom values and passes them to the projection function to generate a new value. This new value is the set as the projectors new value and then all of the projectors subscribers are notified.
Generated using TypeDoc
The Projector is @nuka/state's equivalent to "computed" values. It takes one or more input atom-like objects and a projection function and it sets it's value based on the return of the projection function and all input atoms. It will also subscribe to all the input atom-like objects so that if any of their values are updated the projection function will run again.
Projectors act like atoms, so they can be consumed by other projectors or other atom-consuming API functions.
There is a slight optimization that Projector will do, it will try and sniff if any input atom-like objects are a ReadonlyAtom, and if so it will only subscribe to them if there are subscribers to the Projector so that the readonly atom isn't running when nothings is subscribed.
NOTE
Due to the fact that projectors subscribe to the atoms they are created with, if you wish to stop using a projector (have it fall out of scope) you must manually call
unsubscribeFromAtoms
or else the projector will not be garbage collected as it's subscription function to all atoms will keep it in memory. If the atoms that thep projector was created for/with will also fall out of scope this is not required.Examples
Probably the simpleist example of a projector is summing up multiple count atoms.
Projectors don't always have to be used with multiple atoms, somtimes you just want to be able to have and update data from a single atom in a different way. Like showing the full name for a user.
A simple and clean way to control how data is rendered, just modify
fullName
's projection function to, say, change the computed value tolast, first
instead.DO NOT USE Prefer using the projector factory function to manually creating instances of this class.