UNPKG

@tanstack/router-core

Version:

Modern and scalable routing for React applications

148 lines (147 loc) 5.27 kB
const require_utils = require("./utils.cjs"); const require_lru_cache = require("./lru-cache.cjs"); //#region src/stores.ts /** SSR non-reactive createMutableStore */ function createNonReactiveMutableStore(initialValue) { let value = initialValue; return { get state() { return value; }, setState(updater) { value = updater(value); } }; } /** SSR non-reactive createReadonlyStore */ function createNonReactiveReadonlyStore(read) { return { get state() { return read(); } }; } function createRouterStores(initialState, config) { const { createMutableStore, createReadonlyStore, batch, init } = config; const activeMatchStoresById = /* @__PURE__ */ new Map(); const pendingMatchStoresById = /* @__PURE__ */ new Map(); const cachedMatchStoresById = /* @__PURE__ */ new Map(); const status = createMutableStore(initialState.status); const loadedAt = createMutableStore(initialState.loadedAt); const isLoading = createMutableStore(initialState.isLoading); const isTransitioning = createMutableStore(initialState.isTransitioning); const location = createMutableStore(initialState.location); const resolvedLocation = createMutableStore(initialState.resolvedLocation); const statusCode = createMutableStore(initialState.statusCode); const redirect = createMutableStore(initialState.redirect); const matchesId = createMutableStore([]); const pendingMatchesId = createMutableStore([]); const cachedMatchesId = createMutableStore([]); const activeMatchesSnapshot = createReadonlyStore(() => readPoolMatches(activeMatchStoresById, matchesId.state)); const pendingMatchesSnapshot = createReadonlyStore(() => readPoolMatches(pendingMatchStoresById, pendingMatchesId.state)); const cachedMatchesSnapshot = createReadonlyStore(() => readPoolMatches(cachedMatchStoresById, cachedMatchesId.state)); const firstMatchId = createReadonlyStore(() => matchesId.state[0]); const hasPendingMatches = createReadonlyStore(() => matchesId.state.some((matchId) => { return activeMatchStoresById.get(matchId)?.state.status === "pending"; })); const matchRouteReactivity = createReadonlyStore(() => ({ locationHref: location.state.href, resolvedLocationHref: resolvedLocation.state?.href, status: status.state })); const __store = createReadonlyStore(() => ({ status: status.state, loadedAt: loadedAt.state, isLoading: isLoading.state, isTransitioning: isTransitioning.state, matches: activeMatchesSnapshot.state, location: location.state, resolvedLocation: resolvedLocation.state, statusCode: statusCode.state, redirect: redirect.state })); const matchStoreByRouteIdCache = require_lru_cache.createLRUCache(64); function getMatchStoreByRouteId(routeId) { let cached = matchStoreByRouteIdCache.get(routeId); if (!cached) { cached = createReadonlyStore(() => { const ids = matchesId.state; for (const id of ids) { const matchStore = activeMatchStoresById.get(id); if (matchStore && matchStore.routeId === routeId) return matchStore.state; } }); matchStoreByRouteIdCache.set(routeId, cached); } return cached; } const store = { status, loadedAt, isLoading, isTransitioning, location, resolvedLocation, statusCode, redirect, matchesId, pendingMatchesId, cachedMatchesId, activeMatchesSnapshot, pendingMatchesSnapshot, cachedMatchesSnapshot, firstMatchId, hasPendingMatches, matchRouteReactivity, activeMatchStoresById, pendingMatchStoresById, cachedMatchStoresById, __store, getMatchStoreByRouteId, setActiveMatches, setPendingMatches, setCachedMatches }; setActiveMatches(initialState.matches); init?.(store); function setActiveMatches(nextMatches) { reconcileMatchPool(nextMatches, activeMatchStoresById, matchesId, createMutableStore, batch); } function setPendingMatches(nextMatches) { reconcileMatchPool(nextMatches, pendingMatchStoresById, pendingMatchesId, createMutableStore, batch); } function setCachedMatches(nextMatches) { reconcileMatchPool(nextMatches, cachedMatchStoresById, cachedMatchesId, createMutableStore, batch); } return store; } function readPoolMatches(pool, ids) { const matches = []; for (const id of ids) { const matchStore = pool.get(id); if (matchStore) matches.push(matchStore.state); } return matches; } function reconcileMatchPool(nextMatches, pool, idStore, createMutableStore, batch) { const nextIds = nextMatches.map((d) => d.id); const nextIdSet = new Set(nextIds); batch(() => { for (const id of pool.keys()) if (!nextIdSet.has(id)) pool.delete(id); for (const nextMatch of nextMatches) { const existing = pool.get(nextMatch.id); if (!existing) { const matchStore = createMutableStore(nextMatch); matchStore.routeId = nextMatch.routeId; pool.set(nextMatch.id, matchStore); continue; } existing.routeId = nextMatch.routeId; if (existing.state !== nextMatch) existing.setState(() => nextMatch); } if (!require_utils.arraysEqual(idStore.state, nextIds)) idStore.setState(() => nextIds); }); } //#endregion exports.createNonReactiveMutableStore = createNonReactiveMutableStore; exports.createNonReactiveReadonlyStore = createNonReactiveReadonlyStore; exports.createRouterStores = createRouterStores; //# sourceMappingURL=stores.cjs.map