@tanstack/router-core
Version:
Modern and scalable routing for React applications
72 lines (71 loc) • 2.11 kB
JavaScript
import { arraysEqual, functionalUpdate } from "./utils.js";
//#region src/stores.ts
/** SSR non-reactive createMutableStore */
function createNonReactiveMutableStore(initialValue) {
let value = initialValue;
return {
get() {
return value;
},
set(nextOrUpdater) {
value = functionalUpdate(nextOrUpdater, value);
}
};
}
/** SSR non-reactive createReadonlyStore */
function createNonReactiveReadonlyStore(read) {
return { get() {
return read();
} };
}
function createRouterStores(initialLocation, config) {
const { createMutableStore, createReadonlyStore, batch } = config;
const byRoute = /* @__PURE__ */ new Map();
const status = createMutableStore("idle");
const location = createMutableStore(initialLocation);
const resolvedLocation = createMutableStore(void 0);
const ids = createMutableStore([]);
const matches = createReadonlyStore(() => ids.get().map((id) => byRoute.get(id).get()));
const __store = createReadonlyStore(() => ({
status: status.get(),
isLoading: status.get() === "pending",
matches: matches.get(),
location: location.get(),
resolvedLocation: resolvedLocation.get()
}));
function getMatchStore(routeId) {
let matchStore = byRoute.get(routeId);
if (!matchStore) {
matchStore = createMutableStore(void 0);
byRoute.set(routeId, matchStore);
}
return matchStore;
}
const store = {
status,
location,
resolvedLocation,
ids,
matches,
byRoute,
__store,
getMatchStore,
setMatches
};
function setMatches(nextMatches) {
const previousIds = ids.get();
const nextIds = nextMatches.map((match) => match.routeId);
batch(() => {
if (!arraysEqual(previousIds, nextIds)) ids.set(nextIds);
for (const id of previousIds) if (!nextIds.includes(id)) byRoute.get(id).set(() => void 0);
for (const nextMatch of nextMatches) {
const matchStore = getMatchStore(nextMatch.routeId);
if (matchStore.get() !== nextMatch) matchStore.set(nextMatch);
}
});
}
return store;
}
//#endregion
export { createNonReactiveMutableStore, createNonReactiveReadonlyStore, createRouterStores };
//# sourceMappingURL=stores.js.map