UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

117 lines (116 loc) 4.16 kB
import { useRouter } from "./useRouter.js"; import { getLocationChangeInfo, trimPathRight } from "@tanstack/router-core"; import * as Solid from "solid-js"; import { isServer } from "@tanstack/router-core/isServer"; //#region src/Transitioner.tsx /** * Solid's observe tier (`OBSERVE` is defined on the dev and observe builds, * undefined in production) attributes what the user waited on to the * navigation that caused it. The rule for every router is the same: wrap the * write whose landing is the destination showing, and pass `at` when the * request predates that write. Here that write is the match publish inside * `startTransition` — the loaders were awaited in router-core before it — * so the ref names the destination route from the expected matches and * dates from the history change that started the load. The pending offer * (`offerPending`, a match with `status: 'pending'`) is not the destination * and is published undeclared; the initial load and a same-location reload * are not navigations. */ function describeNavigation(router, expected, at) { if (expected.some((match) => match.status === "pending")) return; const to = router.latestLocation; const from = router.stores.resolvedLocation.get(); if (!from || from.href === to.href) return; const leaf = expected[expected.length - 1]; const ref = { kind: "navigation", name: leaf?.fullPath || to.pathname, to: to.pathname, from: from.pathname }; if (leaf && Object.keys(leaf.params).length) ref.params = leaf.params; if (at !== void 0) ref.at = at; return ref; } function getResolvedLocation(router) { const resolvedLocation = router.stores.resolvedLocation.get(); if (resolvedLocation?.href === router.latestLocation.href && resolvedLocation.state.__TSR_key === router.latestLocation.state.__TSR_key) return resolvedLocation; } function Transitioner() { const router = useRouter(); const acks = []; let committed; const isCommitted = (expected) => !!committed && committed.length === expected.length && expected.every((match, index) => committed[index] === match); let requestedAt; router.startTransition = (fn, expectedMatches) => { if (isServer ?? router.isServer) { fn(); return Promise.resolve(true); } return new Promise((resolve) => { const ack = [expectedMatches, resolve]; acks.push(ack); let publish = fn; if (Solid.OBSERVE !== void 0) { const ref = describeNavigation(router, expectedMatches, requestedAt); if (ref !== void 0) { requestedAt = void 0; const observe = Solid.OBSERVE; publish = () => observe.attribution.withOrigin(ref, fn); } } Solid.runWithOwner(null, publish); try { Solid.flush(); } catch {} if (acks.includes(ack) && isCommitted(expectedMatches)) { acks.splice(acks.indexOf(ack), 1); resolve(true); } }); }; Solid.createEffect(() => router.stores.matches.get(), (current) => { committed = current; if (acks.length) for (const [expected, resolve] of acks.splice(0)) resolve(isCommitted(expected)); }); Solid.onSettled(() => { const unsub = router.history.subscribe(() => { requestedAt ??= performance.now(); queueMicrotask(() => router.load().catch(console.error)); }); router.updateLatestLocation(); const nextLocation = router.buildLocation({ to: router.latestLocation.pathname, search: true, params: true, hash: true, state: true, _includeValidateSearch: true }); if (trimPathRight(router.latestLocation.publicHref) !== trimPathRight(nextLocation.publicHref)) { router.commitLocation({ ...nextLocation, replace: true, ignoreBlocker: true }); return unsub; } if (!getResolvedLocation(router) && !router._tx) queueMicrotask(() => router.load().catch(console.error)); return unsub; }); return null; } function Rendered() { const router = useRouter(); Solid.onSettled(() => { const resolvedLocation = getResolvedLocation(router); if (resolvedLocation) router.emit({ type: "onRendered", ...getLocationChangeInfo(resolvedLocation, resolvedLocation) }); }); return null; } //#endregion export { Rendered, Transitioner }; //# sourceMappingURL=Transitioner.js.map