combinators
Functions for combining and transforming multiple Behaviors.
combine
Combines multiple Behaviors into one using a combining function. The result updates whenever any source Behavior changes.
Signature
function combine<A, B, R>(a: Behavior<A>, b: Behavior<B>, f: (a: A, b: B) => R): Behavior<R>function combine<A, B, C, R>(a: Behavior<A>, b: Behavior<B>, c: Behavior<C>, f: (a: A, b: B, c: C) => R): Behavior<R>// ... up to 5 behaviorsExample
import { stepper, combine } from "cereb/frp";
const position = stepper({ x: 0, y: 0 }, posStream, (s) => s.value.position);const scale = stepper(1, scaleStream, (s) => s.value.scale);const rotation = stepper(0, rotStream, (s) => s.value.rotation);
// Combine into a transformconst transform = combine( position, scale, rotation, (pos, s, rot) => `translate(${pos.x}px, ${pos.y}px) scale(${s}) rotate(${rot}deg)`);
// Sample the combined valueelement.style.transform = transform.sample();
// Or subscribe to changestransform.onChange((t) => { element.style.transform = t;});Parameters
| Parameter | Type | Description |
|---|---|---|
a, b, ... | Behavior<*> | Source behaviors (2-5) |
f | (...values) => R | Combining function |
Notes
- When multiple sources change,
onChangefires for each change (glitch behavior) - Use
sample()for consistent snapshots
switcher
Selects between two Behaviors based on a boolean condition Behavior.
Signature
function switcher<A>( condition: Behavior<boolean>, ifTrue: Behavior<A>, ifFalse: Behavior<A>): Behavior<A>Example
import { constant, stepper, switcher } from "cereb/frp";
const isEditing = stepper(false, modeStream, (s) => s.value.editing);const editValue = stepper("", inputStream, (s) => s.value.text);const savedValue = constant("Saved content");
// Switch between edit and display modeconst displayValue = switcher(isEditing, editValue, savedValue);
displayValue.sample(); // Returns editValue.sample() or savedValue.sample()Parameters
| Parameter | Type | Description |
|---|---|---|
condition | Behavior<boolean> | The condition behavior |
ifTrue | Behavior<A> | Value when condition is true |
ifFalse | Behavior<A> | Value when condition is false |
Notes
- Only the active branch triggers
onChangenotifications - When condition changes, the new branch’s current value is emitted
lift
Lifts a pure function to work with Behaviors. This is a convenience for applying functions to Behavior values.
Signature
function lift<A, R>(f: (a: A) => R): (ba: Behavior<A>) => Behavior<R>function lift<A, B, R>(f: (a: A, b: B) => R): (ba: Behavior<A>, bb: Behavior<B>) => Behavior<R>function lift<A, B, C, R>(f: (a: A, b: B, c: C) => R): (ba: Behavior<A>, bb: Behavior<B>, bc: Behavior<C>) => Behavior<R>Example
import { constant, lift } from "cereb/frp";
// Lift a binary functionconst add = (a: number, b: number) => a + b;const liftedAdd = lift(add);
const a = constant(3);const b = constant(4);const sum = liftedAdd(a, b);
sum.sample(); // 7
// Lift a unary functionconst double = (x: number) => x * 2;const liftedDouble = lift(double);
const doubled = liftedDouble(a);doubled.sample(); // 6Parameters
| Parameter | Type | Description |
|---|---|---|
f | (...args) => R | The pure function to lift |
Returns
A function that takes Behaviors and returns a Behavior.
Notes
liftsupports up to 3 behaviors. For more, usecombine()directly- Equivalent to
combine(a, b, c, f)but with curried syntax