@weasel-js/prefs 1.9.4 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -0
- package/dist/chunk-ASSX3JHY.js +1180 -0
- package/dist/chunk-ASSX3JHY.js.map +1 -0
- package/dist/index.d.ts +376 -43
- package/dist/index.js +385 -564
- package/dist/index.js.map +1 -1
- package/dist/{store-CNFnA_3c.d.ts → open-C83pU7Va.d.ts} +315 -61
- package/dist/react.d.ts +33 -4
- package/dist/react.js +75 -3
- package/dist/react.js.map +1 -1
- package/package.json +5 -4
package/dist/react.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { openPrefsCacheSync, createPrefsStore } from './chunk-ASSX3JHY.js';
|
|
1
2
|
import { useCallback, useSyncExternalStore } from 'react';
|
|
2
3
|
|
|
3
|
-
// src/hooks.ts
|
|
4
4
|
function usePref(store, path) {
|
|
5
5
|
const subscribe = useCallback((onChange) => store.subscribe(onChange), [store]);
|
|
6
6
|
const getSnapshot = () => store.get(path);
|
|
@@ -21,14 +21,86 @@ function usePrefsValues(store) {
|
|
|
21
21
|
const values = useSyncExternalStore(subscribe, store.values, store.values);
|
|
22
22
|
const unset = useSyncExternalStore(subscribe, store.unset, store.unset);
|
|
23
23
|
const stored = useSyncExternalStore(subscribe, store.stored, store.stored);
|
|
24
|
+
const loci = useSyncExternalStore(subscribe, store.loci, store.loci);
|
|
24
25
|
const set = useCallback(
|
|
25
26
|
(path, value) => store.set(path, value),
|
|
26
27
|
[store]
|
|
27
28
|
);
|
|
28
29
|
const reset = useCallback((path) => store.reset(path), [store]);
|
|
29
|
-
|
|
30
|
+
const split = useCallback((path, by) => store.split(path, by), [store]);
|
|
31
|
+
const join = useCallback((path, by) => store.join(path, by), [store]);
|
|
32
|
+
const setAt = useCallback(
|
|
33
|
+
(path, value, at) => store.set(path, value, { at }),
|
|
34
|
+
[store]
|
|
35
|
+
);
|
|
36
|
+
return { values, set, unset, stored, reset, loci, split, join, setAt };
|
|
37
|
+
}
|
|
38
|
+
var shared = /* @__PURE__ */ new WeakMap();
|
|
39
|
+
function sharedAt(storage, prefix) {
|
|
40
|
+
let byPrefix = shared.get(storage);
|
|
41
|
+
if (!byPrefix) shared.set(storage, byPrefix = /* @__PURE__ */ new Map());
|
|
42
|
+
let entry = byPrefix.get(prefix);
|
|
43
|
+
if (!entry) byPrefix.set(prefix, entry = { open: null, bySchema: /* @__PURE__ */ new Map() });
|
|
44
|
+
return entry;
|
|
45
|
+
}
|
|
46
|
+
function heldAt(at, schema) {
|
|
47
|
+
let entry = at.bySchema.get(schema);
|
|
48
|
+
if (!entry) at.bySchema.set(schema, entry = { store: null, holders: 0 });
|
|
49
|
+
return entry;
|
|
50
|
+
}
|
|
51
|
+
var lent = (cache) => Object.create(cache, {
|
|
52
|
+
close: {
|
|
53
|
+
value: async () => {
|
|
54
|
+
await cache.flush();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
function closeUnheldSoon(storage, prefix, schema, entry) {
|
|
59
|
+
queueMicrotask(() => {
|
|
60
|
+
if (entry.holders > 0) return;
|
|
61
|
+
void entry.store?.close();
|
|
62
|
+
entry.store = null;
|
|
63
|
+
const byPrefix = shared.get(storage);
|
|
64
|
+
const at = byPrefix?.get(prefix);
|
|
65
|
+
if (!at || !byPrefix) return;
|
|
66
|
+
if (at.bySchema.get(schema) === entry) at.bySchema.delete(schema);
|
|
67
|
+
if (at.bySchema.size > 0) return;
|
|
68
|
+
at.open?.stop();
|
|
69
|
+
void at.open?.cache.close();
|
|
70
|
+
byPrefix.delete(prefix);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function acquire(schema, options) {
|
|
74
|
+
const { storage, prefix } = options;
|
|
75
|
+
const at = sharedAt(storage, prefix);
|
|
76
|
+
const entry = heldAt(at, schema);
|
|
77
|
+
if (!entry.store) {
|
|
78
|
+
at.open ??= openPrefsCacheSync(options);
|
|
79
|
+
entry.store = createPrefsStore(
|
|
80
|
+
schema,
|
|
81
|
+
lent(at.open.cache),
|
|
82
|
+
options.validators,
|
|
83
|
+
void 0,
|
|
84
|
+
options.migrations?.length ?? 0,
|
|
85
|
+
options.blends
|
|
86
|
+
);
|
|
87
|
+
if (entry.holders === 0) closeUnheldSoon(storage, prefix, schema, entry);
|
|
88
|
+
}
|
|
89
|
+
return entry.store;
|
|
90
|
+
}
|
|
91
|
+
function usePrefsStore(schema, options) {
|
|
92
|
+
const { storage, prefix } = options;
|
|
93
|
+
const hold = useCallback(() => {
|
|
94
|
+
const entry = heldAt(sharedAt(storage, prefix), schema);
|
|
95
|
+
entry.holders++;
|
|
96
|
+
return () => {
|
|
97
|
+
if (--entry.holders === 0) closeUnheldSoon(storage, prefix, schema, entry);
|
|
98
|
+
};
|
|
99
|
+
}, [storage, prefix, schema]);
|
|
100
|
+
const current = () => acquire(schema, options);
|
|
101
|
+
return useSyncExternalStore(hold, current, current);
|
|
30
102
|
}
|
|
31
103
|
|
|
32
|
-
export { usePref, usePrefsValues };
|
|
104
|
+
export { usePref, usePrefsStore, usePrefsValues };
|
|
33
105
|
//# sourceMappingURL=react.js.map
|
|
34
106
|
//# sourceMappingURL=react.js.map
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks.ts"],"names":[],"mappings":";;;AAMO,SAAS,OAAA,CACd,OACA,IAAA,EAIA;AACA,EAAA,MAAM,SAAA,GAAY,WAAA,CAAY,CAAC,QAAA,KAAyB,KAAA,CAAM,UAAU,QAAQ,CAAA,EAAG,CAAC,KAAK,CAAC,CAAA;AAC1F,EAAA,MAAM,WAAA,GAAc,MAAM,KAAA,CAAM,GAAA,CAAI,IAAI,CAAA;AACxC,EAAA,MAAM,KAAA,GAAQ,oBAAA,CAAqB,SAAA,EAAW,WAAA,EAAa,WAAW,CAAA;AACtE,EAAA,MAAM,GAAA,GAAM,WAAA;AAAA,IACV,CAAC,IAAA,KAA+E;AAC9E,MAAA,KAAA,CAAM,GAAA;AAAA,QACJ,IAAA;AAAA,QACA,OAAO,SAAS,UAAA,GACX,IAAA,CAAwD,MAAM,GAAA,CAAI,IAAI,CAAC,CAAA,GACxE;AAAA,OACN;AAAA,IACF,CAAA;AAAA,IACA,CAAC,OAAO,IAAI;AAAA,GACd;AACA,EAAA,OAAO,CAAC,OAAO,GAAG,CAAA;AACpB;AAKO,SAAS,eAAoC,KAAA,EAMlD;AACA,EAAA,MAAM,SAAA,GAAY,WAAA,CAAY,CAAC,QAAA,KAAyB,KAAA,CAAM,UAAU,QAAQ,CAAA,EAAG,CAAC,KAAK,CAAC,CAAA;AAC1F,EAAA,MAAM,SAAS,oBAAA,CAAqB,SAAA,EAAW,KAAA,CAAM,MAAA,EAAQ,MAAM,MAAM,CAAA;AACzE,EAAA,MAAM,QAAQ,oBAAA,CAAqB,SAAA,EAAW,KAAA,CAAM,KAAA,EAAO,MAAM,KAAK,CAAA;AACtE,EAAA,MAAM,SAAS,oBAAA,CAAqB,SAAA,EAAW,KAAA,CAAM,MAAA,EAAQ,MAAM,MAAM,CAAA;AACzE,EAAA,MAAM,GAAA,GAAM,WAAA;AAAA,IACV,CAAC,IAAA,EAAc,KAAA,KAAmB,KAAA,CAAM,GAAA,CAAI,MAAqB,KAAc,CAAA;AAAA,IAC/E,CAAC,KAAK;AAAA,GACR;AACA,EAAA,MAAM,KAAA,GAAQ,WAAA,CAAY,CAAC,IAAA,KAAkB,KAAA,CAAM,MAAM,IAAI,CAAA,EAAG,CAAC,KAAK,CAAC,CAAA;AACvE,EAAA,OAAO,EAAE,MAAA,EAAQ,GAAA,EAAK,KAAA,EAAO,QAAQ,KAAA,EAAM;AAC7C","file":"react.js","sourcesContent":["import { useCallback, useSyncExternalStore } from 'react';\nimport type { PrefPath, PrefValueAt } from './paths';\nimport type { PrefGroup } from './groups';\nimport type { PrefsStore } from './store';\n\n/** One leaf of `store`, as React state. */\nexport function usePref<S extends PrefGroup, P extends PrefPath<S>>(\n store: PrefsStore<S>,\n path: P,\n): [\n PrefValueAt<S, P>,\n (next: PrefValueAt<S, P> | ((prev: PrefValueAt<S, P>) => PrefValueAt<S, P>)) => void,\n] {\n const subscribe = useCallback((onChange: () => void) => store.subscribe(onChange), [store]);\n const getSnapshot = () => store.get(path);\n const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n const set = useCallback(\n (next: PrefValueAt<S, P> | ((prev: PrefValueAt<S, P>) => PrefValueAt<S, P>)) => {\n store.set(\n path,\n typeof next === 'function'\n ? (next as (prev: PrefValueAt<S, P>) => PrefValueAt<S, P>)(store.get(path))\n : next,\n );\n },\n [store, path],\n );\n return [value, set];\n}\n\n/** The whole of `store` as React state, shaped for `PrefsForm`: `values`,\n * `onChange={set}`, `auto={unset}`, and `reset` for `onAutoChange`. `stored`\n * is `store.stored()`. */\nexport function usePrefsValues<S extends PrefGroup>(store: PrefsStore<S>): {\n values: Record<string, unknown>;\n set: (path: string, value: unknown) => void;\n unset: ReadonlySet<string>;\n stored: Record<string, unknown>;\n reset: (path?: string) => void;\n} {\n const subscribe = useCallback((onChange: () => void) => store.subscribe(onChange), [store]);\n const values = useSyncExternalStore(subscribe, store.values, store.values);\n const unset = useSyncExternalStore(subscribe, store.unset, store.unset);\n const stored = useSyncExternalStore(subscribe, store.stored, store.stored);\n const set = useCallback(\n (path: string, value: unknown) => store.set(path as PrefPath<S>, value as never),\n [store],\n );\n const reset = useCallback((path?: string) => store.reset(path), [store]);\n return { values, set, unset, stored, reset };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/hooks.ts","../src/usePrefsStore.ts"],"names":["useCallback","useSyncExternalStore"],"mappings":";;;AAOO,SAAS,OAAA,CACd,OACA,IAAA,EAIA;AACA,EAAA,MAAM,SAAA,GAAY,WAAA,CAAY,CAAC,QAAA,KAAyB,KAAA,CAAM,UAAU,QAAQ,CAAA,EAAG,CAAC,KAAK,CAAC,CAAA;AAC1F,EAAA,MAAM,WAAA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AA,MAAM,OAAA,GAAU,MAAM,OAAA,CAAQ,MAAA,EAAQ,OAAO,CAAA;AAC7C,EAAA,OAAOC,oBAAAA,CAAqB,IAAA,EAAM,OAAA,EAAS,OAAO,CAAA;AACpD","file":"react.js","sourcesContent":["import { useCallback, useSyncExternalStore } from 'react';\nimport type { PrefPath, PrefValueAt } from './paths';\nimport type { PrefGroup } from './groups';\nimport type { PrefLocus, PrefSelection } from './locus';\nimport type { PrefsStore } from './store';\n\n/** One leaf of `store`, as React state. */\nexport function usePref<S extends PrefGroup, P extends PrefPath<S>>(\n store: PrefsStore<S>,\n path: P,\n): [\n PrefValueAt<S, P>,\n (next: PrefValueAt<S, P> | ((prev: PrefValueAt<S, P>) => PrefValueAt<S, P>)) => void,\n] {\n const subscribe = useCallback((onChange: () => void) => store.subscribe(onChange), [store]);\n const getSnapshot = () => store.get(path);\n const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n const set = useCallback(\n (next: PrefValueAt<S, P> | ((prev: PrefValueAt<S, P>) => PrefValueAt<S, P>)) => {\n store.set(\n path,\n typeof next === 'function'\n ? (next as (prev: PrefValueAt<S, P>) => PrefValueAt<S, P>)(store.get(path))\n : next,\n );\n },\n [store, path],\n );\n return [value, set];\n}\n\n/** The whole of `store` as React state, shaped for `PrefsForm`: `values`,\n * `onChange={set}`, `auto={unset}`, and `reset` for `onAutoChange`; for its\n * split values, `loci`, `onSplit={split}`, `onJoin={join}`, and\n * `onChangeAt={setAt}`. `stored` is `store.stored()`. */\nexport function usePrefsValues<S extends PrefGroup>(store: PrefsStore<S>): {\n values: Record<string, unknown>;\n set: (path: string, value: unknown) => void;\n unset: ReadonlySet<string>;\n stored: Record<string, unknown>;\n reset: (path?: string) => void;\n loci: ReadonlyMap<string, PrefLocus>;\n split: (path: string, by: string) => void;\n join: (path: string, by: string) => void;\n setAt: (path: string, value: unknown, at: PrefSelection) => void;\n} {\n const subscribe = useCallback((onChange: () => void) => store.subscribe(onChange), [store]);\n const values = useSyncExternalStore(subscribe, store.values, store.values);\n const unset = useSyncExternalStore(subscribe, store.unset, store.unset);\n const stored = useSyncExternalStore(subscribe, store.stored, store.stored);\n const loci = useSyncExternalStore(subscribe, store.loci, store.loci);\n const set = useCallback(\n (path: string, value: unknown) => store.set(path as PrefPath<S>, value as never),\n [store],\n );\n const reset = useCallback((path?: string) => store.reset(path), [store]);\n const split = useCallback((path: string, by: string) => store.split(path as PrefPath<S>, by), [store]);\n const join = useCallback((path: string, by: string) => store.join(path as PrefPath<S>, by), [store]);\n const setAt = useCallback(\n (path: string, value: unknown, at: PrefSelection) => store.set(path as PrefPath<S>, value as never, { at }),\n [store],\n );\n return { values, set, unset, stored, reset, loci, split, join, setAt };\n}\n","import type { OwnedRecordCache, StorageAdapter, SyncStorageAdapter } from '@weasel-js/storage';\nimport { useCallback, useSyncExternalStore } from 'react';\nimport type { PrefGroup } from './groups';\nimport { openPrefsCacheSync, type PrefsOptions } from './open';\nimport { createPrefsStore, type PrefsStore } from './store';\n\n/** The store open for one schema, and how many mounted components hold it. */\ninterface Held {\n store: PrefsStore<PrefGroup> | null;\n holders: number;\n}\n\n/** The one cache open on a prefix, and a store over it for each schema in use. */\ninterface Shared {\n open: { cache: OwnedRecordCache; stop: () => void } | null;\n bySchema: Map<PrefGroup, Held>;\n}\n\ntype SyncOptions = PrefsOptions & { storage: SyncStorageAdapter };\n\nconst shared = new WeakMap<StorageAdapter, Map<string, Shared>>();\n\nfunction sharedAt(storage: StorageAdapter, prefix: string): Shared {\n let byPrefix = shared.get(storage);\n if (!byPrefix) shared.set(storage, (byPrefix = new Map()));\n let entry = byPrefix.get(prefix);\n if (!entry) byPrefix.set(prefix, (entry = { open: null, bySchema: new Map() }));\n return entry;\n}\n\nfunction heldAt(at: Shared, schema: PrefGroup): Held {\n let entry = at.bySchema.get(schema);\n if (!entry) at.bySchema.set(schema, (entry = { store: null, holders: 0 }));\n return entry;\n}\n\n/** `cache` for one store among several: closing it sends queued writes and\n * leaves the cache open for the others. */\nconst lent = (cache: OwnedRecordCache): OwnedRecordCache =>\n Object.create(cache, {\n close: {\n value: async (): Promise<void> => {\n await cache.flush();\n },\n },\n }) as OwnedRecordCache;\n\n/** Close a store nothing holds, and the cache once no store is over it, a\n * microtask on: after the remount StrictMode and fast refresh make in the\n * same tick, which holds it again. */\nfunction closeUnheldSoon(storage: StorageAdapter, prefix: string, schema: PrefGroup, entry: Held): void {\n queueMicrotask(() => {\n if (entry.holders > 0) return;\n void entry.store?.close();\n entry.store = null;\n const byPrefix = shared.get(storage);\n const at = byPrefix?.get(prefix);\n if (!at || !byPrefix) return;\n if (at.bySchema.get(schema) === entry) at.bySchema.delete(schema);\n if (at.bySchema.size > 0) return;\n at.open?.stop();\n // `close` sends queued writes before its first await, and a sync adapter's\n // write lands in that call, so a cache opened next reads them.\n void at.open?.cache.close();\n byPrefix.delete(prefix);\n });\n}\n\nfunction acquire<S extends PrefGroup>(schema: S, options: SyncOptions): PrefsStore<S> {\n const { storage, prefix } = options;\n const at = sharedAt(storage, prefix);\n const entry = heldAt(at, schema);\n if (!entry.store) {\n at.open ??= openPrefsCacheSync(options);\n entry.store = createPrefsStore(\n schema,\n lent(at.open.cache),\n options.validators,\n undefined,\n options.migrations?.length ?? 0,\n options.blends,\n ) as unknown as PrefsStore<PrefGroup>;\n // Opened by a render that may never mount.\n if (entry.holders === 0) closeUnheldSoon(storage, prefix, schema, entry);\n }\n return entry.store as unknown as PrefsStore<S>;\n}\n\n/**\n * A store for `schema`, open while the component is mounted: opened during\n * the first render, so that render has values, and closed, with its queued\n * writes sent, when the component unmounts.\n *\n * Components naming the same `storage`, `prefix`, and `schema` share one\n * store, which closes when the last of them unmounts. Components naming the\n * same `storage` and `prefix` with different schemas each get a store of\n * their own over the same records, so a write through one is read by the\n * others at once, and each hears it as a `local` change. They must pass the\n * same `migrations`: the records are migrated once, when the first opens.\n *\n * `storage` and `schema` are compared by identity and `prefix` by value, so\n * keep the first two out of the render body. A change to any of the three\n * closes the store and returns the one for the new three. The `options`\n * object itself may be new every render: `migrations` and `validators` are\n * read when a store opens, and a change to either alone opens nothing.\n */\nexport function usePrefsStore<S extends PrefGroup>(schema: S, options: SyncOptions): PrefsStore<S> {\n const { storage, prefix } = options;\n const hold = useCallback(() => {\n const entry = heldAt(sharedAt(storage, prefix), schema);\n entry.holders++;\n return () => {\n if (--entry.holders === 0) closeUnheldSoon(storage, prefix, schema, entry);\n };\n }, [storage, prefix, schema]);\n // Read again once held, so a store closed between render and mount is\n // replaced before anything uses it.\n const current = () => acquire(schema, options);\n return useSyncExternalStore(hold, current, current);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weasel-js/prefs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.1",
|
|
4
4
|
"description": "A preferences schema, a store that loads, validates, migrates and persists values against it, and React hooks over the store.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -41,9 +41,10 @@
|
|
|
41
41
|
"build": "tsup"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@weasel-js/
|
|
45
|
-
"@weasel-js/
|
|
46
|
-
"@weasel-js/
|
|
44
|
+
"@weasel-js/paint": "1.10.1",
|
|
45
|
+
"@weasel-js/quantity": "1.10.1",
|
|
46
|
+
"@weasel-js/registry": "1.10.1",
|
|
47
|
+
"@weasel-js/storage": "1.10.1"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
49
50
|
"react": ">=18"
|