Skip to content

gate()

Human-in-the-loop: pauses stream, inspects pending values, approve/reject/modify before forwarding (Tier 2).

Signature

ts
function gate<A>(opts?: GateOptions): (input: Store<A>) => GatedStore<A>

Parameters

ParameterTypeDescription
optsGateOptionsOptional configuration.

Returns

A function that takes Store&lt;A&gt; and returns GatedStore&lt;A&gt;.

MethodSignatureDescription
get()() =&gt; A \undefined
pendingStore\&lt;A[]\&gt;Reactive queue of values awaiting approval.
isOpenStore\&lt;boolean\&gt;Whether auto-approving.
approve(n?)(count?: number) =&gt; voidForward next n pending values.
reject(n?)(count?: number) =&gt; voidDiscard next n pending values.
modify(fn)(fn: (A) =&gt; A) =&gt; voidTransform and forward next pending.
open()() =&gt; voidFlush pending + auto-approve future values.
close()() =&gt; voidRe-enable gating.
source(type, payload?) =&gt; voidUnderlying reactive source for subscriptions.

Basic Usage

ts
import { state, pipe, subscribe } from 'callbag-recharge';
import { gate } from 'callbag-recharge/orchestrate';

const input = state(0);
const gated = pipe(input, gate());

subscribe(gated, v => console.log("approved:", v));
input.set(1);
gated.pending.get();  // [1]
gated.approve();      // logs "approved: 1"
gated.pending.get();  // []

Options / Behavior Details

  • Tier 2: Cycle boundary — each approved value starts a new reactive update cycle.
  • Queue: Values queue while gate is closed. maxPending limits queue size (FIFO drop).
  • Open/close: open() flushes all pending and auto-approves future values. close() re-enables manual gating.
  • Teardown: After the gate's producer is torn down (unsubscribed), all controls throw. Re-subscribing resets the gate to a clean state.

Examples

Auto-approve mode

ts
const gated = pipe(input, gate({ startOpen: true }));
// All values pass through immediately
gated.close(); // Re-enable manual gating

See Also

  • track — lifecycle metadata
  • route — conditional routing

Released under the MIT License.