cross-state 0.53.3 → 0.54.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/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/diff-BQ8bB3Wk.d.cts +22 -0
- package/dist/diff-gZezL04N.d.ts +22 -0
- package/dist/extendedJson-BZkQBXEv.js +29 -0
- package/dist/extendedJson-BZkQBXEv.js.map +1 -0
- package/dist/extendedJson-Dn2F7Edo.cjs +53 -0
- package/dist/extendedJson-Dn2F7Edo.cjs.map +1 -0
- package/dist/hash-BV06P7va.js +19 -0
- package/dist/hash-BV06P7va.js.map +1 -0
- package/dist/hash-CJPl7SRj.cjs +30 -0
- package/dist/hash-CJPl7SRj.cjs.map +1 -0
- package/dist/index.cjs +49 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +59 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/mutative/index.cjs +3 -0
- package/dist/mutative/index.d.cts +3 -0
- package/dist/mutative/index.d.ts +3 -0
- package/dist/mutative/index.js +3 -0
- package/dist/mutative/register.cjs +10 -0
- package/dist/mutative/register.cjs.map +1 -0
- package/dist/mutative/register.d.cts +11 -0
- package/dist/mutative/register.d.ts +9 -0
- package/dist/mutative/register.js +10 -0
- package/dist/mutative/register.js.map +1 -0
- package/dist/mutativeMethods-6QzygG3W.d.cts +13 -0
- package/dist/mutativeMethods-Br3xN2Iq.d.ts +13 -0
- package/dist/mutativeMethods-CZKeRIDL.js +27 -0
- package/dist/mutativeMethods-CZKeRIDL.js.map +1 -0
- package/dist/mutativeMethods-CgXk_bMt.cjs +33 -0
- package/dist/mutativeMethods-CgXk_bMt.cjs.map +1 -0
- package/dist/patchMethods-C3L7Kh8T.d.cts +48 -0
- package/dist/patchMethods-D5YJwcpn.cjs +264 -0
- package/dist/patchMethods-D5YJwcpn.cjs.map +1 -0
- package/dist/patchMethods-DRPfkhAe.js +241 -0
- package/dist/patchMethods-DRPfkhAe.js.map +1 -0
- package/dist/patchMethods-Dxad6HF6.d.ts +48 -0
- package/dist/patches/index.cjs +5 -0
- package/dist/patches/index.d.cts +4 -0
- package/dist/patches/index.d.ts +4 -0
- package/dist/patches/index.js +5 -0
- package/dist/patches/register.cjs +11 -0
- package/dist/patches/register.cjs.map +1 -0
- package/dist/patches/register.d.cts +12 -0
- package/dist/patches/register.d.ts +10 -0
- package/dist/patches/register.js +11 -0
- package/dist/patches/register.js.map +1 -0
- package/dist/persist/register.cjs +13 -0
- package/dist/persist/register.cjs.map +1 -0
- package/dist/persist/register.d.cts +15 -0
- package/dist/persist/register.d.ts +13 -0
- package/dist/persist/register.js +13 -0
- package/dist/persist/register.js.map +1 -0
- package/dist/persist-BluRVcW6.js +258 -0
- package/dist/persist-BluRVcW6.js.map +1 -0
- package/dist/persist-CPjpg6D0.d.ts +61 -0
- package/dist/persist-CztClydq.cjs +263 -0
- package/dist/persist-CztClydq.cjs.map +1 -0
- package/dist/persist-D7MAsyyW.d.cts +61 -0
- package/dist/propAccess-ByqBjC19.cjs +173 -0
- package/dist/propAccess-ByqBjC19.cjs.map +1 -0
- package/dist/propAccess-DGchzVfy.js +113 -0
- package/dist/propAccess-DGchzVfy.js.map +1 -0
- package/dist/react/index.cjs +622 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +306 -0
- package/dist/react/index.d.ts +306 -0
- package/dist/react/index.js +602 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/register.cjs +16 -0
- package/dist/react/register.cjs.map +1 -0
- package/dist/react/register.d.cts +16 -0
- package/dist/react/register.d.ts +14 -0
- package/dist/react/register.js +16 -0
- package/dist/react/register.js.map +1 -0
- package/dist/scope-DI1OFHcr.d.ts +167 -0
- package/dist/scope-DO5vpLPV.d.cts +167 -0
- package/dist/scope-DU5dFP1T.js +322 -0
- package/dist/scope-DU5dFP1T.js.map +1 -0
- package/dist/scope-DvqvkIHB.cjs +369 -0
- package/dist/scope-DvqvkIHB.cjs.map +1 -0
- package/dist/store-BEsiS8y7.d.cts +242 -0
- package/dist/store-BWMWEUpu.cjs +819 -0
- package/dist/store-BWMWEUpu.cjs.map +1 -0
- package/dist/store-DKaeE840.d.ts +242 -0
- package/dist/store-Dr8gM3lq.js +730 -0
- package/dist/store-Dr8gM3lq.js.map +1 -0
- package/dist/storeMethods-AIJSwxHz.js +315 -0
- package/dist/storeMethods-AIJSwxHz.js.map +1 -0
- package/dist/storeMethods-BQwU2Uia.d.cts +107 -0
- package/dist/storeMethods-C4luym7C.d.ts +107 -0
- package/dist/storeMethods-DVVsmUg1.cjs +375 -0
- package/dist/storeMethods-DVVsmUg1.cjs.map +1 -0
- package/package.json +92 -135
- package/dist/cjs/hash.cjs +0 -30
- package/dist/cjs/hash.cjs.map +0 -1
- package/dist/cjs/index.cjs +0 -56
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index2.cjs +0 -300
- package/dist/cjs/index2.cjs.map +0 -1
- package/dist/cjs/mutative/index.cjs +0 -10
- package/dist/cjs/mutative/index.cjs.map +0 -1
- package/dist/cjs/mutative/register.cjs +0 -8
- package/dist/cjs/mutative/register.cjs.map +0 -1
- package/dist/cjs/mutativeMethods.cjs +0 -29
- package/dist/cjs/mutativeMethods.cjs.map +0 -1
- package/dist/cjs/patches/index.cjs +0 -10
- package/dist/cjs/patches/index.cjs.map +0 -1
- package/dist/cjs/patches/register.cjs +0 -8
- package/dist/cjs/patches/register.cjs.map +0 -1
- package/dist/cjs/persist/register.cjs +0 -11
- package/dist/cjs/persist/register.cjs.map +0 -1
- package/dist/cjs/persist.cjs +0 -302
- package/dist/cjs/persist.cjs.map +0 -1
- package/dist/cjs/propAccess.cjs +0 -162
- package/dist/cjs/propAccess.cjs.map +0 -1
- package/dist/cjs/react/index.cjs +0 -628
- package/dist/cjs/react/index.cjs.map +0 -1
- package/dist/cjs/react/register.cjs +0 -13
- package/dist/cjs/react/register.cjs.map +0 -1
- package/dist/cjs/scope.cjs +0 -400
- package/dist/cjs/scope.cjs.map +0 -1
- package/dist/cjs/store.cjs +0 -829
- package/dist/cjs/store.cjs.map +0 -1
- package/dist/cjs/storeMethods.cjs +0 -856
- package/dist/cjs/storeMethods.cjs.map +0 -1
- package/dist/cjs/urlStore.cjs +0 -120
- package/dist/cjs/urlStore.cjs.map +0 -1
- package/dist/es/hash.mjs +0 -27
- package/dist/es/hash.mjs.map +0 -1
- package/dist/es/index.mjs +0 -20
- package/dist/es/index.mjs.map +0 -1
- package/dist/es/index2.mjs +0 -291
- package/dist/es/index2.mjs.map +0 -1
- package/dist/es/mutative/index.mjs +0 -2
- package/dist/es/mutative/index.mjs.map +0 -1
- package/dist/es/mutative/register.mjs +0 -6
- package/dist/es/mutative/register.mjs.map +0 -1
- package/dist/es/mutativeMethods.mjs +0 -27
- package/dist/es/mutativeMethods.mjs.map +0 -1
- package/dist/es/patches/index.mjs +0 -2
- package/dist/es/patches/index.mjs.map +0 -1
- package/dist/es/patches/register.mjs +0 -6
- package/dist/es/patches/register.mjs.map +0 -1
- package/dist/es/persist/register.mjs +0 -9
- package/dist/es/persist/register.mjs.map +0 -1
- package/dist/es/persist.mjs +0 -300
- package/dist/es/persist.mjs.map +0 -1
- package/dist/es/propAccess.mjs +0 -151
- package/dist/es/propAccess.mjs.map +0 -1
- package/dist/es/react/index.mjs +0 -611
- package/dist/es/react/index.mjs.map +0 -1
- package/dist/es/react/register.mjs +0 -11
- package/dist/es/react/register.mjs.map +0 -1
- package/dist/es/scope.mjs +0 -391
- package/dist/es/scope.mjs.map +0 -1
- package/dist/es/store.mjs +0 -812
- package/dist/es/store.mjs.map +0 -1
- package/dist/es/storeMethods.mjs +0 -845
- package/dist/es/storeMethods.mjs.map +0 -1
- package/dist/es/urlStore.mjs +0 -116
- package/dist/es/urlStore.mjs.map +0 -1
- package/dist/types/core/cache.d.ts +0 -119
- package/dist/types/core/commonTypes.d.ts +0 -76
- package/dist/types/core/index.d.ts +0 -6
- package/dist/types/core/resourceGroup.d.ts +0 -16
- package/dist/types/core/scope.d.ts +0 -5
- package/dist/types/core/store.d.ts +0 -86
- package/dist/types/core/urlStore.d.ts +0 -21
- package/dist/types/index.d.ts +0 -14
- package/dist/types/lib/applyPatches.d.ts +0 -2
- package/dist/types/lib/autobind.d.ts +0 -1
- package/dist/types/lib/cacheState.d.ts +0 -20
- package/dist/types/lib/calcDuration.d.ts +0 -2
- package/dist/types/lib/calculatedValue.d.ts +0 -9
- package/dist/types/lib/castArray.d.ts +0 -1
- package/dist/types/lib/clone.d.ts +0 -1
- package/dist/types/lib/constrain.d.ts +0 -1
- package/dist/types/lib/debounce.d.ts +0 -12
- package/dist/types/lib/deferred.d.ts +0 -5
- package/dist/types/lib/diff.d.ts +0 -17
- package/dist/types/lib/disposable.d.ts +0 -3
- package/dist/types/lib/equals.d.ts +0 -3
- package/dist/types/lib/extendedJson.d.ts +0 -5
- package/dist/types/lib/forwardError.d.ts +0 -1
- package/dist/types/lib/hash.d.ts +0 -5
- package/dist/types/lib/helpers.d.ts +0 -2
- package/dist/types/lib/instanceCache.d.ts +0 -18
- package/dist/types/lib/isPromise.d.ts +0 -1
- package/dist/types/lib/makeSelector.d.ts +0 -2
- package/dist/types/lib/maybePromise.d.ts +0 -1
- package/dist/types/lib/path.d.ts +0 -41
- package/dist/types/lib/promiseChain.d.ts +0 -5
- package/dist/types/lib/promiseWithCancel.d.ts +0 -8
- package/dist/types/lib/promiseWithState.d.ts +0 -9
- package/dist/types/lib/propAccess.d.ts +0 -7
- package/dist/types/lib/queue.d.ts +0 -11
- package/dist/types/lib/standardMethods.d.ts +0 -20
- package/dist/types/lib/throttle.d.ts +0 -2
- package/dist/types/lib/trackingProxy.d.ts +0 -2
- package/dist/types/lib/trie.d.ts +0 -11
- package/dist/types/lib/typeHelpers.d.ts +0 -12
- package/dist/types/lib/unwrapPromise.d.ts +0 -1
- package/dist/types/lib/updateHelpers.d.ts +0 -1
- package/dist/types/lib/wildcardMatch.d.ts +0 -3
- package/dist/types/mutative/index.d.ts +0 -1
- package/dist/types/mutative/mutativeMethods.d.ts +0 -11
- package/dist/types/mutative/register.d.ts +0 -7
- package/dist/types/patches/index.d.ts +0 -1
- package/dist/types/patches/patchMethods.d.ts +0 -44
- package/dist/types/patches/register.d.ts +0 -7
- package/dist/types/persist/index.d.ts +0 -3
- package/dist/types/persist/persist.d.ts +0 -40
- package/dist/types/persist/persistPathHelpers.d.ts +0 -6
- package/dist/types/persist/persistStorage.d.ts +0 -17
- package/dist/types/persist/register.d.ts +0 -9
- package/dist/types/react/cacheMethods.d.ts +0 -5
- package/dist/types/react/form/customInput.d.ts +0 -6
- package/dist/types/react/form/form.d.ts +0 -98
- package/dist/types/react/form/formField.d.ts +0 -68
- package/dist/types/react/form/formForEach.d.ts +0 -19
- package/dist/types/react/form/index.d.ts +0 -3
- package/dist/types/react/form/useFormAutosave.d.ts +0 -9
- package/dist/types/react/index.d.ts +0 -11
- package/dist/types/react/loadingBoundary.d.ts +0 -21
- package/dist/types/react/register.d.ts +0 -15
- package/dist/types/react/scope.d.ts +0 -24
- package/dist/types/react/scopeMethods.d.ts +0 -20
- package/dist/types/react/storeMethods.d.ts +0 -15
- package/dist/types/react/useCache.d.ts +0 -34
- package/dist/types/react/useDecoupledState.d.ts +0 -7
- package/dist/types/react/useProp.d.ts +0 -8
- package/dist/types/react/useStore.d.ts +0 -16
- package/dist/types/react/useUrlParamScope.d.ts +0 -4
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
const require_store = require('./store-BWMWEUpu.cjs');
|
|
2
|
+
const require_propAccess = require('./propAccess-ByqBjC19.cjs');
|
|
3
|
+
const require_patchMethods = require('./patchMethods-D5YJwcpn.cjs');
|
|
4
|
+
const require_extendedJson = require('./extendedJson-Dn2F7Edo.cjs');
|
|
5
|
+
|
|
6
|
+
//#region src/lib/promiseChain.ts
|
|
7
|
+
function promiseChain(value) {
|
|
8
|
+
if (value instanceof Function) value = value();
|
|
9
|
+
return {
|
|
10
|
+
value,
|
|
11
|
+
next(fn) {
|
|
12
|
+
const next = require_store.isPromise(value) ? value.then((value$1) => fn(value$1)) : fn(value);
|
|
13
|
+
return promiseChain(next);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/persist/persistPathHelpers.ts
|
|
20
|
+
const isAncestor = (ancestor, path) => {
|
|
21
|
+
return ancestor.length <= path.length && ancestor.every((v, i) => v === "*" || path[i] === "*" || v === path[i]);
|
|
22
|
+
};
|
|
23
|
+
const split = (value, path) => {
|
|
24
|
+
const [first, ...rest] = path;
|
|
25
|
+
if (first === void 0) return [{
|
|
26
|
+
path: [],
|
|
27
|
+
value
|
|
28
|
+
}];
|
|
29
|
+
let entries;
|
|
30
|
+
if (value instanceof Map) entries = value;
|
|
31
|
+
else if (value instanceof Set) entries = new Map([...value].map((v, i) => [i, v]));
|
|
32
|
+
else if (Array.isArray(value)) entries = new Map(value.map((v, i) => [i, v]));
|
|
33
|
+
else if (typeof value === "object" && value !== null) entries = new Map(Object.entries(value));
|
|
34
|
+
else return [{
|
|
35
|
+
path: [],
|
|
36
|
+
value
|
|
37
|
+
}];
|
|
38
|
+
if (first === "*") return [...entries].flatMap(([k, v]) => split(v, rest).map(({ path: path$1, value: value$1 }) => ({
|
|
39
|
+
path: [k, ...path$1],
|
|
40
|
+
value: value$1
|
|
41
|
+
})));
|
|
42
|
+
const subValue = entries.get(first);
|
|
43
|
+
if (subValue === void 0) return [{
|
|
44
|
+
path: [],
|
|
45
|
+
value
|
|
46
|
+
}];
|
|
47
|
+
return split(subValue, rest).map(({ path: path$1, value: value$1 }) => ({
|
|
48
|
+
path: [first, ...path$1],
|
|
49
|
+
value: value$1
|
|
50
|
+
}));
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/persist/persistStorage.ts
|
|
55
|
+
function normalizeStorage(storage) {
|
|
56
|
+
return {
|
|
57
|
+
getItem: storage.getItem.bind(storage),
|
|
58
|
+
setItem: storage.setItem.bind(storage),
|
|
59
|
+
removeItem: storage.removeItem.bind(storage),
|
|
60
|
+
listItems() {
|
|
61
|
+
if ("listItems" in storage) return storage.listItems();
|
|
62
|
+
return promiseChain(() => {
|
|
63
|
+
if ("keys" in storage) return storage.keys();
|
|
64
|
+
else return promiseChain(storage.length instanceof Function ? storage.length() : storage.length).next((length) => {
|
|
65
|
+
const keys = Array.from({ length }, (_, index) => storage.key(index));
|
|
66
|
+
return keys.some(require_store.isPromise) ? Promise.all(keys) : keys;
|
|
67
|
+
}).next((keys) => {
|
|
68
|
+
return keys.filter((key) => typeof key === "string");
|
|
69
|
+
}).value;
|
|
70
|
+
}).next((keys) => {
|
|
71
|
+
const results = keys.map((key) => promiseChain(storage.getItem(key)).next((value) => [key, value]).value);
|
|
72
|
+
return results.some(require_store.isPromise) ? Promise.all(results) : results;
|
|
73
|
+
}).next((results) => {
|
|
74
|
+
return new Map(results.filter(([, value]) => value !== null));
|
|
75
|
+
}).value;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/persist/persist.ts
|
|
82
|
+
var Persist = class {
|
|
83
|
+
static {
|
|
84
|
+
Symbol.dispose;
|
|
85
|
+
}
|
|
86
|
+
constructor(store, options) {
|
|
87
|
+
this.store = store;
|
|
88
|
+
this.options = options;
|
|
89
|
+
this.queue = require_store.queue();
|
|
90
|
+
this.handles = /* @__PURE__ */ new Set();
|
|
91
|
+
this.stopped = false;
|
|
92
|
+
this.updateInProgress = /* @__PURE__ */ new Map();
|
|
93
|
+
this.storage = normalizeStorage(options.storage);
|
|
94
|
+
this.channel = new BroadcastChannel(`cross-state-persist_${options.id}`);
|
|
95
|
+
this.prefix = `${options.id}:`;
|
|
96
|
+
if (Symbol.dispose) this[Symbol.dispose] = () => this.stop();
|
|
97
|
+
this.paths = (options.paths ?? []).map((p) => {
|
|
98
|
+
if (isPlainPath(p)) return {
|
|
99
|
+
path: require_propAccess.castArrayPath(p),
|
|
100
|
+
throttle: options.throttle && require_store.calcDuration(options.throttle)
|
|
101
|
+
};
|
|
102
|
+
const _p = p;
|
|
103
|
+
return {
|
|
104
|
+
path: require_propAccess.castArrayPath(_p.path),
|
|
105
|
+
throttle: (_p.throttle && require_store.calcDuration(_p.throttle)) ?? (options.throttle && require_store.calcDuration(options.throttle))
|
|
106
|
+
};
|
|
107
|
+
}).sort((a, b) => b.path.length - a.path.length);
|
|
108
|
+
if (this.paths.length === 0) this.paths.push({
|
|
109
|
+
path: ["*"],
|
|
110
|
+
throttle: options.throttle && require_store.calcDuration(options.throttle)
|
|
111
|
+
});
|
|
112
|
+
this.initialized = new Promise((resolve) => {
|
|
113
|
+
this.resolveInitialized = resolve;
|
|
114
|
+
});
|
|
115
|
+
this.watchStore();
|
|
116
|
+
this.watchStorage();
|
|
117
|
+
}
|
|
118
|
+
watchStore() {
|
|
119
|
+
const throttle = Math.min(...this.paths.map((p) => p.throttle ?? 0)) || void 0;
|
|
120
|
+
const cancel = require_patchMethods.subscribePatches.apply(this.store, [(patches, reversePatches) => {
|
|
121
|
+
let i = 0;
|
|
122
|
+
for (const patch of patches) {
|
|
123
|
+
const reversePatch = reversePatches[i++];
|
|
124
|
+
const stringPath = JSON.stringify(patch.path);
|
|
125
|
+
if (this.updateInProgress.has(stringPath) && this.updateInProgress.get(stringPath) === (patch.op === "remove" ? void 0 : patch.value)) continue;
|
|
126
|
+
const matchingPaths = this.paths.filter((p) => isAncestor(p.path, patch.path) || isAncestor(patch.path, p.path));
|
|
127
|
+
for (const { path } of matchingPaths) if (path.length <= patch.path.length) {
|
|
128
|
+
const pathToSave = patch.path.slice(0, path.length);
|
|
129
|
+
this.queue(() => this.save(pathToSave), pathToSave);
|
|
130
|
+
} else if (patch.op === "remove") {
|
|
131
|
+
const subValues = split(reversePatch?.op === "add" ? reversePatch.value : {}, path.slice(patch.path.length));
|
|
132
|
+
for (const { path: path$1 } of subValues) this.queue(() => this.save([...patch.path, ...path$1]), [...patch.path, ...path$1]);
|
|
133
|
+
} else {
|
|
134
|
+
const updatedValues = split(patch.value, path.slice(patch.path.length));
|
|
135
|
+
const removedValues = split(reversePatch?.op !== "remove" ? reversePatch?.value ?? {} : {}, path.slice(patch.path.length)).filter((v) => !updatedValues.some((u) => require_propAccess.shallowEqual(u.path, v.path)));
|
|
136
|
+
for (const { path: path$1 } of updatedValues) this.queue(() => this.save([...patch.path, ...path$1]), [...patch.path, ...path$1]);
|
|
137
|
+
for (const { path: path$1 } of removedValues) this.queue(() => this.save([...patch.path, ...path$1]), [...patch.path, ...path$1]);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}, {
|
|
141
|
+
runNow: this.options.persistInitialState ?? false,
|
|
142
|
+
passive: true,
|
|
143
|
+
throttle
|
|
144
|
+
}]);
|
|
145
|
+
this.handles.add(cancel);
|
|
146
|
+
}
|
|
147
|
+
async watchStorage() {
|
|
148
|
+
if (!this.options.persistInitialState) {
|
|
149
|
+
let items = this.storage.listItems();
|
|
150
|
+
if (require_store.isPromise(items)) items = await items;
|
|
151
|
+
if (this.stopped) return;
|
|
152
|
+
const toLoad = new Map([...items.entries()].sort((a, b) => b[1].length - a[1].length).map(([key, value]) => [this.parseKey(key), value]).filter(([key]) => key));
|
|
153
|
+
this.queue(() => this.load(toLoad));
|
|
154
|
+
}
|
|
155
|
+
this.queue(() => this.resolveInitialized?.());
|
|
156
|
+
const listener = (event) => {
|
|
157
|
+
this.queue(() => this.load([{
|
|
158
|
+
type: "data",
|
|
159
|
+
path: event.data
|
|
160
|
+
}]));
|
|
161
|
+
};
|
|
162
|
+
this.channel.addEventListener("message", listener);
|
|
163
|
+
this.handles.add(() => this.channel.removeEventListener("message", listener));
|
|
164
|
+
}
|
|
165
|
+
buildKey({ type, path }) {
|
|
166
|
+
return `${this.prefix}${type === "internal" ? path : JSON.stringify(path)}`;
|
|
167
|
+
}
|
|
168
|
+
parseKey(key) {
|
|
169
|
+
if (!key.startsWith(this.prefix)) return;
|
|
170
|
+
key = key.slice(this.prefix.length);
|
|
171
|
+
if (!key.startsWith("[")) return {
|
|
172
|
+
type: "internal",
|
|
173
|
+
path: key
|
|
174
|
+
};
|
|
175
|
+
return {
|
|
176
|
+
type: "data",
|
|
177
|
+
path: JSON.parse(key)
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
load(items) {
|
|
181
|
+
return promiseChain(() => {
|
|
182
|
+
if (Array.isArray(items)) return promiseChain(() => {
|
|
183
|
+
const entries = items.map((key) => promiseChain(() => {
|
|
184
|
+
return this.storage.getItem(this.buildKey(key));
|
|
185
|
+
}).next((value) => [key, value]).value);
|
|
186
|
+
return entries.some(require_store.isPromise) ? Promise.all(entries) : entries;
|
|
187
|
+
}).next((entries) => {
|
|
188
|
+
return entries.filter((entry) => entry !== null);
|
|
189
|
+
}).value;
|
|
190
|
+
else return [...items.entries()];
|
|
191
|
+
}).next((entries) => {
|
|
192
|
+
if (this.stopped) return;
|
|
193
|
+
const toWrite = entries.filter(([key, value]) => {
|
|
194
|
+
if (key.type !== "data" || !value) return;
|
|
195
|
+
if (!this.paths.find((p) => p.path.length === 1 && p.path[0] === "*" && key.path.length === 0 || p.path.length === key.path.length && isAncestor(p.path, key.path))) return null;
|
|
196
|
+
const inSaveQueue = this.queue.getRefs().find((ref) => isAncestor(ref, key.path) || isAncestor(key.path, ref));
|
|
197
|
+
return !inSaveQueue;
|
|
198
|
+
}).map(([key, value]) => {
|
|
199
|
+
try {
|
|
200
|
+
return {
|
|
201
|
+
path: key.path,
|
|
202
|
+
value: !value || value === "undefined" ? void 0 : require_extendedJson.fromExtendedJsonString(value)
|
|
203
|
+
};
|
|
204
|
+
} catch {
|
|
205
|
+
return void 0;
|
|
206
|
+
}
|
|
207
|
+
}).filter(Boolean);
|
|
208
|
+
if (toWrite.length > 0) {
|
|
209
|
+
for (const { path, value } of toWrite) this.updateInProgress.set(JSON.stringify(path), value);
|
|
210
|
+
this.store.set((state) => {
|
|
211
|
+
for (const { path, value } of toWrite) if (value === void 0) state = require_propAccess.remove(state, path);
|
|
212
|
+
else state = require_propAccess.set(state, path, value);
|
|
213
|
+
return state;
|
|
214
|
+
});
|
|
215
|
+
this.updateInProgress.clear();
|
|
216
|
+
}
|
|
217
|
+
const versionEntry = entries.find(([key]) => key.type === "internal" && key.path === "version");
|
|
218
|
+
if (versionEntry) this.store.version = versionEntry[1];
|
|
219
|
+
}).value;
|
|
220
|
+
}
|
|
221
|
+
save(path) {
|
|
222
|
+
const key = this.buildKey({
|
|
223
|
+
type: "data",
|
|
224
|
+
path
|
|
225
|
+
});
|
|
226
|
+
const value = require_propAccess.get(this.store.get(), path);
|
|
227
|
+
return promiseChain(value).next((value$1) => {
|
|
228
|
+
if (value$1 === void 0) return this.storage.removeItem(key);
|
|
229
|
+
else return this.storage.setItem(key, require_extendedJson.toExtendedJsonString(value$1));
|
|
230
|
+
}).next(() => {
|
|
231
|
+
this.channel.postMessage(path);
|
|
232
|
+
if (this.store.version) return this.storage.setItem(this.buildKey({
|
|
233
|
+
type: "internal",
|
|
234
|
+
path: "version"
|
|
235
|
+
}), this.store.version);
|
|
236
|
+
else return this.storage.removeItem(this.buildKey({
|
|
237
|
+
type: "internal",
|
|
238
|
+
path: "version"
|
|
239
|
+
}));
|
|
240
|
+
}).value;
|
|
241
|
+
}
|
|
242
|
+
async stop() {
|
|
243
|
+
this.stopped = true;
|
|
244
|
+
for (const handle of this.handles) handle();
|
|
245
|
+
await this.queue.whenDone();
|
|
246
|
+
this.channel.close();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
function persist(store, options) {
|
|
250
|
+
return new Persist(store, options);
|
|
251
|
+
}
|
|
252
|
+
function isPlainPath(p) {
|
|
253
|
+
return typeof p === "string" || Array.isArray(p);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
Object.defineProperty(exports, 'persist', {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
get: function () {
|
|
260
|
+
return persist;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
//# sourceMappingURL=persist-CztClydq.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist-CztClydq.cjs","names":["isPromise","value","entries: Map<KeyType, unknown>","path","isPromise","store: Store<T>","options: PersistOptions<T>","queue","castArrayPath","calcDuration","subscribePatches","path","shallowEqual","isPromise","fromExtendedJsonString","remove","set","get","value","toExtendedJsonString"],"sources":["../src/lib/promiseChain.ts","../src/persist/persistPathHelpers.ts","../src/persist/persistStorage.ts","../src/persist/persist.ts"],"sourcesContent":["import isPromise from '@lib/isPromise';\n\nexport interface Chain<T> {\n value: T;\n next<S>(fn: (value: Awaited<T>) => S): T extends Promise<any> ? Chain<Promise<S>> : Chain<S>;\n}\n\nexport default function promiseChain<T>(value: T | (() => T)): Chain<T> {\n if (value instanceof Function) {\n value = value();\n }\n\n return {\n value,\n next(fn) {\n const next = isPromise(value)\n ? value.then((value) => fn(value as Awaited<T>))\n : fn(value as Awaited<T>);\n\n return promiseChain(next) as any;\n },\n };\n}\n","import type { KeyType } from '@lib/path';\n\nexport const isAncestor = (ancestor: KeyType[], path: KeyType[]): boolean => {\n return (\n ancestor.length <= path.length &&\n ancestor.every((v, i) => v === '*' || path[i] === '*' || v === path[i])\n );\n};\n\nexport const split = (value: any, path: KeyType[]): { path: KeyType[]; value: unknown }[] => {\n const [first, ...rest] = path;\n if (first === undefined) return [{ path: [], value }];\n\n let entries: Map<KeyType, unknown>;\n if (value instanceof Map) {\n entries = value;\n } else if (value instanceof Set) {\n entries = new Map([...value].map((v, i) => [i, v]));\n } else if (Array.isArray(value)) {\n entries = new Map(value.map((v, i) => [i, v]));\n } else if (typeof value === 'object' && value !== null) {\n entries = new Map(Object.entries(value));\n } else {\n return [{ path: [], value }];\n }\n\n if (first === '*') {\n return [...entries].flatMap(([k, v]) =>\n split(v, rest).map(({ path, value }) => ({ path: [k, ...path], value })),\n );\n }\n\n const subValue = entries.get(first);\n if (subValue === undefined) return [{ path: [], value }];\n\n return split(subValue, rest).map(({ path, value }) => ({ path: [first, ...path], value }));\n};\n","import isPromise from '@lib/isPromise';\nimport promiseChain from '@lib/promiseChain';\n\nexport interface PersistStorageBase {\n getItem: (key: string) => string | null | Promise<string | null>;\n setItem: (key: string, value: string) => void | Promise<unknown>;\n removeItem: (key: string) => void | Promise<unknown>;\n}\n\nexport interface PersistStorageWithKeys extends PersistStorageBase {\n keys: () => string[] | Promise<string[]>;\n}\n\nexport interface PersistStorageWithLength extends PersistStorageBase {\n length: number | (() => number | Promise<number>);\n key: (keyIndex: number) => string | null | Promise<string | null>;\n}\n\nexport interface PersistStorageWithListItems extends PersistStorageBase {\n listItems: () => Map<string, string> | Promise<Map<string, string>>;\n}\n\nexport type PersistStorage =\n | PersistStorageWithKeys\n | PersistStorageWithLength\n | PersistStorageWithListItems;\n\nexport function normalizeStorage(storage: PersistStorage): PersistStorageWithListItems {\n return {\n getItem: storage.getItem.bind(storage),\n setItem: storage.setItem.bind(storage),\n removeItem: storage.removeItem.bind(storage),\n\n listItems() {\n if ('listItems' in storage) {\n return storage.listItems();\n }\n\n return promiseChain(() => {\n if ('keys' in storage) {\n return storage.keys();\n } else {\n return promiseChain(\n storage.length instanceof Function ? storage.length() : storage.length,\n )\n .next((length) => {\n const keys = Array.from({ length }, (_, index) => storage.key(index));\n return keys.some(isPromise) ? Promise.all(keys) : (keys as (string | null)[]);\n })\n .next((keys) => {\n return keys.filter((key): key is string => typeof key === 'string');\n }).value;\n }\n })\n .next((keys) => {\n const results = keys.map(\n (key) =>\n promiseChain(storage.getItem(key)).next((value) => [key, value] as const).value,\n );\n\n return results.some(isPromise)\n ? Promise.all(results)\n : (results as [string, string | null][]);\n })\n .next((results) => {\n return new Map(results.filter(([, value]) => value !== null) as [string, string][]);\n }).value;\n },\n };\n}\n","import { type Cancel, type Duration, type Store } from '@core';\nimport { calcDuration } from '@lib/calcDuration';\nimport { shallowEqual } from '@lib/equals';\nimport { fromExtendedJsonString, toExtendedJsonString } from '@lib/extendedJson';\nimport isPromise from '@lib/isPromise';\nimport type { KeyType, WildcardPath } from '@lib/path';\nimport promiseChain from '@lib/promiseChain';\nimport { castArrayPath, get, remove, set } from '@lib/propAccess';\nimport { queue } from '@lib/queue';\nimport { subscribePatches } from '@patches/patchMethods';\nimport { isAncestor, split } from '@persist/persistPathHelpers';\nimport {\n normalizeStorage,\n type PersistStorage,\n type PersistStorageWithListItems,\n} from './persistStorage';\n\ntype PathOption<T> =\n | WildcardPath<T>\n | {\n path: WildcardPath<T>;\n // throttle?: Duration;\n };\n\ntype Key = { type: 'internal'; path: string } | { type: 'data'; path: KeyType[] };\n\nexport interface PersistOptions<T> {\n id: string;\n storage: PersistStorage;\n paths?: PathOption<T>[];\n throttle?: Duration;\n persistInitialState?: boolean;\n}\n\nexport class Persist<T> {\n readonly storage: PersistStorageWithListItems;\n readonly [Symbol.dispose]!: Cancel;\n\n readonly paths: {\n path: KeyType[];\n throttle?: number;\n }[];\n\n readonly initialized: Promise<void>;\n\n private resolveInitialized?: () => void;\n\n private channel: BroadcastChannel;\n\n private queue = queue();\n\n private handles = new Set<Cancel>();\n\n private stopped = false;\n\n private updateInProgress = new Map<string, unknown>();\n\n private prefix;\n\n constructor(\n public readonly store: Store<T>,\n public readonly options: PersistOptions<T>,\n ) {\n this.storage = normalizeStorage(options.storage);\n this.channel = new BroadcastChannel(`cross-state-persist_${options.id}`);\n this.prefix = `${options.id}:`;\n\n if (Symbol.dispose) {\n this[Symbol.dispose] = () => this.stop();\n }\n\n this.paths = (options.paths ?? [])\n .map<{\n path: KeyType[];\n throttle?: number;\n }>((p) => {\n if (isPlainPath(p)) {\n return {\n path: castArrayPath(p),\n throttle: options.throttle && calcDuration(options.throttle),\n };\n }\n\n const _p = p as { path: KeyType[]; throttle?: Duration };\n\n return {\n path: castArrayPath(_p.path),\n throttle:\n (_p.throttle && calcDuration(_p.throttle)) ??\n (options.throttle && calcDuration(options.throttle)),\n };\n })\n .sort((a, b) => b.path.length - a.path.length);\n\n if (this.paths.length === 0) {\n this.paths.push({\n path: ['*'],\n throttle: options.throttle && calcDuration(options.throttle),\n });\n }\n\n this.initialized = new Promise((resolve) => {\n this.resolveInitialized = resolve;\n });\n\n this.watchStore();\n this.watchStorage();\n }\n\n private watchStore() {\n const throttle = Math.min(...this.paths.map((p) => p.throttle ?? 0)) || undefined;\n\n const cancel = subscribePatches.apply(this.store as Store<unknown>, [\n (patches, reversePatches) => {\n let i = 0;\n for (const patch of patches) {\n const reversePatch = reversePatches[i++];\n\n const stringPath = JSON.stringify(patch.path);\n if (\n this.updateInProgress.has(stringPath) &&\n this.updateInProgress.get(stringPath) ===\n (patch.op === 'remove' ? undefined : patch.value)\n ) {\n continue;\n }\n\n const matchingPaths = this.paths.filter(\n (p) => isAncestor(p.path, patch.path) || isAncestor(patch.path, p.path),\n );\n\n for (const { path } of matchingPaths) {\n if (path.length <= patch.path.length) {\n const pathToSave = patch.path.slice(0, path.length);\n this.queue(() => this.save(pathToSave), pathToSave);\n } else if (patch.op === 'remove') {\n const subValues = split(\n reversePatch?.op === 'add' ? reversePatch.value : {},\n path.slice(patch.path.length),\n );\n\n for (const { path } of subValues) {\n this.queue(() => this.save([...patch.path, ...path]), [...patch.path, ...path]);\n }\n } else {\n const updatedValues = split(patch.value, path.slice(patch.path.length));\n const removedValues = split(\n reversePatch?.op !== 'remove' ? (reversePatch?.value ?? {}) : {},\n path.slice(patch.path.length),\n ).filter((v) => !updatedValues.some((u) => shallowEqual(u.path, v.path)));\n\n for (const { path } of updatedValues) {\n this.queue(() => this.save([...patch.path, ...path]), [...patch.path, ...path]);\n }\n for (const { path } of removedValues) {\n this.queue(() => this.save([...patch.path, ...path]), [...patch.path, ...path]);\n }\n }\n }\n }\n },\n { runNow: this.options.persistInitialState ?? false, passive: true, throttle },\n ]);\n\n this.handles.add(cancel);\n }\n\n private async watchStorage() {\n if (!this.options.persistInitialState) {\n let items = this.storage.listItems();\n if (isPromise(items)) {\n items = await items;\n }\n\n if (this.stopped) {\n return;\n }\n\n const toLoad = new Map(\n [...items.entries()]\n .sort((a, b) => b[1].length - a[1].length)\n .map(([key, value]) => [this.parseKey(key), value])\n .filter(([key]) => key) as [Key, string][],\n );\n\n this.queue(() => this.load(toLoad));\n }\n\n this.queue(() => this.resolveInitialized?.());\n\n const listener = (event: MessageEvent) => {\n this.queue(() => this.load([{ type: 'data', path: event.data }]));\n };\n\n this.channel.addEventListener('message', listener);\n this.handles.add(() => this.channel.removeEventListener('message', listener));\n }\n\n private buildKey({ type, path }: Key) {\n return `${this.prefix}${type === 'internal' ? path : JSON.stringify(path)}`;\n }\n\n private parseKey(key: string): Key | undefined {\n if (!key.startsWith(this.prefix)) {\n return;\n }\n\n key = key.slice(this.prefix.length);\n\n if (!key.startsWith('[')) {\n return { type: 'internal', path: key };\n }\n\n return { type: 'data', path: JSON.parse(key) as KeyType[] };\n }\n\n private load(items: Key[] | Map<Key, string>): void | Promise<void> {\n return promiseChain(() => {\n if (Array.isArray(items)) {\n return promiseChain(() => {\n const entries = items.map(\n (key) =>\n promiseChain(() => {\n return this.storage.getItem(this.buildKey(key));\n }).next((value) => [key, value] as const).value,\n );\n\n return entries.some(isPromise)\n ? Promise.all(entries)\n : (entries as [Key, string | null][]);\n }).next((entries) => {\n return entries.filter((entry) => entry !== null) as [Key, string][];\n }).value;\n } else {\n return [...items.entries()];\n }\n }).next((entries) => {\n if (this.stopped) {\n return;\n }\n\n const toWrite = entries\n .filter(([key, value]) => {\n if (key.type !== 'data' || !value) {\n return;\n }\n\n if (\n !this.paths.find(\n (p) =>\n (p.path.length === 1 && p.path[0] === '*' && key.path.length === 0) ||\n (p.path.length === key.path.length && isAncestor(p.path, key.path)),\n )\n ) {\n return null;\n }\n\n const inSaveQueue = this.queue\n .getRefs()\n .find((ref) => isAncestor(ref, key.path) || isAncestor(key.path, ref));\n return !inSaveQueue;\n })\n .map(([key, value]) => {\n try {\n return {\n path: key.path,\n value: !value || value === 'undefined' ? undefined : fromExtendedJsonString(value),\n };\n } catch {\n return undefined;\n }\n })\n .filter(Boolean) as { path: KeyType[]; value: unknown }[];\n\n if (toWrite.length > 0) {\n for (const { path, value } of toWrite) {\n this.updateInProgress.set(JSON.stringify(path), value);\n }\n\n this.store.set((state) => {\n for (const { path, value } of toWrite) {\n if (value === undefined) {\n state = remove(state, path as any);\n } else {\n state = set(state, path as any, value);\n }\n }\n\n return state;\n });\n\n this.updateInProgress.clear();\n }\n\n const versionEntry = entries.find(\n ([key]) => key.type === 'internal' && key.path === 'version',\n );\n if (versionEntry) {\n this.store.version = versionEntry[1];\n }\n }).value;\n }\n\n private save(path: KeyType[]): void | Promise<unknown> {\n const key = this.buildKey({ type: 'data', path });\n const value = get(this.store.get() as any, path);\n\n return promiseChain(value)\n .next((value) => {\n if (value === undefined) {\n return this.storage.removeItem(key);\n } else {\n return this.storage.setItem(key, toExtendedJsonString(value));\n }\n })\n .next(() => {\n this.channel.postMessage(path);\n\n if (this.store.version) {\n return this.storage.setItem(\n this.buildKey({ type: 'internal', path: 'version' }),\n this.store.version,\n );\n } else {\n return this.storage.removeItem(this.buildKey({ type: 'internal', path: 'version' }));\n }\n }).value;\n }\n\n async stop(): Promise<void> {\n this.stopped = true;\n\n for (const handle of this.handles) {\n handle();\n }\n\n await this.queue.whenDone();\n this.channel.close();\n }\n}\n\nexport function persist<T>(store: Store<T>, options: PersistOptions<T>): Persist<T> {\n return new Persist<T>(store, options);\n}\n\nfunction isPlainPath<T>(p: PathOption<T>): p is WildcardPath<T> & (KeyType[] | string) {\n return typeof p === 'string' || Array.isArray(p);\n}\n"],"mappings":";;;;;;AAOA,SAAwB,aAAgB,OAAgC;AACtE,KAAI,iBAAiB,SACnB,SAAQ;AAGV,QAAO;EACL;EACA,KAAK,IAAI;GACP,MAAM,OAAOA,wBAAU,SACnB,MAAM,MAAM,YAAU,GAAGC,YACzB,GAAG;AAEP,UAAO,aAAa;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AAM,eAAe,QAAQ,MAC1B,CAAC,SAAS,IAAI,SAAS,cAAc,IAAI,SAAS;AAErD,OAAI,aACF,MAAK,MAAM,UAAU,aAAa;KAEnC;;CAGL,AAAQ,KAAK,MAA0C;EACrD,MAAM,MAAM,KAAK,SAAS;GAAE,MAAM;GAAQ;;EAC1C,MAAM,QAAQC,uBAAI,KAAK,MAAM,OAAc;AAE3C,SAAO,aAAa,OACjB,MAAM,YAAU;AACf,OAAIC,YAAU,OACZ,QAAO,KAAK,QAAQ,WAAW;OAE/B,QAAO,KAAK,QAAQ,QAAQ,KAAKC,0CAAqBD;KAGzD,WAAW;AACV,QAAK,QAAQ,YAAY;AAEzB,OAAI,KAAK,MAAM,QACb,QAAO,KAAK,QAAQ,QAClB,KAAK,SAAS;IAAE,MAAM;IAAY,MAAM;OACxC,KAAK,MAAM;OAGb,QAAO,KAAK,QAAQ,WAAW,KAAK,SAAS;IAAE,MAAM;IAAY,MAAM;;KAExE;;CAGP,MAAM,OAAsB;AAC1B,OAAK,UAAU;AAEf,OAAK,MAAM,UAAU,KAAK,QACxB;AAGF,QAAM,KAAK,MAAM;AACjB,OAAK,QAAQ;;;AAIjB,SAAgB,QAAW,OAAiB,SAAwC;AAClF,QAAO,IAAI,QAAW,OAAO;;AAG/B,SAAS,YAAe,GAA+D;AACrF,QAAO,OAAO,MAAM,YAAY,MAAM,QAAQ"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Cancel, Duration, KeyType, Store, WildcardPath } from "./store-BEsiS8y7.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/persist/persistStorage.d.ts
|
|
4
|
+
interface PersistStorageBase {
|
|
5
|
+
getItem: (key: string) => string | null | Promise<string | null>;
|
|
6
|
+
setItem: (key: string, value: string) => void | Promise<unknown>;
|
|
7
|
+
removeItem: (key: string) => void | Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface PersistStorageWithKeys extends PersistStorageBase {
|
|
10
|
+
keys: () => string[] | Promise<string[]>;
|
|
11
|
+
}
|
|
12
|
+
interface PersistStorageWithLength extends PersistStorageBase {
|
|
13
|
+
length: number | (() => number | Promise<number>);
|
|
14
|
+
key: (keyIndex: number) => string | null | Promise<string | null>;
|
|
15
|
+
}
|
|
16
|
+
interface PersistStorageWithListItems extends PersistStorageBase {
|
|
17
|
+
listItems: () => Map<string, string> | Promise<Map<string, string>>;
|
|
18
|
+
}
|
|
19
|
+
type PersistStorage = PersistStorageWithKeys | PersistStorageWithLength | PersistStorageWithListItems;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/persist/persist.d.ts
|
|
22
|
+
type PathOption<T> = WildcardPath<T> | {
|
|
23
|
+
path: WildcardPath<T>;
|
|
24
|
+
};
|
|
25
|
+
interface PersistOptions<T> {
|
|
26
|
+
id: string;
|
|
27
|
+
storage: PersistStorage;
|
|
28
|
+
paths?: PathOption<T>[];
|
|
29
|
+
throttle?: Duration;
|
|
30
|
+
persistInitialState?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare class Persist<T> {
|
|
33
|
+
readonly store: Store<T>;
|
|
34
|
+
readonly options: PersistOptions<T>;
|
|
35
|
+
readonly storage: PersistStorageWithListItems;
|
|
36
|
+
readonly [Symbol.dispose]: Cancel;
|
|
37
|
+
readonly paths: {
|
|
38
|
+
path: KeyType[];
|
|
39
|
+
throttle?: number;
|
|
40
|
+
}[];
|
|
41
|
+
readonly initialized: Promise<void>;
|
|
42
|
+
private resolveInitialized?;
|
|
43
|
+
private channel;
|
|
44
|
+
private queue;
|
|
45
|
+
private handles;
|
|
46
|
+
private stopped;
|
|
47
|
+
private updateInProgress;
|
|
48
|
+
private prefix;
|
|
49
|
+
constructor(store: Store<T>, options: PersistOptions<T>);
|
|
50
|
+
private watchStore;
|
|
51
|
+
private watchStorage;
|
|
52
|
+
private buildKey;
|
|
53
|
+
private parseKey;
|
|
54
|
+
private load;
|
|
55
|
+
private save;
|
|
56
|
+
stop(): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
declare function persist<T>(store: Store<T>, options: PersistOptions<T>): Persist<T>;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { Persist, PersistOptions, PersistStorage, PersistStorageBase, PersistStorageWithKeys, PersistStorageWithLength, PersistStorageWithListItems, persist };
|
|
61
|
+
//# sourceMappingURL=persist-D7MAsyyW.d.cts.map
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/lib/equals.ts
|
|
3
|
+
function strictEqual(a, b) {
|
|
4
|
+
return a === b;
|
|
5
|
+
}
|
|
6
|
+
function shallowEqual(a, b) {
|
|
7
|
+
return internalEqual(strictEqual)(a, b);
|
|
8
|
+
}
|
|
9
|
+
function deepEqual(a, b) {
|
|
10
|
+
return internalEqual(deepEqual)(a, b);
|
|
11
|
+
}
|
|
12
|
+
const internalEqual = (comp) => (a, b) => {
|
|
13
|
+
if (a === b) return true;
|
|
14
|
+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return a !== a && b !== b;
|
|
15
|
+
if (a.constructor !== b.constructor) return false;
|
|
16
|
+
if (a.constructor === Object || Array.isArray(a)) {
|
|
17
|
+
const entries1 = Object.entries(a);
|
|
18
|
+
const entries2 = Object.entries(b);
|
|
19
|
+
return entries1.length === entries2.length && entries1.every(([key, value]) => key in b && comp(value, b[key]));
|
|
20
|
+
}
|
|
21
|
+
if (a instanceof Date) return a.getTime() === b.getTime();
|
|
22
|
+
if (a instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
23
|
+
if (a instanceof Map) return a.size === b.size && [...a.entries()].every(([key, value]) => b.has(key) && comp(value, b.get(key)));
|
|
24
|
+
if (a instanceof Set) return a.size === b.size && [...a.values()].every((value) => b.has(value));
|
|
25
|
+
if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(a)) {
|
|
26
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
27
|
+
const a_ = new Int8Array(a.buffer);
|
|
28
|
+
const b_ = new Int8Array(b.buffer);
|
|
29
|
+
return a_.every((value, i) => value === b_[i]);
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/lib/helpers.ts
|
|
36
|
+
function isObject(value) {
|
|
37
|
+
return typeof value === "object" && value !== null;
|
|
38
|
+
}
|
|
39
|
+
function isPlainObject(value) {
|
|
40
|
+
if (typeof value !== "object" || value === null) return false;
|
|
41
|
+
const prototype = Object.getPrototypeOf(value);
|
|
42
|
+
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/lib/clone.ts
|
|
47
|
+
function flatClone(object) {
|
|
48
|
+
if (object instanceof Map) return new Map(object);
|
|
49
|
+
if (object instanceof Set) return new Set(object);
|
|
50
|
+
if (Array.isArray(object)) return [...object];
|
|
51
|
+
if (isObject(object)) return { ...object };
|
|
52
|
+
return object;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/lib/propAccess.ts
|
|
57
|
+
function castArrayPath(path) {
|
|
58
|
+
if (Array.isArray(path)) return path;
|
|
59
|
+
if (path === "") return [];
|
|
60
|
+
return path.split(".");
|
|
61
|
+
}
|
|
62
|
+
function get(object, path) {
|
|
63
|
+
const _path = castArrayPath(path);
|
|
64
|
+
const [first, ...rest] = _path;
|
|
65
|
+
if (first === void 0 || !object) return object;
|
|
66
|
+
if (object instanceof Map) return get(object.get(first), rest);
|
|
67
|
+
if (object instanceof Set) return get(Array.from(object)[Number(first)], rest);
|
|
68
|
+
if (isObject(object)) return get(object[first], rest);
|
|
69
|
+
throw new Error(`Could not get ${path} of ${object}`);
|
|
70
|
+
}
|
|
71
|
+
function set(object, path, value, rootPath = path) {
|
|
72
|
+
const _path = castArrayPath(path);
|
|
73
|
+
const [first, ...rest] = _path;
|
|
74
|
+
if (first === void 0) return value;
|
|
75
|
+
if (object instanceof Map) {
|
|
76
|
+
const copy = flatClone(object);
|
|
77
|
+
const child = copy.get(first);
|
|
78
|
+
copy.set(first, set(child, rest, value, rootPath));
|
|
79
|
+
return copy;
|
|
80
|
+
}
|
|
81
|
+
if (object instanceof Set) {
|
|
82
|
+
const copy = [...object];
|
|
83
|
+
const child = copy[Number(first)];
|
|
84
|
+
copy[Number(first)] = set(child, rest, value, rootPath);
|
|
85
|
+
return new Set(copy);
|
|
86
|
+
}
|
|
87
|
+
if (isObject(object) || object === void 0) {
|
|
88
|
+
const copy = flatClone(object ?? {});
|
|
89
|
+
copy[first] = set(copy[first], rest, value, rootPath);
|
|
90
|
+
return copy;
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`Could not set ${path} of ${object}`);
|
|
93
|
+
}
|
|
94
|
+
function remove(object, path) {
|
|
95
|
+
const _path = castArrayPath(path);
|
|
96
|
+
if (_path.length === 0) return void 0;
|
|
97
|
+
const parentPath = _path.slice(0, -1);
|
|
98
|
+
const key = _path[_path.length - 1];
|
|
99
|
+
const parent = flatClone(get(object, parentPath));
|
|
100
|
+
if (parent instanceof Map) parent.delete(key);
|
|
101
|
+
else if (parent instanceof Set) {
|
|
102
|
+
const value = Array.from(parent)[Number(key)];
|
|
103
|
+
parent.delete(value);
|
|
104
|
+
} else if (Array.isArray(parent)) parent.splice(Number(key), 1);
|
|
105
|
+
else delete parent[key];
|
|
106
|
+
return set(object, parentPath, parent);
|
|
107
|
+
}
|
|
108
|
+
function join(a, b) {
|
|
109
|
+
return [a, b].filter(Boolean).join(".");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
Object.defineProperty(exports, 'castArrayPath', {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
get: function () {
|
|
116
|
+
return castArrayPath;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
Object.defineProperty(exports, 'deepEqual', {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
get: function () {
|
|
122
|
+
return deepEqual;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(exports, 'get', {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
get: function () {
|
|
128
|
+
return get;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(exports, 'isObject', {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
get: function () {
|
|
134
|
+
return isObject;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(exports, 'isPlainObject', {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
get: function () {
|
|
140
|
+
return isPlainObject;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
Object.defineProperty(exports, 'join', {
|
|
144
|
+
enumerable: true,
|
|
145
|
+
get: function () {
|
|
146
|
+
return join;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
Object.defineProperty(exports, 'remove', {
|
|
150
|
+
enumerable: true,
|
|
151
|
+
get: function () {
|
|
152
|
+
return remove;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
Object.defineProperty(exports, 'set', {
|
|
156
|
+
enumerable: true,
|
|
157
|
+
get: function () {
|
|
158
|
+
return set;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
Object.defineProperty(exports, 'shallowEqual', {
|
|
162
|
+
enumerable: true,
|
|
163
|
+
get: function () {
|
|
164
|
+
return shallowEqual;
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
Object.defineProperty(exports, 'strictEqual', {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
get: function () {
|
|
170
|
+
return strictEqual;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
//# sourceMappingURL=propAccess-ByqBjC19.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"propAccess-ByqBjC19.cjs","names":[],"sources":["../src/lib/equals.ts","../src/lib/helpers.ts","../src/lib/clone.ts","../src/lib/propAccess.ts"],"sourcesContent":["export function strictEqual(a: any, b: any): boolean {\n return a === b;\n}\n\nexport function shallowEqual(a: any, b: any): boolean {\n return internalEqual(strictEqual)(a, b);\n}\n\nexport function deepEqual(a: any, b: any): boolean {\n return internalEqual(deepEqual)(a, b);\n}\n\nconst internalEqual = (comp: (a: any, b: any) => boolean) => (a: any, b: any) => {\n if (a === b) {\n return true;\n }\n\n if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') {\n // eslint-disable-next-line no-self-compare\n return a !== a && b !== b;\n }\n\n if (a.constructor !== b.constructor) {\n return false;\n }\n\n if (a.constructor === Object || Array.isArray(a)) {\n const entries1 = Object.entries(a);\n const entries2 = Object.entries(b);\n return (\n entries1.length === entries2.length &&\n entries1.every(([key, value]) => key in b && comp(value, b[key]))\n );\n }\n\n if (a instanceof Date) {\n return a.getTime() === b.getTime();\n }\n\n if (a instanceof RegExp) {\n return a.source === b.source && a.flags === b.flags;\n }\n\n if (a instanceof Map) {\n return (\n a.size === b.size &&\n [...a.entries()].every(([key, value]) => b.has(key) && comp(value, b.get(key)))\n );\n }\n\n if (a instanceof Set) {\n return a.size === b.size && [...a.values()].every((value) => b.has(value));\n }\n\n if (typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView(a)) {\n if (a.byteLength !== b.byteLength) {\n return false;\n }\n\n const a_ = new Int8Array(a.buffer);\n const b_ = new Int8Array(b.buffer);\n return a_.every((value, i) => value === b_[i]);\n }\n\n return false;\n};\n","export function isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nexport function isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(value);\n return (\n (prototype === null ||\n prototype === Object.prototype ||\n Object.getPrototypeOf(prototype) === null) &&\n !(Symbol.toStringTag in value) &&\n !(Symbol.iterator in value)\n );\n}\n","import { isObject } from '@lib/helpers';\n\nexport function flatClone<T>(object: T): T {\n if (object instanceof Map) {\n return new Map(object) as any;\n }\n\n if (object instanceof Set) {\n return new Set(object) as any;\n }\n\n if (Array.isArray(object)) {\n return [...object] as any;\n }\n\n if (isObject(object)) {\n return { ...object };\n }\n\n return object;\n}\n","import type { Constrain } from '@lib/constrain';\nimport { isObject } from '@lib/helpers';\nimport { flatClone } from './clone';\nimport type { KeyType, Path, SettablePath, SettableValue, Value } from './path';\n\nexport function castArrayPath(path: string | KeyType[]): KeyType[] {\n if (Array.isArray(path)) {\n return path;\n }\n\n if (path === '') {\n return [];\n }\n\n return (path as string).split('.');\n}\n\nexport function get<T, const P>(object: T, path: Constrain<P, Path<T>>): Value<T, P> {\n const _path = castArrayPath(path as any);\n const [first, ...rest] = _path;\n\n if (first === undefined || !object) {\n return object as Value<T, P>;\n }\n\n if (object instanceof Map) {\n return get(object.get(first), rest);\n }\n\n if (object instanceof Set) {\n return get(Array.from(object)[Number(first)], rest);\n }\n\n if (isObject(object)) {\n return get(object[first as keyof T], rest as any) as Value<T, P>;\n }\n\n throw new Error(`Could not get ${path} of ${object}`);\n}\n\nexport function set<T, const P>(\n object: T,\n path: Constrain<P, SettablePath<T>>,\n value: SettableValue<T, P>,\n rootPath: string | readonly KeyType[] = path,\n): T {\n const _path = castArrayPath(path as any);\n const [first, ...rest] = _path;\n\n if (first === undefined) {\n return value as T;\n }\n\n if (object instanceof Map) {\n const copy = flatClone(object);\n const child = copy.get(first);\n copy.set(first, set(child, rest as any, value, rootPath));\n return copy;\n }\n\n if (object instanceof Set) {\n const copy = [...object];\n const child = copy[Number(first)];\n copy[Number(first)] = set(child, rest as any, value, rootPath);\n return new Set(copy) as any;\n }\n\n if (isObject(object) || object === undefined) {\n const copy = flatClone(object ?? ({} as T));\n copy[first as keyof T] = set(copy[first as keyof T], rest as any, value as any, rootPath);\n return copy;\n }\n\n throw new Error(`Could not set ${path} of ${object}`);\n}\n\nexport function remove<T, const P>(object: T, path: Constrain<P, Path<T, true>>): T {\n const _path = castArrayPath(path as any);\n\n if (_path.length === 0) {\n return undefined as any;\n }\n\n const parentPath = _path.slice(0, -1);\n const key = _path[_path.length - 1];\n\n const parent = flatClone(get(object, parentPath as any));\n\n if (parent instanceof Map) {\n parent.delete(key);\n } else if (parent instanceof Set) {\n const value = Array.from(parent)[Number(key)];\n parent.delete(value);\n } else if (Array.isArray(parent)) {\n parent.splice(Number(key), 1);\n } else {\n delete parent[key as keyof typeof parent];\n }\n\n return set(object, parentPath as any, parent);\n}\n\nexport function join(a: string, b: string): string {\n return [a, b].filter(Boolean).join('.');\n}\n"],"mappings":";;AAAA,SAAgB,YAAY,GAAQ,GAAiB;AACnD,QAAO,MAAM;;AAGf,SAAgB,aAAa,GAAQ,GAAiB;AACpD,QAAO,cAAc,aAAa,GAAG;;AAGvC,SAAgB,UAAU,GAAQ,GAAiB;AACjD,QAAO,cAAc,WAAW,GAAG;;AAGrC,MAAM,iBAAiB,UAAuC,GAAQ,MAAW;AAC/E,KAAI,MAAM,EACR,QAAO;AAGT,KAAI,MAAM,QAAQ,MAAM,QAAQ,OAAO,MAAM,YAAY,OAAO,MAAM,SAEpE,QAAO,MAAM,KAAK,MAAM;AAG1B,KAAI,EAAE,gBAAgB,EAAE,YACtB,QAAO;AAGT,KAAI,EAAE,gBAAgB,UAAU,MAAM,QAAQ,IAAI;EAChD,MAAM,WAAW,OAAO,QAAQ;EAChC,MAAM,WAAW,OAAO,QAAQ;AAChC,SACE,SAAS,WAAW,SAAS,UAC7B,SAAS,OAAO,CAAC,KAAK,WAAW,OAAO,KAAK,KAAK,OAAO,EAAE;;AAI/D,KAAI,aAAa,KACf,QAAO,EAAE,cAAc,EAAE;AAG3B,KAAI,aAAa,OACf,QAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE;AAGhD,KAAI,aAAa,IACf,QACE,EAAE,SAAS,EAAE,QACb,CAAC,GAAG,EAAE,WAAW,OAAO,CAAC,KAAK,WAAW,EAAE,IAAI,QAAQ,KAAK,OAAO,EA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OAAO,SAAS,KAAK"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
//#region src/lib/equals.ts
|
|
2
|
+
function strictEqual(a, b) {
|
|
3
|
+
return a === b;
|
|
4
|
+
}
|
|
5
|
+
function shallowEqual(a, b) {
|
|
6
|
+
return internalEqual(strictEqual)(a, b);
|
|
7
|
+
}
|
|
8
|
+
function deepEqual(a, b) {
|
|
9
|
+
return internalEqual(deepEqual)(a, b);
|
|
10
|
+
}
|
|
11
|
+
const internalEqual = (comp) => (a, b) => {
|
|
12
|
+
if (a === b) return true;
|
|
13
|
+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return a !== a && b !== b;
|
|
14
|
+
if (a.constructor !== b.constructor) return false;
|
|
15
|
+
if (a.constructor === Object || Array.isArray(a)) {
|
|
16
|
+
const entries1 = Object.entries(a);
|
|
17
|
+
const entries2 = Object.entries(b);
|
|
18
|
+
return entries1.length === entries2.length && entries1.every(([key, value]) => key in b && comp(value, b[key]));
|
|
19
|
+
}
|
|
20
|
+
if (a instanceof Date) return a.getTime() === b.getTime();
|
|
21
|
+
if (a instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
22
|
+
if (a instanceof Map) return a.size === b.size && [...a.entries()].every(([key, value]) => b.has(key) && comp(value, b.get(key)));
|
|
23
|
+
if (a instanceof Set) return a.size === b.size && [...a.values()].every((value) => b.has(value));
|
|
24
|
+
if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(a)) {
|
|
25
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
26
|
+
const a_ = new Int8Array(a.buffer);
|
|
27
|
+
const b_ = new Int8Array(b.buffer);
|
|
28
|
+
return a_.every((value, i) => value === b_[i]);
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/lib/helpers.ts
|
|
35
|
+
function isObject(value) {
|
|
36
|
+
return typeof value === "object" && value !== null;
|
|
37
|
+
}
|
|
38
|
+
function isPlainObject(value) {
|
|
39
|
+
if (typeof value !== "object" || value === null) return false;
|
|
40
|
+
const prototype = Object.getPrototypeOf(value);
|
|
41
|
+
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/lib/clone.ts
|
|
46
|
+
function flatClone(object) {
|
|
47
|
+
if (object instanceof Map) return new Map(object);
|
|
48
|
+
if (object instanceof Set) return new Set(object);
|
|
49
|
+
if (Array.isArray(object)) return [...object];
|
|
50
|
+
if (isObject(object)) return { ...object };
|
|
51
|
+
return object;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/lib/propAccess.ts
|
|
56
|
+
function castArrayPath(path) {
|
|
57
|
+
if (Array.isArray(path)) return path;
|
|
58
|
+
if (path === "") return [];
|
|
59
|
+
return path.split(".");
|
|
60
|
+
}
|
|
61
|
+
function get(object, path) {
|
|
62
|
+
const _path = castArrayPath(path);
|
|
63
|
+
const [first, ...rest] = _path;
|
|
64
|
+
if (first === void 0 || !object) return object;
|
|
65
|
+
if (object instanceof Map) return get(object.get(first), rest);
|
|
66
|
+
if (object instanceof Set) return get(Array.from(object)[Number(first)], rest);
|
|
67
|
+
if (isObject(object)) return get(object[first], rest);
|
|
68
|
+
throw new Error(`Could not get ${path} of ${object}`);
|
|
69
|
+
}
|
|
70
|
+
function set(object, path, value, rootPath = path) {
|
|
71
|
+
const _path = castArrayPath(path);
|
|
72
|
+
const [first, ...rest] = _path;
|
|
73
|
+
if (first === void 0) return value;
|
|
74
|
+
if (object instanceof Map) {
|
|
75
|
+
const copy = flatClone(object);
|
|
76
|
+
const child = copy.get(first);
|
|
77
|
+
copy.set(first, set(child, rest, value, rootPath));
|
|
78
|
+
return copy;
|
|
79
|
+
}
|
|
80
|
+
if (object instanceof Set) {
|
|
81
|
+
const copy = [...object];
|
|
82
|
+
const child = copy[Number(first)];
|
|
83
|
+
copy[Number(first)] = set(child, rest, value, rootPath);
|
|
84
|
+
return new Set(copy);
|
|
85
|
+
}
|
|
86
|
+
if (isObject(object) || object === void 0) {
|
|
87
|
+
const copy = flatClone(object ?? {});
|
|
88
|
+
copy[first] = set(copy[first], rest, value, rootPath);
|
|
89
|
+
return copy;
|
|
90
|
+
}
|
|
91
|
+
throw new Error(`Could not set ${path} of ${object}`);
|
|
92
|
+
}
|
|
93
|
+
function remove(object, path) {
|
|
94
|
+
const _path = castArrayPath(path);
|
|
95
|
+
if (_path.length === 0) return void 0;
|
|
96
|
+
const parentPath = _path.slice(0, -1);
|
|
97
|
+
const key = _path[_path.length - 1];
|
|
98
|
+
const parent = flatClone(get(object, parentPath));
|
|
99
|
+
if (parent instanceof Map) parent.delete(key);
|
|
100
|
+
else if (parent instanceof Set) {
|
|
101
|
+
const value = Array.from(parent)[Number(key)];
|
|
102
|
+
parent.delete(value);
|
|
103
|
+
} else if (Array.isArray(parent)) parent.splice(Number(key), 1);
|
|
104
|
+
else delete parent[key];
|
|
105
|
+
return set(object, parentPath, parent);
|
|
106
|
+
}
|
|
107
|
+
function join(a, b) {
|
|
108
|
+
return [a, b].filter(Boolean).join(".");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
export { castArrayPath, deepEqual, get, isObject, isPlainObject, join, remove, set, shallowEqual, strictEqual };
|
|
113
|
+
//# sourceMappingURL=propAccess-DGchzVfy.js.map
|