Subscription APIs
The Subscription APIs provide powerful tools for reacting to state changes. The primary function is derive, which allows you to subscribe to updates, log changes, and synchronize states.
Core Subscription Functions
These functions form the core of Anchor's derivation system, allowing you to create subscriptions and react to state changes.
subscribe()
The subscribe() function is the main way to subscribe to changes in an anchored state. It takes a state and a handler function that will be executed whenever the state is modified.
type derive = <T>(state: T, handler: StateSubscriber<T>, recursive?: boolean) => StateUnsubscribe;state: The anchored state to observe. See State.handler: A callback function that receives the new state snapshot, the change event details, and an optional emitter ID. See StateSubscriber.snapshot: The latest value of the state.event: An object detailing the change (type,keys, etc.). See StateChange.
recursive(optional): Iftrue, the handler will also be triggered for changes in nested child states. Defaults tofalse.- Returns: An StateUnsubscribe function to stop listening for changes.
derive() DEPRECATED
Deprecated Symbol
The derive() function and derive.* functions are deprecated and will be removed in the next stable release. Use subscribe() instead.
subscribe.log()
A convenience method for debugging. It subscribes to a state and logs any changes to the console.
type log = <T extends Linkable>(state: State<T>) => StateUnsubscribe;state: The anchored state to log. See State and Linkable.- Returns: An StateUnsubscribe function to stop logging.
State Synchronization Functions
These functions allow you to synchronize states with each other, with optional transformations.
subscribe.pipe()
Synchronizes changes from a source state to a target state, with an optional transformation step.
type pipe = <Source extends State, Target extends Linkable>(
source: Source,
target: Target,
transform?: PipeTransformer<Source, Target>
) => StateUnsubscribe;source: The state to listen for changes on. See State.target: The state to apply changes to. See Linkable.transform(optional): A function that receives the source state's value and can return a modified value to be applied to the target. See PipeTransformer.- Returns: An StateUnsubscribe function to stop the synchronization.
subscribe.bind()
Creates a two-way binding between two states. Changes in one state are reflected in the other, and vice-versa.
type bind = <Left extends State, Right extends State>(
left: Left,
right: Right,
transformLeft?: PipeTransformer<Left, Right>,
transformRight?: PipeTransformer<Right, Left>
) => StateUnsubscribe;left: The first state in the binding. See State.right: The second state in the binding. See State.transformLeft(optional): A function to transform the value from theleftstate before applying it to therightstate. See PipeTransformer.transformRight(optional): A function to transform the value from therightstate before applying it to theleftstate. See PipeTransformer.- Returns: An StateUnsubscribe function to remove the binding.
Utility Functions
These are utility functions for advanced use cases and debugging.
subscribe.resolve()
Retrieves the internal StateController for a given state. This is an advanced feature for direct interaction with the state's metadata and lifecycle.
type resolve = <T extends Linkable>(state: State<T>) => StateController<T> | undefined;state: The anchored state. See State and Linkable.- Returns: The StateController instance for the state, or
undefinedif not found.