Skip to content

Index

SynState provides simple, intuitive APIs for managing application state:

Built-in event emitter for event-driven patterns:

For complex scenarios, SynState provides observable-based APIs:

  • source<T>(): Create a new observable source (almost equivalent to RxJS subject).
  • just(value): Create an Observable that holds a single static value and immediately completes. Useful as a synchronous fallback inside switchMap.
  • fromPromise(promise): Create observable from promise.
  • fromSubscribable(): Create observable from any subscribable object.
  • counter(ms): Emit values at intervals (almost equivalent to RxJS interval).
  • timer(delay): Emit after delay.
  • mergeMap(fn): Map to observables and merge all (runs in parallel) (alias: flatMap).
  • switchMap(fn): Map to observables and switch to latest (cancels previous).
  • audit(ms): Emit the last value after specified time window (almost equivalent to RxJS auditTime).
  • debounce(ms): Debounce emissions (almost equivalent to RxJS debounceTime).
  • throttle(ms): Throttle emissions (almost equivalent to RxJS throttleTime).
  • isChildObservable(obs): Check if observable is a child observable.
  • isManagerObservable(obs): Check if observable is a manager observable.
  • isRootObservable(obs): Check if observable is a root observable.

React / Preact Hooks (synstate-react-hooks / synstate-preact-hooks)

Section titled “React / Preact Hooks (synstate-react-hooks / synstate-preact-hooks)”

Both packages share the same API surface. Use synstate-react-hooks for React 18+ and synstate-preact-hooks for Preact.

  • useObservableValue(observable$): Subscribe to an observable and return its current value as component state. Uses useSyncExternalStore internally — the component re-renders when the observable emits.
  • useObservableValue(observable$, fallback): Same as above, with a fallback value for observables without an initial value.
  • useObservableEffect(observable$, fn): Run a side effect whenever the observable emits. Automatically unsubscribes on unmount.
  • useValueAsObservable(value): Convert a component prop/state value into a synstate observable. Useful for bridging React/Preact state into the observable graph.

These re-export core createState / createReducer / createBooleanState but return a hook (useCurrentValue) as the first tuple element instead of the raw observable:

  • createState(initialValue): Returns [useCurrentValue, setState, { state, updateState, resetState, getSnapshot, initialState }].
  • createReducer(reducer, initialState): Returns [useCurrentValue, dispatch, { state, getSnapshot, initialState }].
  • createBooleanState(initialValue): Returns [useCurrentValue, { state, setTrue, setFalse, toggle, setState, updateState, resetState, getSnapshot, initialState }].

Bridges synstate observables with Preact Signals for fine-grained DOM updates without component re-renders. See the interactive demo to compare with the hooks approach.

  • toSignal(observable$): Convert an Observable (or InitializedObservable) into a Preact ReadonlySignal. The signal stays in sync with the observable. Can be embedded directly in JSX — only the affected text node updates, not the entire component.
  • toSignal(observable$, fallback): Same as above, with a fallback value for observables without an initial value.
  • fromSignal(signal): Convert a Preact Signal into a synstate InitializedSourceObservable. Returns [observable, dispose]. The observable mirrors the signal’s value, enabling the full synstate operator chain (map, debounce, switchMap, etc.).

Same as the hooks wrappers, but return a ReadonlySignal instead of a hook:

  • createState(initialValue): Returns [signal, setState, { state, updateState, resetState, getSnapshot, initialState }].
  • createReducer(reducer, initialState): Returns [signal, dispatch, { state, getSnapshot, initialState }].
  • createBooleanState(initialValue): Returns [signal, { state, setTrue, setFalse, toggle, setState, updateState, resetState, getSnapshot, initialState }].