@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
82 lines • 2.63 kB
JavaScript
import * as Solid from 'solid-js';
import { createNonReactiveMutableStore, createNonReactiveReadonlyStore, } from '@tanstack/router-core';
import { isServer } from '@tanstack/router-core/isServer';
function initRouterStores(stores, createReadonlyStore) {
stores.childMatchIdByRouteId = createReadonlyStore(() => {
const ids = stores.matchesId.state;
const obj = {};
for (let i = 0; i < ids.length - 1; i++) {
const parentStore = stores.activeMatchStoresById.get(ids[i]);
if (parentStore?.routeId) {
obj[parentStore.routeId] = ids[i + 1];
}
}
return obj;
});
stores.pendingRouteIds = createReadonlyStore(() => {
const ids = stores.pendingMatchesId.state;
const obj = {};
for (const id of ids) {
const store = stores.pendingMatchStoresById.get(id);
if (store?.routeId) {
obj[store.routeId] = true;
}
}
return obj;
});
}
function createSolidMutableStore(initialValue) {
const [signal, setSignal] = Solid.createSignal(initialValue);
return {
get state() {
return signal();
},
setState: setSignal,
};
}
let finalizationRegistry = null;
if (typeof globalThis !== 'undefined' && 'FinalizationRegistry' in globalThis) {
finalizationRegistry = new FinalizationRegistry((cb) => cb());
}
function createSolidReadonlyStore(read) {
let dispose;
const memo = Solid.createRoot((d) => {
dispose = d;
return Solid.createMemo(read);
});
const store = {
get state() {
return memo();
},
};
finalizationRegistry?.register(store, dispose);
return store;
}
export const getStoreFactory = (opts) => {
if (isServer ?? opts.isServer) {
return {
createMutableStore: createNonReactiveMutableStore,
createReadonlyStore: createNonReactiveReadonlyStore,
batch: (fn) => fn(),
init: (stores) => initRouterStores(stores, createNonReactiveReadonlyStore),
};
}
let depth = 0;
return {
createMutableStore: createSolidMutableStore,
createReadonlyStore: createSolidReadonlyStore,
batch: (fn) => {
depth++;
fn();
depth--;
if (depth === 0) {
try {
Solid.flush();
}
catch { }
}
},
init: (stores) => initRouterStores(stores, createSolidReadonlyStore),
};
};
//# sourceMappingURL=routerStores.js.map