UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

60 lines (59 loc) 1.83 kB
import { useRouter } from "./useRouter.js"; import { getLocationChangeInfo, trimPathRight } from "@tanstack/router-core"; import * as Solid from "solid-js"; //#region src/Transitioner.tsx 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(); router.startTransition = async (fn) => { const result = Solid.runWithOwner(null, fn); try { Solid.flush(); } catch {} await result; await new Promise((resolve) => queueMicrotask(resolve)); return true; }; Solid.onSettled(() => { const unsub = router.history.subscribe(() => { 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