subscribe()
Subscribes to a store's DATA emissions with previous-value tracking. Returns a Subscription with unsubscribe() and signal() for upstream lifecycle control.
Signature
ts
function subscribe<T>(
store: Store<T>,
cb: (value: T, prev: T | undefined) => void,
opts?: { onEnd?: (error?: unknown) => void },
): SubscriptionParameters
| Parameter | Type | Description |
|---|---|---|
store | Store<T> | The Store<T> to listen to. |
cb | `(value: T, prev: T | undefined) => void` |
opts | { onEnd?: (error?: unknown) => void } | Optional onEnd when the stream completes or errors. |
Returns
Subscription — unsubscribe() to disconnect, signal(s) to send lifecycle signals upstream.
Basic Usage
ts
import { state, subscribe, RESET } from 'callbag-recharge';
const n = state(0);
const sub = subscribe(n, (v, prev) => console.log(v));
n.set(1);
sub.signal(RESET); // send RESET upstream
sub.unsubscribe(); // disconnect