UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

52 lines (51 loc) 1.46 kB
import { createNonReactiveMutableStore, createNonReactiveReadonlyStore } from "@tanstack/router-core"; import * as Solid from "solid-js"; import { isServer } from "@tanstack/router-core/isServer"; //#region src/routerStores.ts var getStoreFactory = (opts) => { if (isServer ?? opts.isServer) return { createMutableStore: createNonReactiveMutableStore, createReadonlyStore: createNonReactiveReadonlyStore, batch: (fn) => fn() }; let writeEpoch = 0; function createSolidMutableStore(initialValue) { const [signal, setSignal] = Solid.createSignal(initialValue); let current = initialValue; let dirty = false; return { get: () => { const settled = signal(); if (dirty && settled === current) dirty = false; return dirty ? current : settled; }, set: (next) => { current = typeof next === "function" ? next(current) : next; dirty = true; writeEpoch++; setSignal(() => current); } }; } function createSolidReadonlyStore(read) { const memo = Solid.createRoot(() => Solid.createMemo(read)); let cache; let cachedAt = -1; return { get: () => { memo(); if (cachedAt !== writeEpoch) { cache = Solid.untrack(read); cachedAt = writeEpoch; } return cache; } }; } return { createMutableStore: createSolidMutableStore, createReadonlyStore: createSolidReadonlyStore, batch: (fn) => fn() }; }; //#endregion export { getStoreFactory }; //# sourceMappingURL=routerStores.js.map