ui-router-signals
v0.3.0
Published
TC39 Signals for UI-Router: a framework-agnostic signal mirror of the router's current state
Readme
ui-router-signals
TC39 Signals for UI-Router: a framework-agnostic signal mirror of the router's current state, through signal-polyfill.
The design is tracked in #1157. lit-ui-router-signals builds Lit controllers on it.
Installation
npm install ui-router-signals signal-polyfillFeatures
RouterSignals: oneRouteSnapshotper successful transition, in aSignal.State.route,current,paramsandtransitionare read-onlySignal.Computeds over it, and the trackedincludes()answers for that settled snapshot, not the live router.pendinganderrorsit beside the snapshot, fed by oneonStarthook:pendingis the newest transition still in flight, set on start and cleared when that transition settles, however it settles, unless a newer one has started;erroris that transition'sRejectType.ERRORrejection, cleared when the next transition starts. One instance (and its transition hooks) per router viaRouterSignals.for(router).RouteTypes: an empty interface an app or a registry fills in by module augmentation. Itssnapshotmember typesroute, so a snapshot whoseincludes(name)is a type predicate narrowscurrentandparams. Empty,routeholds aRouteSnapshot.watchSelection: watches a selector through aSignal.Computedand hands each new value to a callback, once synchronously and then in a microtask after any source changes. A watcher's notify callback may not read signals, so it schedules the read; several synchronous sets deliver one change.
Loading and error state
import { RouterSignals, watchSelection } from 'ui-router-signals';
const signals = RouterSignals.for(router);
const stop = watchSelection(
() =>
signals.pending.get() ? 'loading' : signals.error.get() ? 'error' : 'idle',
undefined,
(status) => (document.body.dataset.routeStatus = status),
);Dependencies
@uirouter/core and signal-polyfill are peer dependencies. lit and lit-ui-router are not, and the bundleProbe claim in package.json holds the entry free of them. Every signal consumer in a bundle needs the same copy of signal-polyfill; two copies keep two separate signal graphs.
