ui-router-effect
v0.3.0
Published
Effect bindings for UI-Router: a framework-agnostic SubscriptionRef of the router's current state
Readme
ui-router-effect
Effect bindings for UI-Router: a framework-agnostic SubscriptionRef of the router's current state. It imports only effect and @uirouter/core.
lit-ui-router-effect builds Lit controllers on it and re-exports routeRef.
Installation
npm install ui-router-effect effectFeatures
routeRef(router): aSubscriptionRef<TypedRouteSnapshot>of the current state, params and transition. There is one ref, and oneonSuccesshook, per router, attached lazily on first use. Every value it holds is a settled route.pendingRef(router)anderrorRef(router):SubscriptionRefs of the newest transition, kept out ofRouteSnapshot. Two refs, and oneonStarthook, per router, attached lazily on first use of either.pendingRefholds the transition still in flight, cleared when that transition settles, however it settles, unless a newer one has started.errorRefholds that transition'sRejectType.ERRORrejection, cleared when the next transition starts.RouteSnapshotandsnapshotRoute(router, transition?): the value the ref holds. Itsincludes()evaluatesStateService.includesagainst the snapshot, not the live router.RouteTypes: an empty interface an app or a registry fills in by module augmentation. Itssnapshotmember types the ref's value, so a snapshot whoseincludes(name)is a type predicate narrowscurrentandparams. Empty, the ref holds aRouteSnapshot.snapshotRoutestays untyped.
Usage
import { Effect, Stream, SubscriptionRef } from 'effect';
import { routeRef } from 'ui-router-effect';
const names = SubscriptionRef.changes(routeRef(router)).pipe(
Stream.map((route) => route.current?.name),
Stream.changes,
);
Effect.runFork(Stream.runForEach(names, (name) => Effect.log(name)));Loading and error state
import { Effect, Stream, SubscriptionRef } from 'effect';
import { errorRef, pendingRef } from 'ui-router-effect';
const status = Stream.zipLatestWith(
SubscriptionRef.changes(pendingRef(router)),
SubscriptionRef.changes(errorRef(router)),
(pending, error) => (pending ? 'loading' : error ? 'error' : 'idle'),
).pipe(Stream.changes);
Effect.runFork(
Stream.runForEach(status, (value) =>
Effect.sync(() => (document.body.dataset.routeStatus = value)),
),
);Dependencies
@uirouter/core and effect are peer dependencies. lit and lit-ui-router are not, and the bundleProbe claim in package.json holds the entry free of them.
