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,48 @@
|
|
|
1
|
+
import { Cancel, DisposableCancel, Store, SubscribeOptions } from "./store-BEsiS8y7.cjs";
|
|
2
|
+
import { DiffOptions, Patch } from "./diff-BQ8bB3Wk.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/patches/patchMethods.d.ts
|
|
5
|
+
interface SyncMessage {
|
|
6
|
+
fromVersion?: string;
|
|
7
|
+
toVersion: string;
|
|
8
|
+
patches: Patch[];
|
|
9
|
+
}
|
|
10
|
+
interface HistoryEntry extends SyncMessage {
|
|
11
|
+
reversePatches: Patch[];
|
|
12
|
+
}
|
|
13
|
+
declare module "@core" {
|
|
14
|
+
interface Store<T> {
|
|
15
|
+
__patches?: {
|
|
16
|
+
value: T;
|
|
17
|
+
version: string;
|
|
18
|
+
history: HistoryEntry[];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
interface SubscribePatchOptions extends SubscribeOptions, DiffOptions {
|
|
23
|
+
/** @default false */
|
|
24
|
+
runNow?: boolean;
|
|
25
|
+
/** try to start from a specific version and only receive patches after that.
|
|
26
|
+
* If the id is not found, it will start from the beginning */
|
|
27
|
+
startAt?: string;
|
|
28
|
+
}
|
|
29
|
+
type InteropPatch = Patch | {
|
|
30
|
+
op: "add" | "replace" | "remove";
|
|
31
|
+
value?: any;
|
|
32
|
+
};
|
|
33
|
+
declare function subscribePatches<T>(this: Store<T>, listener: (this: {
|
|
34
|
+
cancel: Cancel;
|
|
35
|
+
}, patches: Patch[], reversePatches: Patch[], version: string, previousVersion: string | undefined) => void, options?: SubscribePatchOptions): DisposableCancel;
|
|
36
|
+
declare function applyPatches<T>(this: Store<T>, patches: InteropPatch[]): void;
|
|
37
|
+
declare function applyPatches<T>(this: Store<T>, ...patches: InteropPatch[]): void;
|
|
38
|
+
declare function sync<T>(this: Store<T>, listener: (syncMessage: SyncMessage) => void, options?: Omit<SubscribePatchOptions, "runNow">): DisposableCancel;
|
|
39
|
+
declare function acceptSync<T>(this: Store<T>, message: SyncMessage): void;
|
|
40
|
+
declare const patchMethods: {
|
|
41
|
+
subscribePatches: typeof subscribePatches;
|
|
42
|
+
applyPatches: typeof applyPatches;
|
|
43
|
+
sync: typeof sync;
|
|
44
|
+
acceptSync: typeof acceptSync;
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { SubscribePatchOptions, SyncMessage, patchMethods };
|
|
48
|
+
//# sourceMappingURL=patchMethods-C3L7Kh8T.d.cts.map
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
const require_propAccess = require('./propAccess-ByqBjC19.cjs');
|
|
2
|
+
const require_extendedJson = require('./extendedJson-Dn2F7Edo.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/lib/applyPatches.ts
|
|
5
|
+
function applySinglePatch(target, patch) {
|
|
6
|
+
if (patch.op === "remove") return require_propAccess.remove(target, patch.path);
|
|
7
|
+
return require_propAccess.set(target, patch.path, patch.value);
|
|
8
|
+
}
|
|
9
|
+
function applyPatches(target, ...patches) {
|
|
10
|
+
for (const patch of patches) target = applySinglePatch(target, patch);
|
|
11
|
+
return target;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/lib/diff.ts
|
|
16
|
+
function diff(a, b, options = {}) {
|
|
17
|
+
const result = [..._diff(a, b, options)];
|
|
18
|
+
const patches = result.map(([patch]) => patch);
|
|
19
|
+
const reversePatches = result.map(([, reversePatch]) => reversePatch);
|
|
20
|
+
return [patches, reversePatches];
|
|
21
|
+
}
|
|
22
|
+
function* _diff(a, b, options, prefix = []) {
|
|
23
|
+
if (a === b) return;
|
|
24
|
+
if (typeof options.stopAt === "number" && prefix.length >= options.stopAt || typeof options.stopAt === "function" && options.stopAt(prefix)) {
|
|
25
|
+
if (require_propAccess.deepEqual(a, b)) return;
|
|
26
|
+
return yield [{
|
|
27
|
+
op: "replace",
|
|
28
|
+
path: prefix,
|
|
29
|
+
value: b
|
|
30
|
+
}, {
|
|
31
|
+
op: "replace",
|
|
32
|
+
path: prefix,
|
|
33
|
+
value: a
|
|
34
|
+
}];
|
|
35
|
+
}
|
|
36
|
+
if (a instanceof Date && b instanceof Date) {
|
|
37
|
+
if (a.getTime() === b.getTime()) return;
|
|
38
|
+
return yield [{
|
|
39
|
+
op: "replace",
|
|
40
|
+
path: prefix,
|
|
41
|
+
value: b
|
|
42
|
+
}, {
|
|
43
|
+
op: "replace",
|
|
44
|
+
path: prefix,
|
|
45
|
+
value: a
|
|
46
|
+
}];
|
|
47
|
+
}
|
|
48
|
+
if (a instanceof Map && b instanceof Map) return yield* mapDiff(a, b, options, prefix);
|
|
49
|
+
if (a instanceof Set && b instanceof Set) {
|
|
50
|
+
if (require_propAccess.deepEqual(a, b)) return;
|
|
51
|
+
return yield [{
|
|
52
|
+
op: "replace",
|
|
53
|
+
path: prefix,
|
|
54
|
+
value: b
|
|
55
|
+
}, {
|
|
56
|
+
op: "replace",
|
|
57
|
+
path: prefix,
|
|
58
|
+
value: a
|
|
59
|
+
}];
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
62
|
+
if (require_propAccess.deepEqual(a, b)) return;
|
|
63
|
+
return yield [{
|
|
64
|
+
op: "replace",
|
|
65
|
+
path: prefix,
|
|
66
|
+
value: b
|
|
67
|
+
}, {
|
|
68
|
+
op: "replace",
|
|
69
|
+
path: prefix,
|
|
70
|
+
value: a
|
|
71
|
+
}];
|
|
72
|
+
}
|
|
73
|
+
if (require_propAccess.isObject(a) && require_propAccess.isObject(b) && !Array.isArray(a) && !Array.isArray(b)) return yield* objectDiff(a, b, options, prefix);
|
|
74
|
+
yield [{
|
|
75
|
+
op: "replace",
|
|
76
|
+
path: prefix,
|
|
77
|
+
value: b
|
|
78
|
+
}, {
|
|
79
|
+
op: "replace",
|
|
80
|
+
path: prefix,
|
|
81
|
+
value: a
|
|
82
|
+
}];
|
|
83
|
+
}
|
|
84
|
+
function* mapDiff(a, b, options, prefix) {
|
|
85
|
+
for (const [key, value] of a) if (!b.has(key)) yield [{
|
|
86
|
+
op: "remove",
|
|
87
|
+
path: [...prefix, key]
|
|
88
|
+
}, {
|
|
89
|
+
op: "add",
|
|
90
|
+
path: [...prefix, key],
|
|
91
|
+
value
|
|
92
|
+
}];
|
|
93
|
+
else yield* _diff(value, b.get(key), options, [...prefix, key]);
|
|
94
|
+
for (const [key, value] of b) if (!a.has(key)) yield [{
|
|
95
|
+
op: "add",
|
|
96
|
+
path: [...prefix, key],
|
|
97
|
+
value
|
|
98
|
+
}, {
|
|
99
|
+
op: "remove",
|
|
100
|
+
path: [...prefix, key]
|
|
101
|
+
}];
|
|
102
|
+
}
|
|
103
|
+
function* objectDiff(a, b, options, prefix) {
|
|
104
|
+
for (const [key, value] of Object.entries(a)) if (!(key in b)) yield [{
|
|
105
|
+
op: "remove",
|
|
106
|
+
path: [...prefix, key]
|
|
107
|
+
}, {
|
|
108
|
+
op: "add",
|
|
109
|
+
path: [...prefix, key],
|
|
110
|
+
value
|
|
111
|
+
}];
|
|
112
|
+
else yield* _diff(value, b[key], options, [...prefix, key]);
|
|
113
|
+
for (const [key, value] of Object.entries(b)) if (!(key in a)) yield [{
|
|
114
|
+
op: "add",
|
|
115
|
+
path: [...prefix, key],
|
|
116
|
+
value
|
|
117
|
+
}, {
|
|
118
|
+
op: "remove",
|
|
119
|
+
path: [...prefix, key]
|
|
120
|
+
}];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/lib/trie.ts
|
|
125
|
+
var TrieNode = class {
|
|
126
|
+
constructor() {
|
|
127
|
+
this.children = /* @__PURE__ */ new Map();
|
|
128
|
+
this.isLeaf = false;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var Trie = class {
|
|
132
|
+
constructor() {
|
|
133
|
+
this.root = new TrieNode();
|
|
134
|
+
}
|
|
135
|
+
add(path) {
|
|
136
|
+
let node = this.root;
|
|
137
|
+
for (const key of path) {
|
|
138
|
+
let next = node.children.get(key);
|
|
139
|
+
if (!next) node.children.set(key, next = new TrieNode());
|
|
140
|
+
node = next;
|
|
141
|
+
}
|
|
142
|
+
node.isLeaf = true;
|
|
143
|
+
}
|
|
144
|
+
hasSubPath(path) {
|
|
145
|
+
let node = this.root;
|
|
146
|
+
for (const key of path) {
|
|
147
|
+
const next = node.children.get(key);
|
|
148
|
+
if (!next) return false;
|
|
149
|
+
node = next;
|
|
150
|
+
}
|
|
151
|
+
return node.isLeaf;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/patches/patchMethods.ts
|
|
157
|
+
const genId = () => Math.random().toString(36).slice(2);
|
|
158
|
+
function subscribePatches(listener, options = {}) {
|
|
159
|
+
const patches = this.__patches ??= {
|
|
160
|
+
value: this.get(),
|
|
161
|
+
version: genId(),
|
|
162
|
+
history: []
|
|
163
|
+
};
|
|
164
|
+
options = { ...options };
|
|
165
|
+
options.runNow ??= false;
|
|
166
|
+
let cursor = options.startAt ?? (options.runNow ? void 0 : this.__patches.version);
|
|
167
|
+
return this.subscribe(function(value) {
|
|
168
|
+
if (patches.value !== value) {
|
|
169
|
+
const result = diff(patches.value, value, options);
|
|
170
|
+
patches.value = value;
|
|
171
|
+
if (result[0].length > 0) {
|
|
172
|
+
const newVersion = genId();
|
|
173
|
+
patches.history = patches.history.concat({
|
|
174
|
+
fromVersion: patches.version,
|
|
175
|
+
toVersion: newVersion,
|
|
176
|
+
patches: result[0],
|
|
177
|
+
reversePatches: result[1]
|
|
178
|
+
}).slice(-1e3);
|
|
179
|
+
patches.version = newVersion;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (cursor === patches.version) return;
|
|
183
|
+
const index = patches.history.findIndex((h) => h.fromVersion === cursor);
|
|
184
|
+
let forward, backward, previousVersion;
|
|
185
|
+
if (index === -1) {
|
|
186
|
+
[forward, backward] = diff(void 0, value, options);
|
|
187
|
+
previousVersion = void 0;
|
|
188
|
+
} else {
|
|
189
|
+
forward = patches.history.slice(index).flatMap((h) => h.patches);
|
|
190
|
+
backward = patches.history.slice(index).flatMap((h) => h.reversePatches);
|
|
191
|
+
previousVersion = cursor;
|
|
192
|
+
}
|
|
193
|
+
cursor = patches.version;
|
|
194
|
+
listener.apply(this, [
|
|
195
|
+
forward,
|
|
196
|
+
backward,
|
|
197
|
+
cursor,
|
|
198
|
+
previousVersion
|
|
199
|
+
]);
|
|
200
|
+
}, options);
|
|
201
|
+
}
|
|
202
|
+
function applyPatches$1(...patches) {
|
|
203
|
+
this.set((value) => applyPatches(value, ...patches.flat()));
|
|
204
|
+
}
|
|
205
|
+
function sync(listener, options) {
|
|
206
|
+
const debounce = typeof options?.debounce === "object" && "wait" in options.debounce ? { ...options.debounce } : options?.debounce !== void 0 ? { wait: options.debounce } : void 0;
|
|
207
|
+
if (debounce) debounce.waitOnRunNow ??= false;
|
|
208
|
+
return subscribePatches.apply(this, [(patches, _, version, previousVersion) => {
|
|
209
|
+
const trie = new Trie();
|
|
210
|
+
patches = [...patches].reverse().filter((patch) => {
|
|
211
|
+
if (trie.hasSubPath(patch.path)) return false;
|
|
212
|
+
trie.add(patch.path);
|
|
213
|
+
return true;
|
|
214
|
+
}).reverse();
|
|
215
|
+
listener({
|
|
216
|
+
fromVersion: previousVersion,
|
|
217
|
+
toVersion: version,
|
|
218
|
+
patches: require_extendedJson.toExtendedJson(patches)
|
|
219
|
+
});
|
|
220
|
+
}, {
|
|
221
|
+
...options,
|
|
222
|
+
debounce,
|
|
223
|
+
runNow: true
|
|
224
|
+
}]);
|
|
225
|
+
}
|
|
226
|
+
function acceptSync(message) {
|
|
227
|
+
if (message.fromVersion && message.fromVersion !== this.version) throw new Error(`version mismatch! version=${this.version}, fromVersion=${message.fromVersion}`);
|
|
228
|
+
const patches = require_extendedJson.fromExtendedJson(message.patches);
|
|
229
|
+
this.version = message.toVersion;
|
|
230
|
+
applyPatches$1.apply(this, patches);
|
|
231
|
+
}
|
|
232
|
+
const patchMethods = {
|
|
233
|
+
subscribePatches,
|
|
234
|
+
applyPatches: applyPatches$1,
|
|
235
|
+
sync,
|
|
236
|
+
acceptSync
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
//#endregion
|
|
240
|
+
Object.defineProperty(exports, 'applyPatches', {
|
|
241
|
+
enumerable: true,
|
|
242
|
+
get: function () {
|
|
243
|
+
return applyPatches;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
Object.defineProperty(exports, 'diff', {
|
|
247
|
+
enumerable: true,
|
|
248
|
+
get: function () {
|
|
249
|
+
return diff;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
Object.defineProperty(exports, 'patchMethods', {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
get: function () {
|
|
255
|
+
return patchMethods;
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
Object.defineProperty(exports, 'subscribePatches', {
|
|
259
|
+
enumerable: true,
|
|
260
|
+
get: function () {
|
|
261
|
+
return subscribePatches;
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
//# sourceMappingURL=patchMethods-D5YJwcpn.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchMethods-D5YJwcpn.cjs","names":["remove","set","deepEqual","isObject","applyPatches","_applyPatches","toExtendedJson","fromExtendedJson","patchMethods: {\n subscribePatches: typeof subscribePatches;\n applyPatches: typeof applyPatches;\n sync: typeof sync;\n acceptSync: typeof acceptSync;\n}"],"sources":["../src/lib/applyPatches.ts","../src/lib/diff.ts","../src/lib/trie.ts","../src/patches/patchMethods.ts"],"sourcesContent":["import type { Patch } from './diff';\nimport { remove, set } from './propAccess';\n\nfunction applySinglePatch<T>(target: T, patch: Patch): T {\n if (patch.op === 'remove') {\n return remove(target, patch.path as any);\n }\n\n return set(target, patch.path as any, patch.value);\n}\n\nexport function applyPatches<T>(target: T, ...patches: Patch[]): T {\n for (const patch of patches) {\n target = applySinglePatch(target, patch);\n }\n\n return target;\n}\n","import { isObject } from '@lib/helpers';\nimport { deepEqual } from './equals';\nimport type { KeyType } from './path';\n\nexport type Patch =\n | { op: 'add'; path: KeyType[]; value: any }\n | { op: 'remove'; path: KeyType[] }\n | { op: 'replace'; path: KeyType[]; value: any };\n\nexport interface DiffOptions {\n stopAt?: number | ((path: KeyType[]) => boolean);\n}\n\nexport function diff(\n a: any,\n b: any,\n options: DiffOptions = {},\n): [patches: Patch[], reversePatches: Patch[]] {\n const result = [..._diff(a, b, options)];\n const patches = result.map(([patch]) => patch);\n const reversePatches = result.map(([, reversePatch]) => reversePatch);\n\n return [patches, reversePatches];\n}\n\nfunction* _diff(\n a: any,\n b: any,\n options: DiffOptions,\n prefix: KeyType[] = [],\n): Iterable<[patch: Patch, reversePatch: Patch]> {\n if (a === b) {\n return;\n }\n\n if (\n (typeof options.stopAt === 'number' && prefix.length >= options.stopAt) ||\n (typeof options.stopAt === 'function' && options.stopAt(prefix))\n ) {\n if (deepEqual(a, b)) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (a instanceof Date && b instanceof Date) {\n if (a.getTime() === b.getTime()) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (a instanceof Map && b instanceof Map) {\n return yield* mapDiff(a, b, options, prefix);\n }\n\n if (a instanceof Set && b instanceof Set) {\n if (deepEqual(a, b)) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (Array.isArray(a) && Array.isArray(b)) {\n if (deepEqual(a, b)) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (isObject(a) && isObject(b) && !Array.isArray(a) && !Array.isArray(b)) {\n return yield* objectDiff(a, b, options, prefix);\n }\n\n yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n}\n\nfunction* mapDiff(\n a: Map<any, any>,\n b: Map<any, any>,\n options: { stopAt?: number | ((path: KeyType[]) => boolean) },\n prefix: KeyType[],\n): Iterable<[patch: Patch, reversePatch: Patch]> {\n for (const [key, value] of a) {\n if (!b.has(key)) {\n yield [\n { op: 'remove', path: [...prefix, key] },\n { op: 'add', path: [...prefix, key], value },\n ];\n } else {\n yield* _diff(value, b.get(key), options, [...prefix, key]);\n }\n }\n\n for (const [key, value] of b) {\n if (!a.has(key)) {\n yield [\n { op: 'add', path: [...prefix, key], value },\n { op: 'remove', path: [...prefix, key] },\n ];\n }\n }\n}\n\nfunction* objectDiff(\n a: any,\n b: any,\n options: { stopAt?: number | ((path: KeyType[]) => boolean) },\n prefix: KeyType[],\n): Iterable<[patch: Patch, reversePatch: Patch]> {\n for (const [key, value] of Object.entries(a)) {\n if (!(key in b)) {\n yield [\n { op: 'remove', path: [...prefix, key] },\n { op: 'add', path: [...prefix, key], value },\n ];\n } else {\n yield* _diff(value, b[key], options, [...prefix, key]);\n }\n }\n\n for (const [key, value] of Object.entries(b)) {\n if (!(key in a)) {\n yield [\n { op: 'add', path: [...prefix, key], value },\n { op: 'remove', path: [...prefix, key] },\n ];\n }\n }\n}\n","import type { KeyType } from '@lib/path';\n\nclass TrieNode {\n children: Map<KeyType, TrieNode> = new Map();\n isLeaf = false;\n}\n\nexport class Trie {\n root: TrieNode = new TrieNode();\n\n add(path: KeyType[]): void {\n let node = this.root;\n for (const key of path) {\n let next = node.children.get(key);\n if (!next) node.children.set(key, (next = new TrieNode()));\n node = next;\n }\n node.isLeaf = true;\n }\n\n hasSubPath(path: KeyType[]): boolean {\n let node = this.root;\n for (const key of path) {\n const next = node.children.get(key);\n if (!next) return false;\n node = next;\n }\n return node.isLeaf;\n }\n}\n","import type { Cancel, DisposableCancel, SubscribeOptions } from '@core/commonTypes';\nimport type { Store } from '@core/store';\nimport { applyPatches as _applyPatches } from '@lib/applyPatches';\nimport { diff, type DiffOptions, type Patch } from '@lib/diff';\nimport { fromExtendedJson, toExtendedJson } from '@lib/extendedJson';\nimport { Trie } from '@lib/trie';\n\nexport interface SyncMessage {\n fromVersion?: string;\n toVersion: string;\n patches: Patch[];\n}\n\nexport interface HistoryEntry extends SyncMessage {\n reversePatches: Patch[];\n}\n\ndeclare module '@core' {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Store<T> {\n __patches?: {\n value: T;\n version: string;\n history: HistoryEntry[];\n };\n }\n}\n\nexport interface SubscribePatchOptions extends SubscribeOptions, DiffOptions {\n /** @default false */\n runNow?: boolean;\n /** try to start from a specific version and only receive patches after that.\n * If the id is not found, it will start from the beginning */\n startAt?: string;\n}\n\nexport type InteropPatch = Patch | { op: 'add' | 'replace' | 'remove'; value?: any };\n\nconst genId = () => Math.random().toString(36).slice(2);\n\nexport function subscribePatches<T>(\n this: Store<T>,\n listener: (\n this: { cancel: Cancel },\n patches: Patch[],\n reversePatches: Patch[],\n version: string,\n previousVersion: string | undefined,\n ) => void,\n options: SubscribePatchOptions = {},\n): DisposableCancel {\n const patches = (this.__patches ??= {\n value: this.get(),\n version: genId(),\n history: [],\n });\n\n options = { ...options };\n options.runNow ??= false;\n let cursor = options.startAt ?? (options.runNow ? undefined : this.__patches.version);\n\n return this.subscribe(function (value) {\n if (patches.value !== value) {\n const result = diff(patches.value, value, options);\n patches.value = value;\n\n if (result[0].length > 0) {\n const newVersion = genId();\n\n patches.history = patches.history\n .concat({\n fromVersion: patches.version,\n toVersion: newVersion,\n patches: result[0],\n reversePatches: result[1],\n })\n .slice(-1000);\n\n patches.version = newVersion;\n }\n }\n\n if (cursor === patches.version) return;\n const index = patches.history.findIndex((h) => h.fromVersion === cursor);\n let forward, backward, previousVersion;\n\n if (index === -1) {\n [forward, backward] = diff(undefined, value, options);\n previousVersion = undefined;\n } else {\n forward = patches.history.slice(index).flatMap((h) => h.patches);\n backward = patches.history.slice(index).flatMap((h) => h.reversePatches);\n previousVersion = cursor;\n }\n\n cursor = patches.version;\n listener.apply(this, [forward, backward, cursor, previousVersion]);\n }, options);\n}\n\nexport function applyPatches<T>(this: Store<T>, patches: InteropPatch[]): void;\nexport function applyPatches<T>(this: Store<T>, ...patches: InteropPatch[]): void;\nexport function applyPatches<T>(\n this: Store<T>,\n ...patches: (InteropPatch | InteropPatch[])[]\n): void {\n this.set((value) => _applyPatches(value, ...(patches.flat() as Patch[])));\n}\n\nexport function sync<T>(\n this: Store<T>,\n listener: (syncMessage: SyncMessage) => void,\n options?: Omit<SubscribePatchOptions, 'runNow'>,\n): DisposableCancel {\n const debounce =\n typeof options?.debounce === 'object' && 'wait' in options.debounce\n ? { ...options.debounce }\n : options?.debounce !== undefined\n ? { wait: options.debounce }\n : undefined;\n\n if (debounce) {\n debounce.waitOnRunNow ??= false;\n }\n\n return subscribePatches.apply<\n Store<T>,\n Parameters<typeof subscribePatches<T>>,\n ReturnType<typeof subscribePatches<T>>\n >(this, [\n (patches, _, version, previousVersion) => {\n const trie = new Trie();\n\n patches = [...patches]\n .reverse()\n .filter((patch) => {\n if (trie.hasSubPath(patch.path)) {\n return false;\n }\n\n trie.add(patch.path);\n return true;\n })\n .reverse();\n\n listener({\n fromVersion: previousVersion,\n toVersion: version,\n patches: toExtendedJson(patches) as Patch[],\n });\n },\n { ...options, debounce, runNow: true },\n ]);\n}\n\nexport function acceptSync<T>(this: Store<T>, message: SyncMessage): void {\n if (message.fromVersion && message.fromVersion !== this.version) {\n throw new Error(\n `version mismatch! version=${this.version}, fromVersion=${message.fromVersion}`,\n );\n }\n\n const patches = fromExtendedJson(message.patches) as Patch[];\n\n this.version = message.toVersion;\n applyPatches.apply<Store<T>, Patch[], void>(this, patches);\n}\n\nexport const patchMethods: {\n subscribePatches: typeof subscribePatches;\n applyPatches: typeof applyPatches;\n sync: typeof sync;\n acceptSync: typeof acceptSync;\n} = {\n subscribePatches,\n applyPatches,\n sync,\n acceptSync,\n};\n"],"mappings":";;;;AAGA,SAAS,iBAAoB,QAAW,OAAiB;AACvD,KAAI,MAAM,OAAO,SACf,QAAOA,0BAAO,QAAQ,MAAM;AAG9B,QAAOC,uBAAI,QAAQ,MAAM,MAAa,MAAM;;AAG9C,SAAgB,aAAgB,QAAW,GAAG,SAAqB;AACjE,MAAK,MAAM,SAAS,QAClB,UAAS,iBAAiB,QAAQ;AAGpC,QAAO;;;;;ACHT,SAAgB,KACd,GACA,GACA,UAAuB,IACsB;CAC7C,MAAM,SAAS,CAAC,GAAG,MAAM,GAAG,GAA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;GAAS;GAAU;GAAQ;;IAChD;;AAKL,SAAgBC,eAEd,GAAG,SACG;AACN,MAAK,KAAK,UAAUC,aAAc,OAAO,GAAI,QAAQ;;AAGvD,SAAgB,KAEd,UACA,SACkB;CAClB,MAAM,WACJ,OAAO,SAAS,aAAa,YAAY,UAAU,QAAQ,WACvD,EAAE,GAAG,QAAQ,aACb,SAAS,aAAa,SACpB,EAAE,MAAM,QAAQ,aAChB;AAER,KAAI,SACF,UAAS,iBAAiB;AAG5B,QAAO,iBAAiB,MAItB,MAAM,EACL,SAAS,GAAG,SAAS,oBAAoB;EACxC,MAAM,OAAO,IAAI;AAEjB,YAAU,CAAC,GAAG,SACX,UACA,QAAQ,UAAU;AACjB,OAAI,KAAK,WAAW,MAAM,MACxB,QAAO;AAGT,QAAK,IAAI,MAAM;AACf,UAAO;KAER;AAEH,WAAS;GACP,aAAa;GACb,WAAW;GACX,SAASC,oCAAe;;IAG5B;EAAE,GAAG;EAAS;EAAU,QAAQ;;;AAIpC,SAAgB,WAA8B,SAA4B;AACxE,KAAI,QAAQ,eAAe,QAAQ,gBAAgB,KAAK,QACtD,OAAM,IAAI,MACR,6BAA6B,KAAK,QAAQ,gBAAgB,QAAQ;CAItE,MAAM,UAAUC,sCAAiB,QAAQ;AAEzC,MAAK,UAAU,QAAQ;AACvB,gBAAa,MAA+B,MAAM;;AAGpD,MAAaC,eAKT;CACF;CACA;CACA;CACA"}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { deepEqual, isObject, remove, set } from "./propAccess-DGchzVfy.js";
|
|
2
|
+
import { fromExtendedJson, toExtendedJson } from "./extendedJson-BZkQBXEv.js";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/applyPatches.ts
|
|
5
|
+
function applySinglePatch(target, patch) {
|
|
6
|
+
if (patch.op === "remove") return remove(target, patch.path);
|
|
7
|
+
return set(target, patch.path, patch.value);
|
|
8
|
+
}
|
|
9
|
+
function applyPatches(target, ...patches) {
|
|
10
|
+
for (const patch of patches) target = applySinglePatch(target, patch);
|
|
11
|
+
return target;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/lib/diff.ts
|
|
16
|
+
function diff(a, b, options = {}) {
|
|
17
|
+
const result = [..._diff(a, b, options)];
|
|
18
|
+
const patches = result.map(([patch]) => patch);
|
|
19
|
+
const reversePatches = result.map(([, reversePatch]) => reversePatch);
|
|
20
|
+
return [patches, reversePatches];
|
|
21
|
+
}
|
|
22
|
+
function* _diff(a, b, options, prefix = []) {
|
|
23
|
+
if (a === b) return;
|
|
24
|
+
if (typeof options.stopAt === "number" && prefix.length >= options.stopAt || typeof options.stopAt === "function" && options.stopAt(prefix)) {
|
|
25
|
+
if (deepEqual(a, b)) return;
|
|
26
|
+
return yield [{
|
|
27
|
+
op: "replace",
|
|
28
|
+
path: prefix,
|
|
29
|
+
value: b
|
|
30
|
+
}, {
|
|
31
|
+
op: "replace",
|
|
32
|
+
path: prefix,
|
|
33
|
+
value: a
|
|
34
|
+
}];
|
|
35
|
+
}
|
|
36
|
+
if (a instanceof Date && b instanceof Date) {
|
|
37
|
+
if (a.getTime() === b.getTime()) return;
|
|
38
|
+
return yield [{
|
|
39
|
+
op: "replace",
|
|
40
|
+
path: prefix,
|
|
41
|
+
value: b
|
|
42
|
+
}, {
|
|
43
|
+
op: "replace",
|
|
44
|
+
path: prefix,
|
|
45
|
+
value: a
|
|
46
|
+
}];
|
|
47
|
+
}
|
|
48
|
+
if (a instanceof Map && b instanceof Map) return yield* mapDiff(a, b, options, prefix);
|
|
49
|
+
if (a instanceof Set && b instanceof Set) {
|
|
50
|
+
if (deepEqual(a, b)) return;
|
|
51
|
+
return yield [{
|
|
52
|
+
op: "replace",
|
|
53
|
+
path: prefix,
|
|
54
|
+
value: b
|
|
55
|
+
}, {
|
|
56
|
+
op: "replace",
|
|
57
|
+
path: prefix,
|
|
58
|
+
value: a
|
|
59
|
+
}];
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
62
|
+
if (deepEqual(a, b)) return;
|
|
63
|
+
return yield [{
|
|
64
|
+
op: "replace",
|
|
65
|
+
path: prefix,
|
|
66
|
+
value: b
|
|
67
|
+
}, {
|
|
68
|
+
op: "replace",
|
|
69
|
+
path: prefix,
|
|
70
|
+
value: a
|
|
71
|
+
}];
|
|
72
|
+
}
|
|
73
|
+
if (isObject(a) && isObject(b) && !Array.isArray(a) && !Array.isArray(b)) return yield* objectDiff(a, b, options, prefix);
|
|
74
|
+
yield [{
|
|
75
|
+
op: "replace",
|
|
76
|
+
path: prefix,
|
|
77
|
+
value: b
|
|
78
|
+
}, {
|
|
79
|
+
op: "replace",
|
|
80
|
+
path: prefix,
|
|
81
|
+
value: a
|
|
82
|
+
}];
|
|
83
|
+
}
|
|
84
|
+
function* mapDiff(a, b, options, prefix) {
|
|
85
|
+
for (const [key, value] of a) if (!b.has(key)) yield [{
|
|
86
|
+
op: "remove",
|
|
87
|
+
path: [...prefix, key]
|
|
88
|
+
}, {
|
|
89
|
+
op: "add",
|
|
90
|
+
path: [...prefix, key],
|
|
91
|
+
value
|
|
92
|
+
}];
|
|
93
|
+
else yield* _diff(value, b.get(key), options, [...prefix, key]);
|
|
94
|
+
for (const [key, value] of b) if (!a.has(key)) yield [{
|
|
95
|
+
op: "add",
|
|
96
|
+
path: [...prefix, key],
|
|
97
|
+
value
|
|
98
|
+
}, {
|
|
99
|
+
op: "remove",
|
|
100
|
+
path: [...prefix, key]
|
|
101
|
+
}];
|
|
102
|
+
}
|
|
103
|
+
function* objectDiff(a, b, options, prefix) {
|
|
104
|
+
for (const [key, value] of Object.entries(a)) if (!(key in b)) yield [{
|
|
105
|
+
op: "remove",
|
|
106
|
+
path: [...prefix, key]
|
|
107
|
+
}, {
|
|
108
|
+
op: "add",
|
|
109
|
+
path: [...prefix, key],
|
|
110
|
+
value
|
|
111
|
+
}];
|
|
112
|
+
else yield* _diff(value, b[key], options, [...prefix, key]);
|
|
113
|
+
for (const [key, value] of Object.entries(b)) if (!(key in a)) yield [{
|
|
114
|
+
op: "add",
|
|
115
|
+
path: [...prefix, key],
|
|
116
|
+
value
|
|
117
|
+
}, {
|
|
118
|
+
op: "remove",
|
|
119
|
+
path: [...prefix, key]
|
|
120
|
+
}];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/lib/trie.ts
|
|
125
|
+
var TrieNode = class {
|
|
126
|
+
constructor() {
|
|
127
|
+
this.children = /* @__PURE__ */ new Map();
|
|
128
|
+
this.isLeaf = false;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var Trie = class {
|
|
132
|
+
constructor() {
|
|
133
|
+
this.root = new TrieNode();
|
|
134
|
+
}
|
|
135
|
+
add(path) {
|
|
136
|
+
let node = this.root;
|
|
137
|
+
for (const key of path) {
|
|
138
|
+
let next = node.children.get(key);
|
|
139
|
+
if (!next) node.children.set(key, next = new TrieNode());
|
|
140
|
+
node = next;
|
|
141
|
+
}
|
|
142
|
+
node.isLeaf = true;
|
|
143
|
+
}
|
|
144
|
+
hasSubPath(path) {
|
|
145
|
+
let node = this.root;
|
|
146
|
+
for (const key of path) {
|
|
147
|
+
const next = node.children.get(key);
|
|
148
|
+
if (!next) return false;
|
|
149
|
+
node = next;
|
|
150
|
+
}
|
|
151
|
+
return node.isLeaf;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/patches/patchMethods.ts
|
|
157
|
+
const genId = () => Math.random().toString(36).slice(2);
|
|
158
|
+
function subscribePatches(listener, options = {}) {
|
|
159
|
+
const patches = this.__patches ??= {
|
|
160
|
+
value: this.get(),
|
|
161
|
+
version: genId(),
|
|
162
|
+
history: []
|
|
163
|
+
};
|
|
164
|
+
options = { ...options };
|
|
165
|
+
options.runNow ??= false;
|
|
166
|
+
let cursor = options.startAt ?? (options.runNow ? void 0 : this.__patches.version);
|
|
167
|
+
return this.subscribe(function(value) {
|
|
168
|
+
if (patches.value !== value) {
|
|
169
|
+
const result = diff(patches.value, value, options);
|
|
170
|
+
patches.value = value;
|
|
171
|
+
if (result[0].length > 0) {
|
|
172
|
+
const newVersion = genId();
|
|
173
|
+
patches.history = patches.history.concat({
|
|
174
|
+
fromVersion: patches.version,
|
|
175
|
+
toVersion: newVersion,
|
|
176
|
+
patches: result[0],
|
|
177
|
+
reversePatches: result[1]
|
|
178
|
+
}).slice(-1e3);
|
|
179
|
+
patches.version = newVersion;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (cursor === patches.version) return;
|
|
183
|
+
const index = patches.history.findIndex((h) => h.fromVersion === cursor);
|
|
184
|
+
let forward, backward, previousVersion;
|
|
185
|
+
if (index === -1) {
|
|
186
|
+
[forward, backward] = diff(void 0, value, options);
|
|
187
|
+
previousVersion = void 0;
|
|
188
|
+
} else {
|
|
189
|
+
forward = patches.history.slice(index).flatMap((h) => h.patches);
|
|
190
|
+
backward = patches.history.slice(index).flatMap((h) => h.reversePatches);
|
|
191
|
+
previousVersion = cursor;
|
|
192
|
+
}
|
|
193
|
+
cursor = patches.version;
|
|
194
|
+
listener.apply(this, [
|
|
195
|
+
forward,
|
|
196
|
+
backward,
|
|
197
|
+
cursor,
|
|
198
|
+
previousVersion
|
|
199
|
+
]);
|
|
200
|
+
}, options);
|
|
201
|
+
}
|
|
202
|
+
function applyPatches$1(...patches) {
|
|
203
|
+
this.set((value) => applyPatches(value, ...patches.flat()));
|
|
204
|
+
}
|
|
205
|
+
function sync(listener, options) {
|
|
206
|
+
const debounce = typeof options?.debounce === "object" && "wait" in options.debounce ? { ...options.debounce } : options?.debounce !== void 0 ? { wait: options.debounce } : void 0;
|
|
207
|
+
if (debounce) debounce.waitOnRunNow ??= false;
|
|
208
|
+
return subscribePatches.apply(this, [(patches, _, version, previousVersion) => {
|
|
209
|
+
const trie = new Trie();
|
|
210
|
+
patches = [...patches].reverse().filter((patch) => {
|
|
211
|
+
if (trie.hasSubPath(patch.path)) return false;
|
|
212
|
+
trie.add(patch.path);
|
|
213
|
+
return true;
|
|
214
|
+
}).reverse();
|
|
215
|
+
listener({
|
|
216
|
+
fromVersion: previousVersion,
|
|
217
|
+
toVersion: version,
|
|
218
|
+
patches: toExtendedJson(patches)
|
|
219
|
+
});
|
|
220
|
+
}, {
|
|
221
|
+
...options,
|
|
222
|
+
debounce,
|
|
223
|
+
runNow: true
|
|
224
|
+
}]);
|
|
225
|
+
}
|
|
226
|
+
function acceptSync(message) {
|
|
227
|
+
if (message.fromVersion && message.fromVersion !== this.version) throw new Error(`version mismatch! version=${this.version}, fromVersion=${message.fromVersion}`);
|
|
228
|
+
const patches = fromExtendedJson(message.patches);
|
|
229
|
+
this.version = message.toVersion;
|
|
230
|
+
applyPatches$1.apply(this, patches);
|
|
231
|
+
}
|
|
232
|
+
const patchMethods = {
|
|
233
|
+
subscribePatches,
|
|
234
|
+
applyPatches: applyPatches$1,
|
|
235
|
+
sync,
|
|
236
|
+
acceptSync
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
//#endregion
|
|
240
|
+
export { applyPatches, diff, patchMethods, subscribePatches };
|
|
241
|
+
//# sourceMappingURL=patchMethods-DRPfkhAe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchMethods-DRPfkhAe.js","names":["applyPatches","_applyPatches","patchMethods: {\n subscribePatches: typeof subscribePatches;\n applyPatches: typeof applyPatches;\n sync: typeof sync;\n acceptSync: typeof acceptSync;\n}"],"sources":["../src/lib/applyPatches.ts","../src/lib/diff.ts","../src/lib/trie.ts","../src/patches/patchMethods.ts"],"sourcesContent":["import type { Patch } from './diff';\nimport { remove, set } from './propAccess';\n\nfunction applySinglePatch<T>(target: T, patch: Patch): T {\n if (patch.op === 'remove') {\n return remove(target, patch.path as any);\n }\n\n return set(target, patch.path as any, patch.value);\n}\n\nexport function applyPatches<T>(target: T, ...patches: Patch[]): T {\n for (const patch of patches) {\n target = applySinglePatch(target, patch);\n }\n\n return target;\n}\n","import { isObject } from '@lib/helpers';\nimport { deepEqual } from './equals';\nimport type { KeyType } from './path';\n\nexport type Patch =\n | { op: 'add'; path: KeyType[]; value: any }\n | { op: 'remove'; path: KeyType[] }\n | { op: 'replace'; path: KeyType[]; value: any };\n\nexport interface DiffOptions {\n stopAt?: number | ((path: KeyType[]) => boolean);\n}\n\nexport function diff(\n a: any,\n b: any,\n options: DiffOptions = {},\n): [patches: Patch[], reversePatches: Patch[]] {\n const result = [..._diff(a, b, options)];\n const patches = result.map(([patch]) => patch);\n const reversePatches = result.map(([, reversePatch]) => reversePatch);\n\n return [patches, reversePatches];\n}\n\nfunction* _diff(\n a: any,\n b: any,\n options: DiffOptions,\n prefix: KeyType[] = [],\n): Iterable<[patch: Patch, reversePatch: Patch]> {\n if (a === b) {\n return;\n }\n\n if (\n (typeof options.stopAt === 'number' && prefix.length >= options.stopAt) ||\n (typeof options.stopAt === 'function' && options.stopAt(prefix))\n ) {\n if (deepEqual(a, b)) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (a instanceof Date && b instanceof Date) {\n if (a.getTime() === b.getTime()) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (a instanceof Map && b instanceof Map) {\n return yield* mapDiff(a, b, options, prefix);\n }\n\n if (a instanceof Set && b instanceof Set) {\n if (deepEqual(a, b)) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (Array.isArray(a) && Array.isArray(b)) {\n if (deepEqual(a, b)) {\n return;\n }\n\n return yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n }\n\n if (isObject(a) && isObject(b) && !Array.isArray(a) && !Array.isArray(b)) {\n return yield* objectDiff(a, b, options, prefix);\n }\n\n yield [\n { op: 'replace', path: prefix, value: b },\n { op: 'replace', path: prefix, value: a },\n ];\n}\n\nfunction* mapDiff(\n a: Map<any, any>,\n b: Map<any, any>,\n options: { stopAt?: number | ((path: KeyType[]) => boolean) },\n prefix: KeyType[],\n): Iterable<[patch: Patch, reversePatch: Patch]> {\n for (const [key, value] of a) {\n if (!b.has(key)) {\n yield [\n { op: 'remove', path: [...prefix, key] },\n { op: 'add', path: [...prefix, key], value },\n ];\n } else {\n yield* _diff(value, b.get(key), options, [...prefix, key]);\n }\n }\n\n for (const [key, value] of b) {\n if (!a.has(key)) {\n yield [\n { op: 'add', path: [...prefix, key], value },\n { op: 'remove', path: [...prefix, key] },\n ];\n }\n }\n}\n\nfunction* objectDiff(\n a: any,\n b: any,\n options: { stopAt?: number | ((path: KeyType[]) => boolean) },\n prefix: KeyType[],\n): Iterable<[patch: Patch, reversePatch: Patch]> {\n for (const [key, value] of Object.entries(a)) {\n if (!(key in b)) {\n yield [\n { op: 'remove', path: [...prefix, key] },\n { op: 'add', path: [...prefix, key], value },\n ];\n } else {\n yield* _diff(value, b[key], options, [...prefix, key]);\n }\n }\n\n for (const [key, value] of Object.entries(b)) {\n if (!(key in a)) {\n yield [\n { op: 'add', path: [...prefix, key], value },\n { op: 'remove', path: [...prefix, key] },\n ];\n }\n }\n}\n","import type { KeyType } from '@lib/path';\n\nclass TrieNode {\n children: Map<KeyType, TrieNode> = new Map();\n isLeaf = false;\n}\n\nexport class Trie {\n root: TrieNode = new TrieNode();\n\n add(path: KeyType[]): void {\n let node = this.root;\n for (const key of path) {\n let next = node.children.get(key);\n if (!next) node.children.set(key, (next = new TrieNode()));\n node = next;\n }\n node.isLeaf = true;\n }\n\n hasSubPath(path: KeyType[]): boolean {\n let node = this.root;\n for (const key of path) {\n const next = node.children.get(key);\n if (!next) return false;\n node = next;\n }\n return node.isLeaf;\n }\n}\n","import type { Cancel, DisposableCancel, SubscribeOptions } from '@core/commonTypes';\nimport type { Store } from '@core/store';\nimport { applyPatches as _applyPatches } from '@lib/applyPatches';\nimport { diff, type DiffOptions, type Patch } from '@lib/diff';\nimport { fromExtendedJson, toExtendedJson } from '@lib/extendedJson';\nimport { Trie } from '@lib/trie';\n\nexport interface SyncMessage {\n fromVersion?: string;\n toVersion: string;\n patches: Patch[];\n}\n\nexport interface HistoryEntry extends SyncMessage {\n reversePatches: Patch[];\n}\n\ndeclare module '@core' {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Store<T> {\n __patches?: {\n value: T;\n version: string;\n history: HistoryEntry[];\n };\n }\n}\n\nexport interface SubscribePatchOptions extends SubscribeOptions, DiffOptions {\n /** @default false */\n runNow?: boolean;\n /** try to start from a specific version and only receive patches after that.\n * If the id is not found, it will start from the beginning */\n startAt?: string;\n}\n\nexport type InteropPatch = Patch | { op: 'add' | 'replace' | 'remove'; value?: any };\n\nconst genId = () => Math.random().toString(36).slice(2);\n\nexport function subscribePatches<T>(\n this: Store<T>,\n listener: (\n this: { cancel: Cancel },\n patches: Patch[],\n reversePatches: Patch[],\n version: string,\n previousVersion: string | undefined,\n ) => void,\n options: SubscribePatchOptions = {},\n): DisposableCancel {\n const patches = (this.__patches ??= {\n value: this.get(),\n version: genId(),\n history: [],\n });\n\n options = { ...options };\n options.runNow ??= false;\n let cursor = options.startAt ?? (options.runNow ? undefined : this.__patches.version);\n\n return this.subscribe(function (value) {\n if (patches.value !== value) {\n const result = diff(patches.value, value, options);\n patches.value = value;\n\n if (result[0].length > 0) {\n const newVersion = genId();\n\n patches.history = patches.history\n .concat({\n fromVersion: patches.version,\n toVersion: newVersion,\n patches: result[0],\n reversePatches: result[1],\n })\n .slice(-1000);\n\n patches.version = newVersion;\n }\n }\n\n if (cursor === patches.version) return;\n const index = patches.history.findIndex((h) => h.fromVersion === cursor);\n let forward, backward, previousVersion;\n\n if (index === -1) {\n [forward, backward] = diff(undefined, value, options);\n previousVersion = undefined;\n } else {\n forward = patches.history.slice(index).flatMap((h) => h.patches);\n backward = patches.history.slice(index).flatMap((h) => h.reversePatches);\n previousVersion = cursor;\n }\n\n cursor = patches.version;\n listener.apply(this, [forward, backward, cursor, previousVersion]);\n }, options);\n}\n\nexport function applyPatches<T>(this: Store<T>, patches: InteropPatch[]): void;\nexport function applyPatches<T>(this: Store<T>, ...patches: InteropPatch[]): void;\nexport function applyPatches<T>(\n this: Store<T>,\n ...patches: (InteropPatch | InteropPatch[])[]\n): void {\n this.set((value) => _applyPatches(value, ...(patches.flat() as Patch[])));\n}\n\nexport function sync<T>(\n this: Store<T>,\n listener: (syncMessage: SyncMessage) => void,\n options?: Omit<SubscribePatchOptions, 'runNow'>,\n): DisposableCancel {\n const debounce =\n typeof options?.debounce === 'object' && 'wait' in options.debounce\n ? { ...options.debounce }\n : options?.debounce !== undefined\n ? { wait: options.debounce }\n : undefined;\n\n if (debounce) {\n debounce.waitOnRunNow ??= false;\n }\n\n return subscribePatches.apply<\n Store<T>,\n Parameters<typeof subscribePatches<T>>,\n ReturnType<typeof subscribePatches<T>>\n >(this, [\n (patches, _, version, previousVersion) => {\n const trie = new Trie();\n\n patches = [...patches]\n .reverse()\n .filter((patch) => {\n if (trie.hasSubPath(patch.path)) {\n return false;\n }\n\n trie.add(patch.path);\n return true;\n })\n .reverse();\n\n listener({\n fromVersion: previousVersion,\n toVersion: version,\n patches: toExtendedJson(patches) as Patch[],\n });\n },\n { ...options, debounce, runNow: true },\n ]);\n}\n\nexport function acceptSync<T>(this: Store<T>, message: SyncMessage): void {\n if (message.fromVersion && message.fromVersion !== this.version) {\n throw new Error(\n `version mismatch! version=${this.version}, fromVersion=${message.fromVersion}`,\n );\n }\n\n const patches = fromExtendedJson(message.patches) as Patch[];\n\n this.version = message.toVersion;\n applyPatches.apply<Store<T>, Patch[], void>(this, patches);\n}\n\nexport const patchMethods: {\n subscribePatches: typeof subscribePatches;\n applyPatches: typeof applyPatches;\n sync: typeof sync;\n acceptSync: typeof acceptSync;\n} = {\n subscribePatches,\n applyPatches,\n sync,\n acceptSync,\n};\n"],"mappings":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|
1
|
+
import { Cancel, DisposableCancel, Store, SubscribeOptions } from "./store-DKaeE840.js";
|
|
2
|
+
import { DiffOptions, Patch } from "./diff-gZezL04N.js";
|
|
3
|
+
|
|
4
|
+
//#region src/patches/patchMethods.d.ts
|
|
5
|
+
interface SyncMessage {
|
|
6
|
+
fromVersion?: string;
|
|
7
|
+
toVersion: string;
|
|
8
|
+
patches: Patch[];
|
|
9
|
+
}
|
|
10
|
+
interface HistoryEntry extends SyncMessage {
|
|
11
|
+
reversePatches: Patch[];
|
|
12
|
+
}
|
|
13
|
+
declare module "@core" {
|
|
14
|
+
interface Store<T> {
|
|
15
|
+
__patches?: {
|
|
16
|
+
value: T;
|
|
17
|
+
version: string;
|
|
18
|
+
history: HistoryEntry[];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
interface SubscribePatchOptions extends SubscribeOptions, DiffOptions {
|
|
23
|
+
/** @default false */
|
|
24
|
+
runNow?: boolean;
|
|
25
|
+
/** try to start from a specific version and only receive patches after that.
|
|
26
|
+
* If the id is not found, it will start from the beginning */
|
|
27
|
+
startAt?: string;
|
|
28
|
+
}
|
|
29
|
+
type InteropPatch = Patch | {
|
|
30
|
+
op: "add" | "replace" | "remove";
|
|
31
|
+
value?: any;
|
|
32
|
+
};
|
|
33
|
+
declare function subscribePatches<T>(this: Store<T>, listener: (this: {
|
|
34
|
+
cancel: Cancel;
|
|
35
|
+
}, patches: Patch[], reversePatches: Patch[], version: string, previousVersion: string | undefined) => void, options?: SubscribePatchOptions): DisposableCancel;
|
|
36
|
+
declare function applyPatches<T>(this: Store<T>, patches: InteropPatch[]): void;
|
|
37
|
+
declare function applyPatches<T>(this: Store<T>, ...patches: InteropPatch[]): void;
|
|
38
|
+
declare function sync<T>(this: Store<T>, listener: (syncMessage: SyncMessage) => void, options?: Omit<SubscribePatchOptions, "runNow">): DisposableCancel;
|
|
39
|
+
declare function acceptSync<T>(this: Store<T>, message: SyncMessage): void;
|
|
40
|
+
declare const patchMethods: {
|
|
41
|
+
subscribePatches: typeof subscribePatches;
|
|
42
|
+
applyPatches: typeof applyPatches;
|
|
43
|
+
sync: typeof sync;
|
|
44
|
+
acceptSync: typeof acceptSync;
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { SubscribePatchOptions, SyncMessage, patchMethods };
|
|
48
|
+
//# sourceMappingURL=patchMethods-Dxad6HF6.d.ts.map
|