Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

routerStores.ts

41 lines1.1 KB

1import * as Solid from 'solid-js'
2import {
3 createNonReactiveMutableStore,
4 createNonReactiveReadonlyStore,
5} from '@tanstack/router-core'
6import { isServer } from '@tanstack/router-core/isServer'
7import type {
8 GetStoreConfig,
9 RouterReadableStore,
10 RouterWritableStore,
11} from '@tanstack/router-core'
12​
13function createSolidMutableStore<TValue>(
14 initialValue: TValue,
15): RouterWritableStore<TValue> {
16 const [signal, setSignal] = Solid.createSignal(initialValue)
17​
18 return { get: signal, set: setSignal }
20​
21function createSolidReadonlyStore<TValue>(
22 read: () => TValue,
23): RouterReadableStore<TValue> {
24 return { get: Solid.createRoot(() => Solid.createMemo(read)) }
26​
27export const getStoreFactory: GetStoreConfig = (opts) => {
28 if (isServer ?? opts.isServer) {
29 return {
30 createMutableStore: createNonReactiveMutableStore,
31 createReadonlyStore: createNonReactiveReadonlyStore,
32 batch: (fn) => fn(),
33 }
34 }
35​
36 return {
37 createMutableStore: createSolidMutableStore,
38 createReadonlyStore: createSolidReadonlyStore,
39 batch: Solid.batch,
40 }