🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@solid-primitives/static-store

Package Overview
Dependencies
Maintainers
3
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solid-primitives/static-store - npm Package Compare versions

Comparing version
0.1.3
to
1.0.0-next.0
+7
-5
dist/index.d.ts
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 {};
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;
}
{
"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": {

@@ -7,5 +7,6 @@ <p>

[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/static-store?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/static-store)
[![size](https://img.shields.io/badge/size-589_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/static-store)
[![version](https://img.shields.io/npm/v/@solid-primitives/static-store?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/static-store)
[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-2.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)
[![tested with vitest](https://img.shields.io/badge/tested_with-vitest-6E9F18?style=for-the-badge&logo=vitest)](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.