@solid-primitives/static-store
Advanced tools
+7
-5
| import { type SetterParam } from "@solid-primitives/utils"; | ||
| import { type EffectFunction, type MemoOptions, type NoInfer } from "solid-js"; | ||
| import { createMemo, type ComputeFunction, type NoInfer } from "solid-js"; | ||
| type Defined<T> = T extends undefined ? never : T; | ||
| export type MemoOptions<T> = Defined<Parameters<typeof createMemo<T>>[1]>; | ||
| export type StaticStoreSetter<T extends object> = { | ||
@@ -29,3 +31,3 @@ (setter: (prev: T) => Partial<T>): T; | ||
| */ | ||
| export declare function createStaticStore<T extends object>(init: T): [access: T, write: StaticStoreSetter<T>]; | ||
| export declare function createStaticStore<T extends Record<string, Exclude<unknown, Function>>>(init: T): [access: T, write: StaticStoreSetter<T>]; | ||
| /** | ||
@@ -42,3 +44,3 @@ * 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. | ||
| */ | ||
| export declare function createHydratableStaticStore<T extends object>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>; | ||
| export declare function createHydratableStaticStore<T extends Record<string, Exclude<unknown, Function>>>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>; | ||
| /** | ||
@@ -64,3 +66,3 @@ * 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. | ||
| */ | ||
| export declare function createDerivedStaticStore<Next extends Prev & object, Prev = Next>(fn: EffectFunction<undefined | NoInfer<Prev>, Next>): Next; | ||
| export declare function createDerivedStaticStore<Next extends Prev & object, Init = Next, Prev = Next>(fn: EffectFunction<Init | Prev, Next>, value: Init, options?: MemoOptions<Next>): Next; | ||
| export declare function createDerivedStaticStore<Next extends Prev & object, Prev = Next>(fn: ComputeFunction<undefined | NoInfer<Prev>, Next>): Next; | ||
| export {}; |
+41
-23
| import { accessWith, isObject } from "@solid-primitives/utils"; | ||
| import { batch, createMemo, createSignal, getListener, getOwner, onMount, runWithOwner, sharedConfig, untrack, } from "solid-js"; | ||
| import { isServer } from "solid-js/web"; | ||
| import { createMemo, createSignal, getObserver, onSettled, sharedConfig, untrack, } from "solid-js"; | ||
| import { isServer } from "@solidjs/web"; | ||
| /** | ||
@@ -30,5 +30,5 @@ * 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. | ||
| if (!signal) { | ||
| if (!getListener()) | ||
| if (!getObserver()) | ||
| return copy[key]; | ||
| cache[key] = signal = createSignal(copy[key], { internal: true }); | ||
| cache[key] = signal = createSignal(copy[key], { ownedWrite: true }); | ||
| delete copy[key]; | ||
@@ -53,6 +53,4 @@ } | ||
| const entries = untrack(() => Object.entries(accessWith(a, store))); | ||
| batch(() => { | ||
| for (const [key, value] of entries) | ||
| setValue(key, () => value); | ||
| }); | ||
| for (const [key, value] of entries) | ||
| setValue(key, () => value); | ||
| } | ||
@@ -79,5 +77,5 @@ else | ||
| return createStaticStore(serverValue); | ||
| if (sharedConfig.context) { | ||
| if (sharedConfig.hydrating) { | ||
| const [state, setState] = createStaticStore(serverValue); | ||
| onMount(() => setState(update())); | ||
| onSettled(() => { setState(update()); }); | ||
| return [state, setState]; | ||
@@ -87,18 +85,38 @@ } | ||
| } | ||
| export function createDerivedStaticStore(fn, value, options) { | ||
| const o = getOwner(), fnMemo = createMemo(fn, value, options), store = { ...untrack(fnMemo) }, cache = {}; | ||
| for (const key in store) | ||
| Object.defineProperty(store, key, { | ||
| get() { | ||
| let keyMemo = cache[key]; | ||
| if (!keyMemo) { | ||
| if (!getListener()) | ||
| return fnMemo()[key]; | ||
| runWithOwner(o, () => (cache[key] = keyMemo = createMemo(() => fnMemo()[key]))); | ||
| } | ||
| return keyMemo(); | ||
| }, | ||
| /** | ||
| * 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(() => { | ||
| * console.log(store.width, store.height); | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export function createDerivedStaticStore(fn) { | ||
| const fnMemo = createMemo(fn), 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; | ||
| const keyMemo = createMemo(() => fnMemo()[k]); | ||
| Object.defineProperty(store, k, { | ||
| get: () => keyMemo(), | ||
| enumerable: true, | ||
| }); | ||
| } | ||
| return store; | ||
| } |
+9
-6
| { | ||
| "name": "@solid-primitives/static-store", | ||
| "version": "0.1.3", | ||
| "version": "1.0.0-next.0", | ||
| "description": "Primitives for creating small reactive objects that doesn't change their shape over time - don't need a proxy wrapper.", | ||
@@ -18,3 +18,3 @@ "author": "Damian Tarnawski <gthetarnav@gmail.com>", | ||
| "name": "static-store", | ||
| "stage": 2, | ||
| "stage": 3, | ||
| "list": [ | ||
@@ -24,3 +24,4 @@ "createStaticStore", | ||
| ], | ||
| "category": "Reactivity" | ||
| "category": "Reactivity", | ||
| "gzip": 589 | ||
| }, | ||
@@ -49,9 +50,11 @@ "keywords": [ | ||
| "peerDependencies": { | ||
| "solid-js": "^1.6.12" | ||
| "@solidjs/web": "^2.0.0-beta.15", | ||
| "solid-js": "^2.0.0-beta.15" | ||
| }, | ||
| "dependencies": { | ||
| "@solid-primitives/utils": "^6.4.0" | ||
| "@solid-primitives/utils": "^7.0.0-next.0" | ||
| }, | ||
| "devDependencies": { | ||
| "solid-js": "^1.9.7" | ||
| "@solidjs/web": "2.0.0-beta.15", | ||
| "solid-js": "2.0.0-beta.15" | ||
| }, | ||
@@ -58,0 +61,0 @@ "scripts": { |
+2
-1
@@ -7,5 +7,6 @@ <p> | ||
| [](https://bundlephobia.com/package/@solid-primitives/static-store) | ||
| [](https://bundlephobia.com/package/@solid-primitives/static-store) | ||
| [](https://www.npmjs.com/package/@solid-primitives/static-store) | ||
| [](https://github.com/solidjs-community/solid-primitives#contribution-process) | ||
| [](https://vitest.dev) | ||
@@ -12,0 +13,0 @@ Primitives for creating small reactive objects that doesn't change their shape over time - don't need a proxy wrapper. |
Unstable ownership
Supply chain riskA new collaborator has begun publishing package versions. Package stability and security risk may be elevated.
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
15419
7.56%183
12.27%113
0.89%3
50%2
100%1
Infinity%1
Infinity%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed