cross-state 0.34.3 → 0.36.0

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.
Files changed (84) hide show
  1. package/dist/cjs/diff.cjs +92 -0
  2. package/dist/cjs/diff.cjs.map +1 -0
  3. package/dist/cjs/index.cjs +15 -143
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs/{immer → mutative}/index.cjs +2 -2
  6. package/dist/cjs/mutative/register.cjs +5 -0
  7. package/dist/cjs/mutative/register.cjs.map +1 -0
  8. package/dist/cjs/mutativeMethods.cjs +26 -0
  9. package/dist/cjs/mutativeMethods.cjs.map +1 -0
  10. package/dist/cjs/patches/index.cjs +53 -0
  11. package/dist/cjs/patches/index.cjs.map +1 -0
  12. package/dist/cjs/patches/register.cjs +34 -0
  13. package/dist/cjs/patches/register.cjs.map +1 -0
  14. package/dist/cjs/propAccess.cjs +151 -0
  15. package/dist/cjs/propAccess.cjs.map +1 -0
  16. package/dist/cjs/react/index.cjs +11 -10
  17. package/dist/cjs/react/index.cjs.map +1 -1
  18. package/dist/cjs/scope.cjs +8 -3
  19. package/dist/cjs/scope.cjs.map +1 -1
  20. package/dist/cjs/store.cjs +26 -167
  21. package/dist/cjs/store.cjs.map +1 -1
  22. package/dist/cjs/useCache.cjs +3 -2
  23. package/dist/cjs/useCache.cjs.map +1 -1
  24. package/dist/es/diff.mjs +93 -0
  25. package/dist/es/diff.mjs.map +1 -0
  26. package/dist/es/index.mjs +18 -144
  27. package/dist/es/index.mjs.map +1 -1
  28. package/dist/es/mutative/index.mjs +5 -0
  29. package/dist/es/mutative/register.mjs +4 -0
  30. package/dist/es/mutative/register.mjs.map +1 -0
  31. package/dist/es/mutativeMethods.mjs +27 -0
  32. package/dist/es/mutativeMethods.mjs.map +1 -0
  33. package/dist/es/patches/index.mjs +53 -0
  34. package/dist/es/patches/index.mjs.map +1 -0
  35. package/dist/es/patches/register.mjs +34 -0
  36. package/dist/es/patches/register.mjs.map +1 -0
  37. package/dist/es/propAccess.mjs +152 -0
  38. package/dist/es/propAccess.mjs.map +1 -0
  39. package/dist/es/react/index.mjs +2 -1
  40. package/dist/es/react/index.mjs.map +1 -1
  41. package/dist/es/scope.mjs +8 -3
  42. package/dist/es/scope.mjs.map +1 -1
  43. package/dist/es/store.mjs +23 -164
  44. package/dist/es/store.mjs.map +1 -1
  45. package/dist/es/useCache.mjs +2 -1
  46. package/dist/es/useCache.mjs.map +1 -1
  47. package/dist/types/core/cache.d.ts +1 -1
  48. package/dist/types/core/commonTypes.d.ts +3 -0
  49. package/dist/types/core/store.d.ts +1 -0
  50. package/dist/types/index.d.ts +0 -1
  51. package/dist/types/lib/autobind.d.ts +1 -1
  52. package/dist/types/lib/diff.d.ts +3 -2
  53. package/dist/types/lib/isPromise.d.ts +1 -0
  54. package/dist/types/lib/promiseWithState.d.ts +1 -1
  55. package/dist/types/mutative/index.d.ts +1 -0
  56. package/dist/types/mutative/mutativeMethods.d.ts +10 -0
  57. package/dist/types/mutative/register.d.ts +7 -0
  58. package/dist/types/patches/index.d.ts +1 -0
  59. package/dist/types/patches/patchMethods.d.ts +33 -0
  60. package/dist/types/patches/register.d.ts +9 -0
  61. package/dist/types/react/form/customInput.d.ts +1 -1
  62. package/dist/types/react/form/form.d.ts +2 -2
  63. package/dist/types/react/form/formField.d.ts +2 -2
  64. package/dist/types/react/form/formForEach.d.ts +1 -1
  65. package/dist/types/react/loadingBoundary.d.ts +1 -1
  66. package/dist/types/react/register.d.ts +1 -1
  67. package/dist/types/react/scope.d.ts +1 -1
  68. package/package.json +58 -33
  69. package/dist/cjs/immer/register.cjs +0 -5
  70. package/dist/cjs/immer/register.cjs.map +0 -1
  71. package/dist/cjs/immerMethods.cjs +0 -23
  72. package/dist/cjs/immerMethods.cjs.map +0 -1
  73. package/dist/es/immer/index.mjs +0 -5
  74. package/dist/es/immer/register.mjs +0 -4
  75. package/dist/es/immer/register.mjs.map +0 -1
  76. package/dist/es/immerMethods.mjs +0 -24
  77. package/dist/es/immerMethods.mjs.map +0 -1
  78. package/dist/types/immer/immerMethods.d.ts +0 -10
  79. package/dist/types/immer/index.d.ts +0 -1
  80. package/dist/types/immer/register.d.ts +0 -7
  81. package/dist/types/sync/index.d.ts +0 -1
  82. package/dist/types/sync/sync.d.ts +0 -17
  83. /package/dist/cjs/{immer → mutative}/index.cjs.map +0 -0
  84. /package/dist/es/{immer → mutative}/index.mjs.map +0 -0
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ const propAccess = require("./propAccess.cjs");
3
+ function applySinglePatch(target, patch) {
4
+ if (patch.op === "remove") {
5
+ return propAccess.remove(target, patch.path);
6
+ }
7
+ return propAccess.set(target, patch.path, patch.value);
8
+ }
9
+ function applyPatches(target, ...patches) {
10
+ for (const patch of patches) {
11
+ target = applySinglePatch(target, patch);
12
+ }
13
+ return target;
14
+ }
15
+ function diff(a, b, options = {}) {
16
+ const result = [..._diff(a, b, options)];
17
+ const patches = result.map(([patch]) => patch);
18
+ const reversePatches = result.map(([, reversePatch]) => reversePatch);
19
+ return [patches, reversePatches];
20
+ }
21
+ function* _diff(a, b, options, prefix = []) {
22
+ if (a === b) {
23
+ return;
24
+ }
25
+ if (typeof options.stopAt === "number" && prefix.length >= options.stopAt || typeof options.stopAt === "function" && options.stopAt(prefix)) {
26
+ if (propAccess.deepEqual(a, b)) {
27
+ return;
28
+ }
29
+ return yield [
30
+ { op: "replace", path: prefix, value: b },
31
+ { op: "replace", path: prefix, value: a }
32
+ ];
33
+ }
34
+ if (a instanceof Map && b instanceof Map) {
35
+ return yield* mapDiff(a, b, options, prefix);
36
+ }
37
+ if (a instanceof Set && b instanceof Set) {
38
+ a = [...a];
39
+ b = [...b];
40
+ }
41
+ if (a instanceof Object && b instanceof Object && Array.isArray(a) === Array.isArray(b)) {
42
+ return yield* objectDiff(a, b, options, prefix);
43
+ }
44
+ yield [
45
+ { op: "replace", path: prefix, value: b },
46
+ { op: "replace", path: prefix, value: a }
47
+ ];
48
+ }
49
+ function* mapDiff(a, b, options, prefix) {
50
+ for (const [key, value] of a) {
51
+ if (!b.has(key)) {
52
+ yield [
53
+ { op: "remove", path: [...prefix, key] },
54
+ { op: "add", path: [...prefix, key], value }
55
+ ];
56
+ } else {
57
+ yield* _diff(value, b.get(key), options, [...prefix, key]);
58
+ }
59
+ }
60
+ for (const [key, value] of b) {
61
+ if (!a.has(key)) {
62
+ yield [
63
+ { op: "add", path: [...prefix, key], value },
64
+ { op: "remove", path: [...prefix, key] }
65
+ ];
66
+ }
67
+ }
68
+ }
69
+ function* objectDiff(a, b, options, prefix) {
70
+ const castKey = (key) => Array.isArray(a) ? Number(key) : key;
71
+ for (const [key, value] of Object.entries(a)) {
72
+ if (!(key in b)) {
73
+ yield [
74
+ { op: "remove", path: [...prefix, castKey(key)] },
75
+ { op: "add", path: [...prefix, castKey(key)], value }
76
+ ];
77
+ } else {
78
+ yield* _diff(value, b[key], options, [...prefix, castKey(key)]);
79
+ }
80
+ }
81
+ for (const [key, value] of Object.entries(b)) {
82
+ if (!(key in a)) {
83
+ yield [
84
+ { op: "add", path: [...prefix, castKey(key)], value },
85
+ { op: "remove", path: [...prefix, castKey(key)] }
86
+ ];
87
+ }
88
+ }
89
+ }
90
+ exports.applyPatches = applyPatches;
91
+ exports.diff = diff;
92
+ //# sourceMappingURL=diff.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diff.cjs","sources":["../../src/lib/applyPatches.ts","../../src/lib/diff.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 { 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 Map && b instanceof Map) {\n return yield* mapDiff(a, b, options, prefix);\n }\n\n if (a instanceof Set && b instanceof Set) {\n a = [...a];\n b = [...b];\n }\n\n if (a instanceof Object && b instanceof Object && 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 const castKey = (key: string) => (Array.isArray(a) ? Number(key) : key);\n\n for (const [key, value] of Object.entries(a)) {\n if (!(key in b)) {\n yield [\n { op: 'remove', path: [...prefix, castKey(key)] },\n { op: 'add', path: [...prefix, castKey(key)], value },\n ];\n } else {\n yield* _diff(value, b[key], options, [...prefix, castKey(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, castKey(key)], value },\n { op: 'remove', path: [...prefix, castKey(key)] },\n ];\n }\n }\n}\n"],"names":["remove","set","deepEqual"],"mappings":";;AAGA,SAAS,iBAAoB,QAAW,OAAiB;AACnD,MAAA,MAAM,OAAO,UAAU;AAClB,WAAAA,kBAAO,QAAQ,MAAM,IAAW;AAAA,EACzC;AAEA,SAAOC,WAAAA,IAAI,QAAQ,MAAM,MAAa,MAAM,KAAK;AACnD;AAEgB,SAAA,aAAgB,WAAc,SAAqB;AACjE,aAAW,SAAS,SAAS;AAClB,aAAA,iBAAiB,QAAQ,KAAK;AAAA,EACzC;AAEO,SAAA;AACT;ACLO,SAAS,KACd,GACA,GACA,UAAuB,CAAA,GACsB;AAC7C,QAAM,SAAS,CAAC,GAAG,MAAM,GAAG,GAAG,OAAO,CAAC;AACvC,QAAM,UAAU,OAAO,IAAI,CAAC,CAAC,KAAK,MAAM,KAAK;AACvC,QAAA,iBAAiB,OAAO,IAAI,CAAC,CAAA,EAAG,YAAY,MAAM,YAAY;AAE7D,SAAA,CAAC,SAAS,cAAc;AACjC;AAEA,UAAU,MACR,GACA,GACA,SACA,SAAoB,CAAA,GAC2B;AAC/C,MAAI,MAAM,GAAG;AACX;AAAA,EACF;AAEA,MACG,OAAO,QAAQ,WAAW,YAAY,OAAO,UAAU,QAAQ,UAC/D,OAAO,QAAQ,WAAW,cAAc,QAAQ,OAAO,MAAM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ACJ,EAAE,IAAI,OAAO,MAAM,CAAC,GAAG,QAAQ,GAAG,GAAG,MAAM;AAAA,QAC3C,EAAE,IAAI,UAAU,MAAM,CAAC,GAAG,QAAQ,GAAG,EAAE;AAAA,MAAA;AAAA,IAE3C;AAAA,EACF;AACF;AAEA,UAAU,WACR,GACA,GACA,SACA,QAC+C;AACzC,QAAA,UAAU,CAAC,QAAiB,MAAM,QAAQ,CAAC,IAAI,OAAO,GAAG,IAAI;AAEnE,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,CAAC,GAAG;AACxC,QAAA,EAAE,OAAO,IAAI;AACT,YAAA;AAAA,QACJ,EAAE,IAAI,UAAU,MAAM,CAAC,GAAG,QAAQ,QAAQ,GAAG,CAAC,EAAE;AAAA,QAChD,EAAE,IAAI,OAAO,MAAM,CAAC,GAAG,QAAQ,QAAQ,GAAG,CAAC,GAAG,MAAM;AAAA,MAAA;AAAA,IACtD,OACK;AACL,aAAO,MAAM,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC,GAAG,QAAQ,QAAQ,GAAG,CAAC,CAAC;AAAA,IAChE;AAAA,EACF;AAEA,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,CAAC,GAAG;AACxC,QAAA,EAAE,OAAO,IAAI;AACT,YAAA;AAAA,QACJ,EAAE,IAAI,OAAO,MAAM,CAAC,GAAG,QAAQ,QAAQ,GAAG,CAAC,GAAG,MAAM;AAAA,QACpD,EAAE,IAAI,UAAU,MAAM,CAAC,GAAG,QAAQ,QAAQ,GAAG,CAAC,EAAE;AAAA,MAAA;AAAA,IAEpD;AAAA,EACF;AACF;;;"}
@@ -3,93 +3,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const scope = require("./scope.cjs");
4
4
  const store = require("./store.cjs");
5
5
  const urlStore = require("./urlStore.cjs");
6
- function applySinglePatch(target, patch) {
7
- if (patch.op === "remove") {
8
- return store.remove(target, patch.path);
9
- }
10
- return store.set(target, patch.path, patch.value);
11
- }
12
- function applyPatches(target, ...patches) {
13
- for (const patch of patches) {
14
- target = applySinglePatch(target, patch);
15
- }
16
- return target;
17
- }
18
- function diff(a, b, options = {}) {
19
- const result = [..._diff(a, b, options)];
20
- const patches = result.map(([patch]) => patch);
21
- const reversePatches = result.map(([, reversePatch]) => reversePatch);
22
- return [patches, reversePatches];
23
- }
24
- function* _diff(a, b, options, prefix = []) {
25
- if (a === b) {
26
- return;
27
- }
28
- if (typeof options.stopAt === "number" && prefix.length >= options.stopAt || typeof options.stopAt === "function" && options.stopAt(prefix)) {
29
- if (store.deepEqual(a, b)) {
30
- return;
31
- }
32
- return yield [
33
- { op: "replace", path: prefix, value: b },
34
- { op: "replace", path: prefix, value: a }
35
- ];
36
- }
37
- if (a instanceof Map && b instanceof Map) {
38
- return yield* mapDiff(a, b, options, prefix);
39
- }
40
- if (a instanceof Set && b instanceof Set) {
41
- a = [...a];
42
- b = [...b];
43
- }
44
- if (a instanceof Object && b instanceof Object && Array.isArray(a) === Array.isArray(b)) {
45
- return yield* objectDiff(a, b, options, prefix);
46
- }
47
- yield [
48
- { op: "replace", path: prefix, value: b },
49
- { op: "replace", path: prefix, value: a }
50
- ];
51
- }
52
- function* mapDiff(a, b, options, prefix) {
53
- for (const [key, value] of a) {
54
- if (!b.has(key)) {
55
- yield [
56
- { op: "remove", path: [...prefix, key] },
57
- { op: "add", path: [...prefix, key], value }
58
- ];
59
- } else {
60
- yield* _diff(value, b.get(key), options, [...prefix, key]);
61
- }
62
- }
63
- for (const [key, value] of b) {
64
- if (!a.has(key)) {
65
- yield [
66
- { op: "add", path: [...prefix, key], value },
67
- { op: "remove", path: [...prefix, key] }
68
- ];
69
- }
70
- }
71
- }
72
- function* objectDiff(a, b, options, prefix) {
73
- const castKey = (key) => Array.isArray(a) ? Number(key) : key;
74
- for (const [key, value] of Object.entries(a)) {
75
- if (!(key in b)) {
76
- yield [
77
- { op: "remove", path: [...prefix, castKey(key)] },
78
- { op: "add", path: [...prefix, castKey(key)], value }
79
- ];
80
- } else {
81
- yield* _diff(value, b[key], options, [...prefix, castKey(key)]);
82
- }
83
- }
84
- for (const [key, value] of Object.entries(b)) {
85
- if (!(key in a)) {
86
- yield [
87
- { op: "add", path: [...prefix, castKey(key)], value },
88
- { op: "remove", path: [...prefix, castKey(key)] }
89
- ];
90
- }
91
- }
92
- }
6
+ const diff = require("./diff.cjs");
7
+ const propAccess = require("./propAccess.cjs");
93
8
  function findOrDefault(array, predicate, defaultValue) {
94
9
  const index = array.findIndex(predicate);
95
10
  if (index >= 0) {
@@ -154,11 +69,11 @@ class Persist {
154
69
  this.prefix = `${options.id}:`;
155
70
  this.paths = (options.paths ?? []).map((p) => {
156
71
  if (isPlainPath(p)) {
157
- return { path: store.castArrayPath(p) };
72
+ return { path: propAccess.castArrayPath(p) };
158
73
  }
159
74
  const _p = p;
160
75
  return {
161
- path: store.castArrayPath(_p.path),
76
+ path: propAccess.castArrayPath(_p.path),
162
77
  throttleMs: _p.throttleMs
163
78
  };
164
79
  }).sort((a, b) => b.path.length - a.path.length);
@@ -175,10 +90,10 @@ class Persist {
175
90
  let committed = this.store.get();
176
91
  const cancel = this.store.subscribe(
177
92
  (value) => {
178
- const [patches] = diff(committed, value);
93
+ const [patches] = diff.diff(committed, value);
179
94
  committed = value;
180
95
  for (const patch of patches) {
181
- if (this.updateInProgress && store.shallowEqual(this.updateInProgress[0], patch.path) && this.updateInProgress[1] === (patch.op === "remove" ? void 0 : patch.value)) {
96
+ if (this.updateInProgress && propAccess.shallowEqual(this.updateInProgress[0], patch.path) && this.updateInProgress[1] === (patch.op === "remove" ? void 0 : patch.value)) {
182
97
  continue;
183
98
  }
184
99
  const ancestor = this.paths.find((p) => isAncestor(p.path, patch.path));
@@ -245,13 +160,13 @@ class Persist {
245
160
  }
246
161
  const parsedValue = value === "undefined" ? void 0 : JSON.parse(value);
247
162
  this.updateInProgress = [path, parsedValue];
248
- this.store.set((state) => store.set(state, path, parsedValue));
163
+ this.store.set((state) => propAccess.set(state, path, parsedValue));
249
164
  this.updateInProgress = void 0;
250
165
  });
251
166
  }
252
167
  save(path) {
253
168
  const key = this.buildKey(path);
254
- const value = store.get(this.store.get(), path);
169
+ const value = propAccess.get(this.store.get(), path);
255
170
  const serializedValue = value === void 0 ? "undefined" : JSON.stringify(value);
256
171
  return maybeAsync(this.storage.setItem(key, serializedValue), () => {
257
172
  this.channel.postMessage(path);
@@ -279,47 +194,6 @@ function persist(store2, options) {
279
194
  function isPlainPath(p) {
280
195
  return typeof p === "string" || Array.isArray(p);
281
196
  }
282
- const genId = () => Math.random().toString(36).slice(2);
283
- class Sync {
284
- constructor(store2) {
285
- this.store = store2;
286
- this.previousId = genId();
287
- this.previousState = this.store.get();
288
- this.patchStream = this.store.map((state) => {
289
- const id = genId();
290
- const previousId = this.previousId;
291
- const patches = diff(this.previousState, state)[0];
292
- this.previousId = id;
293
- this.previousState = state;
294
- return { id, previousId, patches };
295
- });
296
- this.patchStream.addEffect(() => {
297
- this.previousId = genId();
298
- this.previousState = this.store.get();
299
- });
300
- }
301
- connectToClient(listener) {
302
- const cancel = this.patchStream.subscribe(listener, { runNow: false });
303
- listener({
304
- id: this.previousId,
305
- patches: [{ op: "replace", path: [], value: this.previousState }]
306
- });
307
- return cancel;
308
- }
309
- async connectToServer(stream) {
310
- let previousId;
311
- for await (const message of stream) {
312
- if (message.previousId && message.previousId !== previousId) {
313
- throw new Error("previousId mismatch");
314
- }
315
- previousId = message.id;
316
- this.store.set((state) => applyPatches(state, ...message.patches));
317
- }
318
- }
319
- }
320
- function createSync(store2) {
321
- return new Sync(store2);
322
- }
323
197
  exports.Cache = scope.Cache;
324
198
  exports.InstanceCache = scope.InstanceCache;
325
199
  exports.ResourceGroup = scope.ResourceGroup;
@@ -332,21 +206,19 @@ exports.Store = store.Store;
332
206
  exports.arrayMethods = store.arrayMethods;
333
207
  exports.calcDuration = store.calcDuration;
334
208
  exports.createStore = store.createStore;
335
- exports.deepEqual = store.deepEqual;
336
- exports.get = store.get;
337
209
  exports.mapMethods = store.mapMethods;
338
210
  exports.recordMethods = store.recordMethods;
339
- exports.set = store.set;
340
211
  exports.setMethods = store.setMethods;
341
- exports.shallowEqual = store.shallowEqual;
342
- exports.strictEqual = store.strictEqual;
343
212
  exports.connectUrl = urlStore.connectUrl;
344
213
  exports.createUrlStore = urlStore.createUrlStore;
345
214
  exports.updateUrlStore = urlStore.updateUrlStore;
346
- exports.Sync = Sync;
347
- exports.applyPatches = applyPatches;
348
- exports.createSync = createSync;
349
- exports.diff = diff;
215
+ exports.applyPatches = diff.applyPatches;
216
+ exports.diff = diff.diff;
217
+ exports.deepEqual = propAccess.deepEqual;
218
+ exports.get = propAccess.get;
219
+ exports.set = propAccess.set;
220
+ exports.shallowEqual = propAccess.shallowEqual;
221
+ exports.strictEqual = propAccess.strictEqual;
350
222
  exports.findOrDefault = findOrDefault;
351
223
  exports.persist = persist;
352
224
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/lib/applyPatches.ts","../../src/lib/diff.ts","../../src/lib/updateHelpers.ts","../../src/persist/persistPathHelpers.ts","../../src/lib/maybeAsync.ts","../../src/persist/persistStorage.ts","../../src/persist/persist.ts","../../src/sync/sync.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 { 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 function diff(\n a: any,\n b: any,\n options: { stopAt?: number | ((path: KeyType[]) => boolean) } = {},\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: { stopAt?: number | ((path: KeyType[]) => boolean) },\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 Map && b instanceof Map) {\n return yield* mapDiff(a, b, options, prefix);\n }\n\n if (a instanceof Set && b instanceof Set) {\n a = [...a];\n b = [...b];\n }\n\n if (a instanceof Object && b instanceof Object && 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 const castKey = (key: string) => (Array.isArray(a) ? Number(key) : key);\n\n for (const [key, value] of Object.entries(a)) {\n if (!(key in b)) {\n yield [\n { op: 'remove', path: [...prefix, castKey(key)] },\n { op: 'add', path: [...prefix, castKey(key)], value },\n ];\n } else {\n yield* _diff(value, b[key], options, [...prefix, castKey(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, castKey(key)], value },\n { op: 'remove', path: [...prefix, castKey(key)] },\n ];\n }\n }\n}\n","export function findOrDefault<T>(\n array: T[],\n predicate: (item: T) => boolean,\n defaultValue: T | (() => T),\n): T {\n const index = array.findIndex(predicate);\n\n if (index >= 0) {\n return array[index]!;\n }\n\n const value = defaultValue instanceof Function ? defaultValue() : defaultValue;\n array.push(value);\n return value;\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 = (\n value: any,\n path: KeyType[],\n): [value: unknown, subValues: { path: KeyType[]; value: unknown }[]] => {\n const [first, ...rest] = path;\n if (first === undefined) return [value, []];\n\n if (rest.length === 0) {\n if (first === '*')\n return [{}, Object.entries(value).map(([k, v]) => ({ path: [k], value: v }))];\n if (!(first in value)) return [value, []];\n const { [first]: subValue, ...newValue } = value;\n return [newValue, [{ path: [first], value: subValue }]];\n }\n\n const newValue = { ...value };\n const subValues = new Array<{ path: KeyType[]; value: unknown }>();\n for (const key of first === '*' ? Object.keys(value) : [first]) {\n if (!(newValue[key] instanceof Object)) return [value, []];\n const result = split(newValue[key], rest);\n newValue[key] = result[0];\n subValues.push(...result[1].map((s) => ({ path: [key, ...s.path], value: s.value })));\n }\n return [newValue, subValues];\n};\n","import type { MaybePromise } from './maybePromise';\n\nexport function maybeAsync<T, R>(\n value: MaybePromise<T>,\n action: (value: T) => MaybePromise<R>,\n): MaybePromise<R> {\n if (value instanceof Promise) {\n return value.then(action);\n }\n return action(value);\n}\n\nexport function maybeAsyncArray<T>(values: (() => MaybePromise<T>)[]): MaybePromise<T[]> {\n const run = (remainingValues: (() => MaybePromise<T>)[], results: T[]): MaybePromise<T[]> => {\n const [first, ...rest] = remainingValues;\n if (!first) {\n return results;\n }\n\n return maybeAsync(first(), (result) => run(rest, results.concat(result)));\n };\n\n return run(values, []);\n}\n","import { maybeAsync, maybeAsyncArray } from '@lib/maybeAsync';\n\nexport interface PersistStorageBase {\n getItem: (key: string) => string | null | Promise<string | null>;\n setItem: (key: string, value: string) => unknown | Promise<unknown>;\n removeItem: (key: string) => unknown | 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 type PersistStorage = PersistStorageBase &\n (PersistStorageWithKeys | PersistStorageWithLength);\n\nexport function normalizeStorage(storage: PersistStorage): PersistStorageWithKeys {\n return {\n getItem: storage.getItem.bind(storage),\n setItem: storage.setItem.bind(storage),\n removeItem: storage.removeItem.bind(storage),\n\n keys(): string[] | Promise<string[]> {\n if ('keys' in storage) {\n return storage.keys();\n }\n\n return maybeAsync(\n storage.length instanceof Function ? storage.length() : storage.length,\n (length) => {\n const keyPromises = maybeAsyncArray(\n Array.from({ length }, (_, index) => () => storage.key(index)),\n );\n\n return maybeAsync(keyPromises, (keys) =>\n keys.filter((key): key is string => typeof key === 'string'),\n );\n },\n );\n },\n };\n}\n","import { isAncestor } from './persistPathHelpers';\nimport {\n normalizeStorage,\n type PersistStorage,\n type PersistStorageWithKeys,\n} from './persistStorage';\nimport { type Cancel, type Store } from '@core';\nimport { diff } from '@lib/diff';\nimport { shallowEqual } from '@lib/equals';\nimport { maybeAsync, maybeAsyncArray } from '@lib/maybeAsync';\nimport type { KeyType, WildcardPath } from '@lib/path';\nimport { castArrayPath, get, set } from '@lib/propAccess';\nimport { queue } from '@lib/queue';\n\ntype PathOption<T> =\n | WildcardPath<T>\n | {\n path: WildcardPath<T>;\n throttleMs?: number;\n };\n\nexport interface PersistOptions<T> {\n id: string;\n storage: PersistStorage;\n paths?: PathOption<T>[];\n throttleMs?: number;\n}\n\nexport class Persist<T> {\n readonly storage: PersistStorageWithKeys;\n\n readonly paths: {\n path: KeyType[];\n throttleMs?: 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?: [any, any];\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 this.paths = (options.paths ?? [])\n .map<{\n path: KeyType[];\n throttleMs?: number;\n }>((p) => {\n if (isPlainPath(p)) {\n return { path: castArrayPath(p) };\n }\n\n const _p = p as { path: KeyType[]; throttleMs?: number };\n\n return {\n path: castArrayPath(_p.path),\n throttleMs: _p.throttleMs,\n };\n })\n .sort((a, b) => b.path.length - a.path.length);\n\n if (this.paths.length === 0) {\n this.paths.push({ path: ['*'] });\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 let committed = this.store.get();\n\n const cancel = this.store.subscribe(\n (value) => {\n const [patches] = diff(committed, value);\n committed = value;\n\n for (const patch of patches) {\n if (\n this.updateInProgress &&\n shallowEqual(this.updateInProgress[0], patch.path) &&\n this.updateInProgress[1] === (patch.op === 'remove' ? undefined : patch.value)\n ) {\n continue;\n }\n\n const ancestor = this.paths.find((p) => isAncestor(p.path, patch.path));\n\n if (!ancestor) {\n continue;\n }\n\n const pathToSave = patch.path.slice(0, ancestor.path.length);\n this.queue(() => this.save(pathToSave), pathToSave);\n }\n },\n { runNow: false },\n );\n\n this.handles.add(cancel);\n }\n\n private async watchStorage() {\n let keys = this.storage.keys();\n if (keys instanceof Promise) {\n keys = await keys;\n }\n\n if (this.stopped) {\n return;\n }\n\n for (const key of keys) {\n const path = this.parseKey(key);\n if (!path) {\n continue;\n }\n\n this.queue(() => this.load(path));\n }\n\n this.queue(() => this.resolveInitialized?.());\n\n const listener = (event: MessageEvent) => {\n this.queue(() => this.load(event.data));\n };\n\n this.channel.addEventListener('message', listener);\n this.handles.add(() => this.channel.removeEventListener('message', listener));\n }\n\n private buildKey(path: KeyType[]) {\n return `${this.prefix}${JSON.stringify(path)}`;\n }\n\n private parseKey(key: string) {\n if (!key.startsWith(this.prefix)) {\n return;\n }\n\n return JSON.parse(key.slice(this.prefix.length)) as KeyType[];\n }\n\n private load(path: KeyType[]) {\n const matchingPath = this.paths.find(\n (p) => p.path.length === path.length && isAncestor(p.path, path),\n );\n if (!matchingPath) {\n return;\n }\n\n const key = this.buildKey(path);\n\n return maybeAsync(this.storage.getItem(key), (value) => {\n if (this.stopped || !value) {\n return;\n }\n\n const inSaveQueue = this.queue\n .getRefs()\n .find((ref) => isAncestor(ref, path) || isAncestor(path, ref));\n if (inSaveQueue) {\n return;\n }\n\n const parsedValue = value === 'undefined' ? undefined : JSON.parse(value);\n\n this.updateInProgress = [path, parsedValue];\n this.store.set((state) => set(state, path as any, parsedValue));\n this.updateInProgress = undefined;\n });\n }\n\n private save(path: KeyType[]) {\n const key = this.buildKey(path);\n const value = get(this.store.get(), path as any);\n const serializedValue = value === undefined ? 'undefined' : JSON.stringify(value);\n\n return maybeAsync(this.storage.setItem(key, serializedValue), () => {\n this.channel.postMessage(path);\n\n return maybeAsync(this.storage.keys(), (keys) => {\n const toRemove = keys.filter((k) => {\n const parsedKey = this.parseKey(k);\n return (\n parsedKey && parsedKey.length > path.length && isAncestor(path, parsedKey)\n // !this.queue.getRefs().find((ref) => isAncestor(ref, parsedKey))\n );\n });\n\n return maybeAsyncArray(toRemove.map((k) => () => this.storage.removeItem(k)));\n });\n });\n }\n\n async stop() {\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","import { type Store } from '@core';\nimport { applyPatches } from '@lib/applyPatches';\nimport { type Patch, diff } from '@lib/diff';\n\nexport interface Message<T> {\n id: string;\n previousId?: string;\n patches: Patch[];\n}\n\nconst genId = () => Math.random().toString(36).slice(2);\n\nexport class Sync<T> {\n private previousId = genId();\n private previousState = this.store.get();\n\n private patchStream = this.store.map((state) => {\n const id = genId();\n const previousId = this.previousId;\n const patches = diff(this.previousState, state)[0];\n\n this.previousId = id;\n this.previousState = state;\n return { id, previousId, patches };\n });\n\n constructor(public readonly store: Store<T>) {\n this.patchStream.addEffect(() => {\n this.previousId = genId();\n this.previousState = this.store.get();\n });\n }\n\n connectToClient(listener: (message: Message<T>) => void) {\n const cancel = this.patchStream.subscribe(listener, { runNow: false });\n\n listener({\n id: this.previousId,\n patches: [{ op: 'replace', path: [], value: this.previousState }],\n });\n\n return cancel;\n }\n\n async connectToServer(stream: AsyncIterable<Message<T>>) {\n let previousId;\n\n for await (const message of stream) {\n if (message.previousId && message.previousId !== previousId) {\n throw new Error('previousId mismatch');\n }\n\n previousId = message.id;\n this.store.set((state) => applyPatches(state, ...message.patches));\n }\n }\n}\n\nexport function createSync<T>(store: Store<T>) {\n return new Sync(store);\n}\n"],"names":["remove","set","deepEqual","store","queue","castArrayPath","shallowEqual","get"],"mappings":";;;;;AAGA,SAAS,iBAAoB,QAAW,OAAiB;AACnD,MAAA,MAAM,OAAO,UAAU;AAClB,WAAAA,aAAO,QAAQ,MAAM,IAAW;AAAA,EACzC;AAEA,SAAOC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CAAC,UAAU;AAC9C,YAAM,KAAK;AACX,YAAM,aAAa,KAAK;AACxB,YAAM,UAAU,KAAK,KAAK,eAAe,KAAK,EAAE,CAAC;AAEjD,WAAK,aAAa;AAClB,WAAK,gBAAgB;AACd,aAAA,EAAE,IAAI,YAAY;IAAQ,CAClC;AAGM,SAAA,YAAY,UAAU,MAAM;AAC/B,WAAK,aAAa;AACb,WAAA,gBAAgB,KAAK,MAAM,IAAI;AAAA,IAAA,CACrC;AAAA,EACH;AAAA,EAEA,gBAAgB,UAAyC;AACjD,UAAA,SAAS,KAAK,YAAY,UAAU,UAAU,EAAE,QAAQ,OAAO;AAE5D,aAAA;AAAA,MACP,IAAI,KAAK;AAAA,MACT,SAAS,CAAC,EAAE,IAAI,WAAW,MAAM,CAAA,GAAI,OAAO,KAAK,eAAe;AAAA,IAAA,CACjE;AAEM,WAAA;AAAA,EACT;AAAA,EAEA,MAAM,gBAAgB,QAAmC;AACnD,QAAA;AAEJ,qBAAiB,WAAW,QAAQ;AAClC,UAAI,QAAQ,cAAc,QAAQ,eAAe,YAAY;AACrD,cAAA,IAAI,MAAM,qBAAqB;AAAA,MACvC;AAEA,mBAAa,QAAQ;AAChB,WAAA,MAAM,IAAI,CAAC,UAAU,aAAa,OAAO,GAAG,QAAQ,OAAO,CAAC;AAAA,IACnE;AAAA,EACF;AACF;AAEO,SAAS,WAAcA,QAAiB;AACtC,SAAA,IAAI,KAAKA,MAAK;AACvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/lib/updateHelpers.ts","../../src/persist/persistPathHelpers.ts","../../src/lib/maybeAsync.ts","../../src/persist/persistStorage.ts","../../src/persist/persist.ts"],"sourcesContent":["export function findOrDefault<T>(\n array: T[],\n predicate: (item: T) => boolean,\n defaultValue: T | (() => T),\n): T {\n const index = array.findIndex(predicate);\n\n if (index >= 0) {\n return array[index]!;\n }\n\n const value = defaultValue instanceof Function ? defaultValue() : defaultValue;\n array.push(value);\n return value;\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 = (\n value: any,\n path: KeyType[],\n): [value: unknown, subValues: { path: KeyType[]; value: unknown }[]] => {\n const [first, ...rest] = path;\n if (first === undefined) return [value, []];\n\n if (rest.length === 0) {\n if (first === '*')\n return [{}, Object.entries(value).map(([k, v]) => ({ path: [k], value: v }))];\n if (!(first in value)) return [value, []];\n const { [first]: subValue, ...newValue } = value;\n return [newValue, [{ path: [first], value: subValue }]];\n }\n\n const newValue = { ...value };\n const subValues = new Array<{ path: KeyType[]; value: unknown }>();\n for (const key of first === '*' ? Object.keys(value) : [first]) {\n if (!(newValue[key] instanceof Object)) return [value, []];\n const result = split(newValue[key], rest);\n newValue[key] = result[0];\n subValues.push(...result[1].map((s) => ({ path: [key, ...s.path], value: s.value })));\n }\n return [newValue, subValues];\n};\n","import type { MaybePromise } from './maybePromise';\n\nexport function maybeAsync<T, R>(\n value: MaybePromise<T>,\n action: (value: T) => MaybePromise<R>,\n): MaybePromise<R> {\n if (value instanceof Promise) {\n return value.then(action);\n }\n return action(value);\n}\n\nexport function maybeAsyncArray<T>(values: (() => MaybePromise<T>)[]): MaybePromise<T[]> {\n const run = (remainingValues: (() => MaybePromise<T>)[], results: T[]): MaybePromise<T[]> => {\n const [first, ...rest] = remainingValues;\n if (!first) {\n return results;\n }\n\n return maybeAsync(first(), (result) => run(rest, results.concat(result)));\n };\n\n return run(values, []);\n}\n","import { maybeAsync, maybeAsyncArray } from '@lib/maybeAsync';\n\nexport interface PersistStorageBase {\n getItem: (key: string) => string | null | Promise<string | null>;\n setItem: (key: string, value: string) => unknown | Promise<unknown>;\n removeItem: (key: string) => unknown | 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 type PersistStorage = PersistStorageBase &\n (PersistStorageWithKeys | PersistStorageWithLength);\n\nexport function normalizeStorage(storage: PersistStorage): PersistStorageWithKeys {\n return {\n getItem: storage.getItem.bind(storage),\n setItem: storage.setItem.bind(storage),\n removeItem: storage.removeItem.bind(storage),\n\n keys(): string[] | Promise<string[]> {\n if ('keys' in storage) {\n return storage.keys();\n }\n\n return maybeAsync(\n storage.length instanceof Function ? storage.length() : storage.length,\n (length) => {\n const keyPromises = maybeAsyncArray(\n Array.from({ length }, (_, index) => () => storage.key(index)),\n );\n\n return maybeAsync(keyPromises, (keys) =>\n keys.filter((key): key is string => typeof key === 'string'),\n );\n },\n );\n },\n };\n}\n","import { isAncestor } from './persistPathHelpers';\nimport {\n normalizeStorage,\n type PersistStorage,\n type PersistStorageWithKeys,\n} from './persistStorage';\nimport { type Cancel, type Store } from '@core';\nimport { diff } from '@lib/diff';\nimport { shallowEqual } from '@lib/equals';\nimport { maybeAsync, maybeAsyncArray } from '@lib/maybeAsync';\nimport type { KeyType, WildcardPath } from '@lib/path';\nimport { castArrayPath, get, set } from '@lib/propAccess';\nimport { queue } from '@lib/queue';\n\ntype PathOption<T> =\n | WildcardPath<T>\n | {\n path: WildcardPath<T>;\n throttleMs?: number;\n };\n\nexport interface PersistOptions<T> {\n id: string;\n storage: PersistStorage;\n paths?: PathOption<T>[];\n throttleMs?: number;\n}\n\nexport class Persist<T> {\n readonly storage: PersistStorageWithKeys;\n\n readonly paths: {\n path: KeyType[];\n throttleMs?: 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?: [any, any];\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 this.paths = (options.paths ?? [])\n .map<{\n path: KeyType[];\n throttleMs?: number;\n }>((p) => {\n if (isPlainPath(p)) {\n return { path: castArrayPath(p) };\n }\n\n const _p = p as { path: KeyType[]; throttleMs?: number };\n\n return {\n path: castArrayPath(_p.path),\n throttleMs: _p.throttleMs,\n };\n })\n .sort((a, b) => b.path.length - a.path.length);\n\n if (this.paths.length === 0) {\n this.paths.push({ path: ['*'] });\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 let committed = this.store.get();\n\n const cancel = this.store.subscribe(\n (value) => {\n const [patches] = diff(committed, value);\n committed = value;\n\n for (const patch of patches) {\n if (\n this.updateInProgress &&\n shallowEqual(this.updateInProgress[0], patch.path) &&\n this.updateInProgress[1] === (patch.op === 'remove' ? undefined : patch.value)\n ) {\n continue;\n }\n\n const ancestor = this.paths.find((p) => isAncestor(p.path, patch.path));\n\n if (!ancestor) {\n continue;\n }\n\n const pathToSave = patch.path.slice(0, ancestor.path.length);\n this.queue(() => this.save(pathToSave), pathToSave);\n }\n },\n { runNow: false },\n );\n\n this.handles.add(cancel);\n }\n\n private async watchStorage() {\n let keys = this.storage.keys();\n if (keys instanceof Promise) {\n keys = await keys;\n }\n\n if (this.stopped) {\n return;\n }\n\n for (const key of keys) {\n const path = this.parseKey(key);\n if (!path) {\n continue;\n }\n\n this.queue(() => this.load(path));\n }\n\n this.queue(() => this.resolveInitialized?.());\n\n const listener = (event: MessageEvent) => {\n this.queue(() => this.load(event.data));\n };\n\n this.channel.addEventListener('message', listener);\n this.handles.add(() => this.channel.removeEventListener('message', listener));\n }\n\n private buildKey(path: KeyType[]) {\n return `${this.prefix}${JSON.stringify(path)}`;\n }\n\n private parseKey(key: string) {\n if (!key.startsWith(this.prefix)) {\n return;\n }\n\n return JSON.parse(key.slice(this.prefix.length)) as KeyType[];\n }\n\n private load(path: KeyType[]) {\n const matchingPath = this.paths.find(\n (p) => p.path.length === path.length && isAncestor(p.path, path),\n );\n if (!matchingPath) {\n return;\n }\n\n const key = this.buildKey(path);\n\n return maybeAsync(this.storage.getItem(key), (value) => {\n if (this.stopped || !value) {\n return;\n }\n\n const inSaveQueue = this.queue\n .getRefs()\n .find((ref) => isAncestor(ref, path) || isAncestor(path, ref));\n if (inSaveQueue) {\n return;\n }\n\n const parsedValue = value === 'undefined' ? undefined : JSON.parse(value);\n\n this.updateInProgress = [path, parsedValue];\n this.store.set((state) => set(state, path as any, parsedValue));\n this.updateInProgress = undefined;\n });\n }\n\n private save(path: KeyType[]) {\n const key = this.buildKey(path);\n const value = get(this.store.get(), path as any);\n const serializedValue = value === undefined ? 'undefined' : JSON.stringify(value);\n\n return maybeAsync(this.storage.setItem(key, serializedValue), () => {\n this.channel.postMessage(path);\n\n return maybeAsync(this.storage.keys(), (keys) => {\n const toRemove = keys.filter((k) => {\n const parsedKey = this.parseKey(k);\n return (\n parsedKey && parsedKey.length > path.length && isAncestor(path, parsedKey)\n // !this.queue.getRefs().find((ref) => isAncestor(ref, parsedKey))\n );\n });\n\n return maybeAsyncArray(toRemove.map((k) => () => this.storage.removeItem(k)));\n });\n });\n }\n\n async stop() {\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"],"names":["store","queue","castArrayPath","diff","shallowEqual","set","get"],"mappings":";;;;;;;AAAgB,SAAA,cACd,OACA,WACA,cACG;AACG,QAAA,QAAQ,MAAM,UAAU,SAAS;AAEvC,MAAI,SAAS,GAAG;AACd,WAAO,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AAU,WAAW,MAAM,SAAS;AAAA,QAAA,CAG5E;AAEM,eAAA,gBAAgB,SAAS,IAAI,CAAC,MAAM,MAAM,KAAK,QAAQ,WAAW,CAAC,CAAC,CAAC;AAAA,MAAA,CAC7E;AAAA,IAAA,CACF;AAAA,EACH;AAAA,EAEA,MAAM,OAAO;AACX,SAAK,UAAU;AAEJ,eAAA,UAAU,KAAK,SAAS;AAC1B;IACT;AAEM,UAAA,KAAK,MAAM;AACjB,SAAK,QAAQ;EACf;AACF;AAEgB,SAAA,QAAWN,QAAiB,SAAwC;AAC3E,SAAA,IAAI,QAAWA,QAAO,OAAO;AACtC;AAEA,SAAS,YAAe,GAA+D;AACrF,SAAO,OAAO,MAAM,YAAY,MAAM,QAAQ,CAAC;AACjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const immerMethods = require("../immerMethods.cjs");
4
- exports.immerMethods = immerMethods.immerMethods;
3
+ const mutativeMethods = require("../mutativeMethods.cjs");
4
+ exports.mutativeMethods = mutativeMethods.mutativeMethods;
5
5
  //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ const mutativeMethods = require("../mutativeMethods.cjs");
3
+ const store = require("../store.cjs");
4
+ Object.assign(store.Store.prototype, mutativeMethods.mutativeMethods);
5
+ //# sourceMappingURL=register.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.cjs","sources":["../../../src/mutative/register.ts"],"sourcesContent":["import { mutativeMethods } from './mutativeMethods';\nimport { Store } from '@core';\n\ntype MutativeMethods = typeof mutativeMethods;\n\ndeclare module '@core' {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Store<T> extends MutativeMethods {}\n}\n\nObject.assign(Store.prototype, mutativeMethods);\n"],"names":["Store","mutativeMethods"],"mappings":";;;AAUA,OAAO,OAAOA,MAAAA,MAAM,WAAWC,+BAAe;"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ const mutative = require("mutative");
3
+ function update(...args) {
4
+ if (args.length === 1) {
5
+ const [mutation] = args;
6
+ this.set((value) => {
7
+ const result = mutative.create(value, (draft) => {
8
+ mutation(draft);
9
+ });
10
+ return result;
11
+ });
12
+ } else {
13
+ const [path, mutation] = args;
14
+ this.set(path, (value) => {
15
+ const result = mutative.create(value, (draft) => {
16
+ mutation(draft);
17
+ });
18
+ return result;
19
+ });
20
+ }
21
+ }
22
+ const mutativeMethods = {
23
+ update
24
+ };
25
+ exports.mutativeMethods = mutativeMethods;
26
+ //# sourceMappingURL=mutativeMethods.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutativeMethods.cjs","sources":["../../src/mutative/mutativeMethods.ts"],"sourcesContent":["import type { Store } from '@core/store';\nimport { type Path, type Value } from '@lib/path';\nimport { create, type Draft } from 'mutative';\n\nexport type Mutation<T> = (draft: Draft<T>) => void;\n\nfunction update<T>(this: Store<T>, mutation: Mutation<T>): void;\n\nfunction update<T, const P extends Path<T>>(\n this: Store<T>,\n path: P,\n mutation: Mutation<Value<T, P>>,\n): void;\n\nfunction update<T, TPath extends Path<T>>(\n this: Store<T>,\n ...args: [recipe: Mutation<T>] | [path: TPath, mutation: Mutation<Value<T, TPath>>]\n) {\n if (args.length === 1) {\n const [mutation] = args;\n\n this.set((value) => {\n const result = create(value, (draft) => {\n mutation(draft);\n });\n return result;\n });\n } else {\n const [path, mutation] = args;\n\n this.set(path, (value) => {\n const result = create(value, (draft) => {\n mutation(draft);\n });\n return result;\n });\n }\n}\n\nexport const mutativeMethods = {\n update,\n};\n"],"names":["create"],"mappings":";;AAcA,SAAS,UAEJ,MACH;AACI,MAAA,KAAK,WAAW,GAAG;AACf,UAAA,CAAC,QAAQ,IAAI;AAEd,SAAA,IAAI,CAAC,UAAU;AAClB,YAAM,SAASA,SAAAA,OAAO,OAAO,CAAC,UAAU;AACtC,iBAAS,KAAK;AAAA,MAAA,CACf;AACM,aAAA;AAAA,IAAA,CACR;AAAA,EAAA,OACI;AACC,UAAA,CAAC,MAAM,QAAQ,IAAI;AAEpB,SAAA,IAAI,MAAM,CAAC,UAAU;AACxB,YAAM,SAASA,SAAAA,OAAO,OAAO,CAAC,UAAU;AACtC,iBAAS,KAAK;AAAA,MAAA,CACf;AACM,aAAA;AAAA,IAAA,CACR;AAAA,EACH;AACF;AAEO,MAAM,kBAAkB;AAAA,EAC7B;AACF;;"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const diff = require("../diff.cjs");
4
+ const genId = () => Math.random().toString(36).slice(2);
5
+ function subscribePatches(listener, options) {
6
+ if (!this.__patches) {
7
+ let previousValue = this.get();
8
+ this.__patches = this.map((value) => {
9
+ const result = diff.diff(previousValue, value, options);
10
+ previousValue = value;
11
+ return result;
12
+ });
13
+ }
14
+ const { stopAt, runNow, ...subscribeOptions } = options ?? {};
15
+ const cancel = this.__patches.subscribe((p) => listener(...p), subscribeOptions);
16
+ if (runNow) {
17
+ listener(...diff.diff(void 0, this.get(), options));
18
+ }
19
+ return cancel;
20
+ }
21
+ function applyPatches(...patches) {
22
+ this.set((value) => diff.applyPatches(value, ...patches.flat()));
23
+ }
24
+ function sync(listener, options) {
25
+ let previousId;
26
+ return this.subscribePatches(
27
+ (patches) => {
28
+ const id = genId();
29
+ const message = { id, previousId, patches };
30
+ previousId = id;
31
+ listener(message);
32
+ },
33
+ { ...options, runNow: true }
34
+ );
35
+ }
36
+ function acceptSync() {
37
+ let previousId;
38
+ return (message) => {
39
+ if (message.previousId && message.previousId !== previousId) {
40
+ throw new Error("previousId mismatch");
41
+ }
42
+ previousId = message.id;
43
+ this.applyPatches(...message.patches);
44
+ };
45
+ }
46
+ const patchMethods = {
47
+ subscribePatches,
48
+ applyPatches,
49
+ sync,
50
+ acceptSync
51
+ };
52
+ exports.patchMethods = patchMethods;
53
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../../../src/patches/patchMethods.ts"],"sourcesContent":["import type { SubscribeOptions } from '@core';\nimport type { Store } from '@core/store';\nimport { applyPatches as _applyPatches } from '@lib/applyPatches';\nimport { diff, type DiffOptions, type Patch } from '@lib/diff';\n\ndeclare module '@core' {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Store<T> {\n __patches?: Store<[Patch[], Patch[]]>;\n }\n}\n\nexport interface SubscribePatchOptions extends SubscribeOptions, DiffOptions {\n /** @default false */\n runNow?: boolean;\n}\n\nexport interface SyncMessage {\n id: string;\n previousId?: string;\n patches: Patch[];\n}\n\nexport type InteropPatch = Patch | { op: 'add' | 'replace' | 'remove'; value?: any };\n\nconst genId = () => Math.random().toString(36).slice(2);\n\nfunction subscribePatches<T>(\n this: Store<T>,\n listener: (patches: Patch[], reversePatches: Patch[]) => void,\n options?: SubscribePatchOptions,\n) {\n if (!this.__patches) {\n let previousValue = this.get();\n\n this.__patches = this.map((value) => {\n const result = diff(previousValue, value, options);\n previousValue = value;\n return result;\n });\n }\n\n const { stopAt, runNow, ...subscribeOptions } = options ?? {};\n\n const cancel = this.__patches.subscribe((p) => listener(...p), subscribeOptions);\n\n if (runNow) {\n listener(...diff(undefined, this.get(), options));\n }\n\n return cancel;\n}\n\nfunction applyPatches<T>(this: Store<T>, patches: InteropPatch[]): void;\nfunction applyPatches<T>(this: Store<T>, ...patches: InteropPatch[]): void;\nfunction applyPatches<T>(this: Store<T>, ...patches: (InteropPatch | InteropPatch[])[]): void {\n this.set((value) => _applyPatches(value, ...(patches.flat() as Patch[])));\n}\n\nfunction sync<T>(\n this: Store<T>,\n listener: (syncMessage: SyncMessage) => void,\n options?: Omit<SubscribePatchOptions, 'runNow'>,\n) {\n let previousId: string | undefined;\n\n return this.subscribePatches(\n (patches) => {\n const id = genId();\n const message = { id, previousId, patches };\n previousId = id;\n\n listener(message);\n },\n { ...options, runNow: true },\n );\n}\n\nfunction acceptSync<T>(this: Store<T>) {\n let previousId: string | undefined;\n\n return (message: SyncMessage) => {\n if (message.previousId && message.previousId !== previousId) {\n throw new Error('previousId mismatch');\n }\n\n previousId = message.id;\n this.applyPatches(...message.patches);\n };\n}\n\nexport const patchMethods = {\n subscribePatches,\n applyPatches,\n sync,\n acceptSync,\n};\n"],"names":["diff","_applyPatches"],"mappings":";;;AAyBA,MAAM,QAAQ,MAAM,KAAK,SAAS,SAAS,EAAE,EAAE,MAAM,CAAC;AAEtD,SAAS,iBAEP,UACA,SACA;AACI,MAAA,CAAC,KAAK,WAAW;AACf,QAAA,gBAAgB,KAAK;AAEzB,SAAK,YAAY,KAAK,IAAI,CAAC,UAAU;AACnC,YAAM,SAASA,KAAA,KAAK,eAAe,OAAO,OAAO;AACjC,sBAAA;AACT,aAAA;AAAA,IAAA,CACR;AAAA,EACH;AAEA,QAAM,EAAE,QAAQ,QAAQ,GAAG,iBAAiB,IAAI,WAAW;AAErD,QAAA,SAAS,KAAK,UAAU,UAAU,CAAC,MAAM,SAAS,GAAG,CAAC,GAAG,gBAAgB;AAE/E,MAAI,QAAQ;AACV,aAAS,GAAGA,KAAK,KAAA,QAAW,KAAK,IAAI,GAAG,OAAO,CAAC;AAAA,EAClD;AAEO,SAAA;AACT;AAIA,SAAS,gBAAmC,SAAkD;AACvF,OAAA,IAAI,CAAC,UAAUC,KAAA,aAAc,OAAO,GAAI,QAAQ,KAAkB,CAAA,CAAC;AAC1E;AAEA,SAAS,KAEP,UACA,SACA;AACI,MAAA;AAEJ,SAAO,KAAK;AAAA,IACV,CAAC,YAAY;AACX,YAAM,KAAK;AACX,YAAM,UAAU,EAAE,IAAI,YAAY,QAAQ;AAC7B,mBAAA;AAEb,eAAS,OAAO;AAAA,IAClB;AAAA,IACA,EAAE,GAAG,SAAS,QAAQ,KAAK;AAAA,EAAA;AAE/B;AAEA,SAAS,aAA8B;AACjC,MAAA;AAEJ,SAAO,CAAC,YAAyB;AAC/B,QAAI,QAAQ,cAAc,QAAQ,eAAe,YAAY;AACrD,YAAA,IAAI,MAAM,qBAAqB;AAAA,IACvC;AAEA,iBAAa,QAAQ;AAChB,SAAA,aAAa,GAAG,QAAQ,OAAO;AAAA,EAAA;AAExC;AAEO,MAAM,eAAe;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;;"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const patches_index = require("./index.cjs");
4
+ const store = require("../store.cjs");
5
+ let backup;
6
+ function register() {
7
+ if (backup) {
8
+ return;
9
+ }
10
+ backup = {};
11
+ for (const [name, method] of Object.entries(patches_index.patchMethods)) {
12
+ if (name in store.Store.prototype) {
13
+ backup[name] = store.Store.prototype[name];
14
+ }
15
+ store.Store.prototype[name] = method;
16
+ }
17
+ }
18
+ function unregister() {
19
+ if (!backup) {
20
+ return;
21
+ }
22
+ for (const key in patches_index.patchMethods) {
23
+ if (key in backup) {
24
+ store.Store.prototype[key] = backup[key];
25
+ } else {
26
+ delete store.Store.prototype[key];
27
+ }
28
+ }
29
+ backup = void 0;
30
+ }
31
+ register();
32
+ exports.register = register;
33
+ exports.unregister = unregister;
34
+ //# sourceMappingURL=register.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.cjs","sources":["../../../src/patches/register.ts"],"sourcesContent":["import { patchMethods } from './patchMethods';\nimport { Store } from '@core';\n\ntype PatchMethods = typeof patchMethods;\n\ndeclare module '@core' {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Store<T> extends PatchMethods {}\n}\n\nlet backup: any;\n\nexport function register() {\n if (backup) {\n return;\n }\n\n backup = {};\n\n for (const [name, method] of Object.entries(patchMethods)) {\n if (name in Store.prototype) {\n backup[name] = (Store.prototype as any)[name];\n }\n\n (Store.prototype as any)[name] = method;\n }\n}\n\nexport function unregister() {\n if (!backup) {\n return;\n }\n\n for (const key in patchMethods) {\n if (key in backup) {\n (Store.prototype as any)[key] = backup[key];\n } else {\n delete (Store.prototype as any)[key];\n }\n }\n\n backup = undefined;\n}\n\nregister();\n"],"names":["patchMethods","Store"],"mappings":";;;;AAUA,IAAI;AAEG,SAAS,WAAW;AACzB,MAAI,QAAQ;AACV;AAAA,EACF;AAEA,WAAS,CAAA;AAET,aAAW,CAAC,MAAM,MAAM,KAAK,OAAO,QAAQA,cAAAA,YAAY,GAAG;AACrD,QAAA,QAAQC,YAAM,WAAW;AAC3B,aAAO,IAAI,IAAKA,MAAM,MAAA,UAAkB,IAAI;AAAA,IAC9C;AAECA,UAAAA,MAAM,UAAkB,IAAI,IAAI;AAAA,EACnC;AACF;AAEO,SAAS,aAAa;AAC3B,MAAI,CAAC,QAAQ;AACX;AAAA,EACF;AAEA,aAAW,OAAOD,4BAAc;AAC9B,QAAI,OAAO,QAAQ;AAChBC,YAAAA,MAAM,UAAkB,GAAG,IAAI,OAAO,GAAG;AAAA,IAAA,OACrC;AACG,aAAAA,MAAA,MAAM,UAAkB,GAAG;AAAA,IACrC;AAAA,EACF;AAES,WAAA;AACX;AAEA,SAAS;;;"}