import { accessWith, isObject, type SetterParam } from "@solid-primitives/utils"; import { createMemo, createSignal, type ComputeFunction, getObserver, type NoInfer, onSettled, type Signal, type SignalOptions, untrack, } from "solid-js"; import { sharedConfig } from "solid-js/internal"; import { isServer } from "@solidjs/web"; type Defined = T extends undefined ? never : T; export type MemoOptions = Defined>[1]>; export type StaticStoreSetter = { (setter: (prev: T) => Partial): T; (state: Partial): T; (key: K, state: SetterParam): T; }; /** * A shallowly wrapped reactive store object. It behaves similarly to the createStore, but with limited features to keep it simple. Designed to be used for reactive objects with static keys, but dynamic values, like reactive Event State, location, etc. * @param init initial value of the store * @returns tuple with the store object and a setter function * ```ts * [access: T, write: StaticStoreSetter] * ``` * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/static-store#createStaticStore * @example * ```ts * const [size, setSize] = createStaticStore({ width: 0, height: 0 }); * * el.addEventListener("resize", () => { * setSize({ width: el.offsetWidth, height: el.offsetHeight }); * }); * * createEffect( * () => [size.width, size.height], * ([width, height]) => console.log(width, height), * ); * ``` */ export function createStaticStore>>( init: T, ): [access: T, write: StaticStoreSetter] { const copy = { ...init }, store = { ...init }, cache: Partial>> = {}; const getValue = (key: keyof T): T[keyof T] => { let signal = cache[key]; if (!signal) { if (!getObserver()) return copy[key]; cache[key] = signal = createSignal( copy[key] as Exclude, { ownedWrite: true } as SignalOptions, ); delete copy[key]; } return signal[0](); }; for (const key in init) { Object.defineProperty(store, key, { get: () => getValue(key), enumerable: true }); } const setValue = (key: keyof T, value: SetterParam): void => { const signal = cache[key]; if (signal) return signal[1](value); if (key in copy) copy[key] = accessWith(value, copy[key]); }; return [ store, (a: ((prev: T) => Partial) | Partial | keyof T, b?: SetterParam) => { if (isObject(a)) { const entries = untrack( () => Object.entries(accessWith(a, store) as Partial) as [any, any][], ); for (const [key, value] of entries) setValue(key, () => value); } else setValue(a, b); return store; }, ]; } /** * A hydratable version of the {@link createStaticStore}. It will use the {@link serverValue} on the server and the {@link update} function on the client. If initialized during hydration it will use {@link serverValue} as the initial value and update it once hydration is complete. * * @param serverValue initial value of the state on the server * @param update called once on the client or on hydration to initialize the value * @returns tuple with the store object and a setter function * ```ts * [access: T, write: StaticStoreSetter] * ``` * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/static-store#createHydratableStaticStore */ export function createHydratableStaticStore>>( serverValue: T, update: () => T, ): ReturnType> { if (isServer || sharedConfig.hydrating) { const [state, setState] = createStaticStore(serverValue); onSettled(() => { setState(update()); }); return [state, setState]; } return createStaticStore(update()); } /** * A derived version of the {@link createStaticStore}. It will use the update function to derive the value of the store. It will only update when the dependencies of the update function change. * @param fn a reactive function to derive the value of the store * @returns a shallow, reactive, static store object * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/static-store#createDerivedStaticStore * @example * ```ts * const [size, setSize] = createSignal({ width: 0, height: 0 }); * * el.addEventListener("resize", () => { * setSize({ width: el.offsetWidth, height: el.offsetHeight }); * }); * * const store = createDerivedStaticStore(size); * * createEffect( * () => [store.width, store.height], * ([width, height]) => console.log(width, height), * ); * ``` */ export function createDerivedStaticStore( fn: ComputeFunction, Next>, ): Next { const fnMemo = createMemo(fn as ComputeFunction, Next>), store = { ...untrack(fnMemo) }; // Eagerly create per-key memos so their child IDs are allocated at initialization // time on both server and client. The previous lazy approach created memos on first // reactive access — which happened inside _$className render-effect computes on the // client but not on the server (SSR doesn't run render-effect computes with a live // observer), shifting every subsequent hydration key by +1 per lazily-created memo. for (const key in store) { const k = key as keyof Next; const keyMemo = createMemo(() => fnMemo()[k]); Object.defineProperty(store, k, { get: () => keyMemo(), enumerable: true, }); } return store; }