UNPKG

@platform/state

Version:

A small, simple, strongly typed, [rx/observable] state-machine.

21 lines (20 loc) 803 B
import { filter, map, share, takeUntil } from 'rxjs/operators'; export function create(event$, dispose$) { const $ = event$.pipe(takeUntil(dispose$)); const changing$ = $.pipe(filter((e) => e.type === 'StateObject/changing'), map((e) => e.payload), share()); const changed$ = $.pipe(filter((e) => e.type === 'StateObject/changed'), map((e) => e.payload), share()); const patched$ = changed$.pipe(map((e) => { const { op, cid, action } = e; const { prev, next } = e.patches; return { op, cid, action, prev, next }; })); const cancelled$ = $.pipe(filter((e) => e.type === 'StateObject/cancelled'), map((e) => e.payload), share()); return { $, dispose$, changing$, changed$, patched$, cancelled$, }; }