cross-state 0.6.6 → 0.6.7

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 (147) hide show
  1. package/dist/cjs/_virtual/use-sync-external-store-with-selector.development.cjs +103 -0
  2. package/dist/cjs/_virtual/use-sync-external-store-with-selector.development.cjs.map +1 -0
  3. package/dist/cjs/_virtual/use-sync-external-store-with-selector.development2.cjs +5 -0
  4. package/dist/cjs/_virtual/use-sync-external-store-with-selector.development2.cjs.map +1 -0
  5. package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min.cjs +71 -0
  6. package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min.cjs.map +1 -0
  7. package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min2.cjs +5 -0
  8. package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min2.cjs.map +1 -0
  9. package/dist/cjs/_virtual/with-selector.cjs +12 -0
  10. package/dist/cjs/_virtual/with-selector.cjs.map +1 -0
  11. package/dist/cjs/_virtual/with-selector2.cjs +13 -0
  12. package/dist/cjs/_virtual/with-selector2.cjs.map +1 -0
  13. package/dist/cjs/core/cache.cjs +227 -0
  14. package/dist/cjs/core/cache.cjs.map +1 -0
  15. package/dist/cjs/core/resourceGroup.cjs +53 -0
  16. package/dist/cjs/core/resourceGroup.cjs.map +1 -0
  17. package/dist/cjs/core/store.cjs +259 -0
  18. package/dist/cjs/core/store.cjs.map +1 -0
  19. package/dist/cjs/immer/immerActions.cjs +10 -0
  20. package/dist/cjs/immer/immerActions.cjs.map +1 -0
  21. package/dist/cjs/immer.cjs +2 -7
  22. package/dist/cjs/immer.cjs.map +1 -1
  23. package/dist/cjs/index.cjs +19 -299
  24. package/dist/cjs/index.cjs.map +1 -1
  25. package/dist/cjs/lib/calcDuration.cjs +9 -0
  26. package/dist/cjs/lib/calcDuration.cjs.map +1 -0
  27. package/dist/cjs/lib/calculationHelper.cjs +140 -0
  28. package/dist/cjs/lib/calculationHelper.cjs.map +1 -0
  29. package/dist/cjs/lib/callable.cjs +11 -0
  30. package/dist/cjs/lib/callable.cjs.map +1 -0
  31. package/dist/cjs/lib/clone.cjs +19 -0
  32. package/dist/cjs/lib/clone.cjs.map +1 -0
  33. package/dist/cjs/lib/debounce.cjs +28 -0
  34. package/dist/cjs/lib/debounce.cjs.map +1 -0
  35. package/dist/cjs/lib/equals.cjs +5 -0
  36. package/dist/cjs/lib/equals.cjs.map +1 -0
  37. package/dist/cjs/lib/forwardError.cjs +9 -0
  38. package/dist/cjs/lib/forwardError.cjs.map +1 -0
  39. package/dist/cjs/lib/hash.cjs +19 -0
  40. package/dist/cjs/lib/hash.cjs.map +1 -0
  41. package/dist/cjs/lib/instanceCache.cjs +68 -0
  42. package/dist/cjs/lib/instanceCache.cjs.map +1 -0
  43. package/dist/cjs/lib/makeSelector.cjs +14 -0
  44. package/dist/cjs/lib/makeSelector.cjs.map +1 -0
  45. package/dist/cjs/lib/propAccess.cjs +66 -0
  46. package/dist/cjs/lib/propAccess.cjs.map +1 -0
  47. package/dist/cjs/lib/queue.cjs +61 -0
  48. package/dist/cjs/lib/queue.cjs.map +1 -0
  49. package/dist/cjs/lib/standardMethods.cjs +78 -0
  50. package/dist/cjs/lib/standardMethods.cjs.map +1 -0
  51. package/dist/cjs/lib/throttle.cjs +25 -0
  52. package/dist/cjs/lib/throttle.cjs.map +1 -0
  53. package/dist/cjs/lib/trackingProxy.cjs +39 -0
  54. package/dist/cjs/lib/trackingProxy.cjs.map +1 -0
  55. package/dist/cjs/react/read.cjs +15 -0
  56. package/dist/cjs/react/read.cjs.map +1 -0
  57. package/dist/cjs/react/storeScope.cjs +31 -0
  58. package/dist/cjs/react/storeScope.cjs.map +1 -0
  59. package/dist/cjs/react/useCache.cjs +31 -0
  60. package/dist/cjs/react/useCache.cjs.map +1 -0
  61. package/dist/cjs/react/useProp.cjs +9 -0
  62. package/dist/cjs/react/useProp.cjs.map +1 -0
  63. package/dist/cjs/react/useStore.cjs +51 -0
  64. package/dist/cjs/react/useStore.cjs.map +1 -0
  65. package/dist/cjs/react.cjs +11 -289
  66. package/dist/cjs/react.cjs.map +1 -1
  67. package/dist/es/_virtual/use-sync-external-store-with-selector.development.js +103 -0
  68. package/dist/es/_virtual/use-sync-external-store-with-selector.development.js.map +1 -0
  69. package/dist/es/_virtual/use-sync-external-store-with-selector.development2.js +5 -0
  70. package/dist/es/_virtual/use-sync-external-store-with-selector.development2.js.map +1 -0
  71. package/dist/es/_virtual/use-sync-external-store-with-selector.production.min.js +71 -0
  72. package/dist/es/_virtual/use-sync-external-store-with-selector.production.min.js.map +1 -0
  73. package/dist/es/_virtual/use-sync-external-store-with-selector.production.min2.js +5 -0
  74. package/dist/es/_virtual/use-sync-external-store-with-selector.production.min2.js.map +1 -0
  75. package/dist/es/_virtual/with-selector.js +11 -0
  76. package/dist/es/_virtual/with-selector.js.map +1 -0
  77. package/dist/es/_virtual/with-selector2.js +14 -0
  78. package/dist/es/_virtual/with-selector2.js.map +1 -0
  79. package/dist/es/core/cache.js +227 -0
  80. package/dist/es/core/cache.js.map +1 -0
  81. package/dist/es/core/resourceGroup.js +53 -0
  82. package/dist/es/core/resourceGroup.js.map +1 -0
  83. package/dist/es/core/store.js +259 -0
  84. package/dist/es/core/store.js.map +1 -0
  85. package/dist/es/{immer.mjs → immer/immerActions.js} +1 -1
  86. package/dist/es/immer/immerActions.js.map +1 -0
  87. package/dist/es/immer.js +5 -0
  88. package/dist/es/immer.js.map +1 -0
  89. package/dist/es/index.js +22 -0
  90. package/dist/es/index.js.map +1 -0
  91. package/dist/es/lib/calcDuration.js +9 -0
  92. package/dist/es/lib/calcDuration.js.map +1 -0
  93. package/dist/es/lib/calculationHelper.js +140 -0
  94. package/dist/es/lib/calculationHelper.js.map +1 -0
  95. package/dist/es/lib/callable.js +11 -0
  96. package/dist/es/lib/callable.js.map +1 -0
  97. package/dist/es/lib/clone.js +19 -0
  98. package/dist/es/lib/clone.js.map +1 -0
  99. package/dist/es/lib/debounce.js +28 -0
  100. package/dist/es/lib/debounce.js.map +1 -0
  101. package/dist/es/lib/equals.js +5 -0
  102. package/dist/es/lib/equals.js.map +1 -0
  103. package/dist/es/lib/forwardError.js +9 -0
  104. package/dist/es/lib/forwardError.js.map +1 -0
  105. package/dist/es/lib/hash.js +19 -0
  106. package/dist/es/lib/hash.js.map +1 -0
  107. package/dist/es/lib/instanceCache.js +68 -0
  108. package/dist/es/lib/instanceCache.js.map +1 -0
  109. package/dist/es/lib/makeSelector.js +14 -0
  110. package/dist/es/lib/makeSelector.js.map +1 -0
  111. package/dist/es/lib/propAccess.js +66 -0
  112. package/dist/es/lib/propAccess.js.map +1 -0
  113. package/dist/es/lib/queue.js +61 -0
  114. package/dist/es/lib/queue.js.map +1 -0
  115. package/dist/es/lib/standardMethods.js +78 -0
  116. package/dist/es/lib/standardMethods.js.map +1 -0
  117. package/dist/es/lib/throttle.js +25 -0
  118. package/dist/es/lib/throttle.js.map +1 -0
  119. package/dist/es/lib/trackingProxy.js +39 -0
  120. package/dist/es/lib/trackingProxy.js.map +1 -0
  121. package/dist/es/react/read.js +15 -0
  122. package/dist/es/react/read.js.map +1 -0
  123. package/dist/es/react/storeScope.js +31 -0
  124. package/dist/es/react/storeScope.js.map +1 -0
  125. package/dist/es/react/useCache.js +31 -0
  126. package/dist/es/react/useCache.js.map +1 -0
  127. package/dist/es/react/useProp.js +9 -0
  128. package/dist/es/react/useProp.js.map +1 -0
  129. package/dist/es/react/useStore.js +51 -0
  130. package/dist/es/react/useStore.js.map +1 -0
  131. package/dist/es/react.js +14 -0
  132. package/dist/es/react.js.map +1 -0
  133. package/dist/types/core/cache.d.ts +13 -11
  134. package/dist/types/core/store.d.ts +5 -3
  135. package/dist/types/lib/callable.d.ts +4 -0
  136. package/dist/types/lib/instanceCache.d.ts +2 -2
  137. package/package.json +8 -8
  138. package/dist/cjs/hash.cjs +0 -718
  139. package/dist/cjs/hash.cjs.map +0 -1
  140. package/dist/es/hash.mjs +0 -719
  141. package/dist/es/hash.mjs.map +0 -1
  142. package/dist/es/immer.mjs.map +0 -1
  143. package/dist/es/index.mjs +0 -303
  144. package/dist/es/index.mjs.map +0 -1
  145. package/dist/es/react.mjs +0 -292
  146. package/dist/es/react.mjs.map +0 -1
  147. package/dist/types/lib/bind.d.ts +0 -1
@@ -0,0 +1,68 @@
1
+ import { hash } from "./hash.js";
2
+ class InstanceCache {
3
+ constructor(factory, cacheTime) {
4
+ this.factory = factory;
5
+ this.cacheTime = cacheTime;
6
+ this.cache = /* @__PURE__ */ new Map();
7
+ this.interval = this.cacheTime ? setInterval(() => this.cleanup(), Math.max(this.cacheTime / 10, 1)) : void 0;
8
+ }
9
+ cleanup() {
10
+ var _a;
11
+ const cutoff = this.now() - (this.cacheTime ?? 0);
12
+ for (const [key, entry] of this.cache.entries()) {
13
+ if (entry.ref && entry.t <= cutoff) {
14
+ delete entry.ref;
15
+ }
16
+ if (!entry.ref && !((_a = entry.weakRef) == null ? void 0 : _a.deref())) {
17
+ this.cache.delete(key);
18
+ }
19
+ }
20
+ }
21
+ get(...args) {
22
+ var _a;
23
+ const key = hash(args);
24
+ let entry = this.cache.get(key);
25
+ let value = (entry == null ? void 0 : entry.ref) ?? ((_a = entry == null ? void 0 : entry.weakRef) == null ? void 0 : _a.deref());
26
+ if (!entry || !value) {
27
+ value = this.factory(...args);
28
+ entry = {
29
+ t: this.now(),
30
+ ref: value,
31
+ weakRef: new WeakRef(value)
32
+ };
33
+ this.cache.set(key, entry);
34
+ } else {
35
+ entry.t = this.now();
36
+ entry.ref ?? (entry.ref = value);
37
+ }
38
+ return value;
39
+ }
40
+ values() {
41
+ return [...this.cache.values()].map((entry) => {
42
+ var _a;
43
+ return entry.ref ?? ((_a = entry.weakRef) == null ? void 0 : _a.deref());
44
+ }).filter((value) => !!value);
45
+ }
46
+ stop() {
47
+ if (this.interval) {
48
+ clearInterval(this.interval);
49
+ }
50
+ }
51
+ stats() {
52
+ return {
53
+ count: this.cache.size,
54
+ withRef: [...this.cache.values()].filter((x) => !!x.ref).length,
55
+ withWeakRef: [...this.cache.values()].filter((x) => {
56
+ var _a;
57
+ return !!((_a = x.weakRef) == null ? void 0 : _a.deref());
58
+ }).length
59
+ };
60
+ }
61
+ now() {
62
+ return performance.now();
63
+ }
64
+ }
65
+ export {
66
+ InstanceCache
67
+ };
68
+ //# sourceMappingURL=instanceCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instanceCache.js","sources":["../../../src/lib/instanceCache.ts"],"sourcesContent":["import { hash } from './hash';\n\nexport class InstanceCache<Args extends any[], T extends object> {\n private cache = new Map<string, { t: number; ref?: T; weakRef: WeakRef<T> }>();\n\n private interval = this.cacheTime\n ? setInterval(() => this.cleanup(), Math.max(this.cacheTime / 10, 1))\n : undefined;\n\n constructor(public readonly factory: (...args: Args) => T, public readonly cacheTime?: number) {}\n\n cleanup() {\n const cutoff = this.now() - (this.cacheTime ?? 0);\n\n for (const [key, entry] of this.cache.entries()) {\n if (entry.ref && entry.t <= cutoff) {\n delete entry.ref;\n }\n\n if (!entry.ref && !entry.weakRef?.deref()) {\n this.cache.delete(key);\n }\n }\n }\n\n get(...args: Args) {\n const key = hash(args);\n let entry = this.cache.get(key);\n let value = entry?.ref ?? entry?.weakRef?.deref();\n\n if (!entry || !value) {\n value = this.factory(...args);\n entry = {\n t: this.now(),\n ref: value,\n weakRef: new WeakRef(value),\n };\n\n this.cache.set(key, entry);\n } else {\n entry.t = this.now();\n entry.ref ??= value;\n }\n\n return value;\n }\n\n values() {\n return [...this.cache.values()]\n .map((entry) => entry.ref ?? entry.weakRef?.deref())\n .filter((value): value is T => !!value);\n }\n\n stop() {\n if (this.interval) {\n clearInterval(this.interval);\n }\n }\n\n stats() {\n return {\n count: this.cache.size,\n withRef: [...this.cache.values()].filter((x) => !!x.ref).length,\n withWeakRef: [...this.cache.values()].filter((x) => !!x.weakRef?.deref()).length,\n };\n }\n\n private now() {\n return performance.now();\n }\n}\n"],"names":[],"mappings":";AAEO,MA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OAAO;AACL,QAAI,KAAK,UAAU;AACjB,oBAAc,KAAK,QAAQ;AAAA,IAC7B;AAAA,EACF;AAAA,EAEA,QAAQ;AACC,WAAA;AAAA,MACL,OAAO,KAAK,MAAM;AAAA,MAClB,SAAS,CAAC,GAAG,KAAK,MAAM,OAAO,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE;AAAA,MACzD,aAAa,CAAC,GAAG,KAAK,MAAM,OAAQ,CAAA,EAAE,OAAO,CAAC;;AAAM,gBAAC,GAAC,OAAE,YAAF,mBAAW;AAAA,OAAO,EAAE;AAAA,IAAA;AAAA,EAE9E;AAAA,EAEQ,MAAM;AACZ,WAAO,YAAY;EACrB;AACF;"}
@@ -0,0 +1,14 @@
1
+ import { get } from "./propAccess.js";
2
+ function makeSelector(selector) {
3
+ if (!selector) {
4
+ return (x) => x;
5
+ }
6
+ if (selector instanceof Function) {
7
+ return selector;
8
+ }
9
+ return (x) => get(x, selector);
10
+ }
11
+ export {
12
+ makeSelector
13
+ };
14
+ //# sourceMappingURL=makeSelector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeSelector.js","sources":["../../../src/lib/makeSelector.ts"],"sourcesContent":["import type { Path } from './path';\nimport { get } from './propAccess';\n\nexport function makeSelector<T, S>(selector?: ((value: T) => S) | Path<any>): (value: T) => S {\n if (!selector) {\n return (x) => x as any;\n }\n\n if (selector instanceof Function) {\n return selector;\n }\n\n return (x) => get(x, selector as any) as any;\n}\n"],"names":[],"mappings":";AAGO,SAAS,aAAmB,UAA2D;AAC5F,MAAI,CAAC,UAAU;AACb,WAAO,CAAC,MAAM;AAAA,EAChB;AAEA,MAAI,oBAAoB,UAAU;AACzB,WAAA;AAAA,EACT;AAEA,SAAO,CAAC,MAAM,IAAI,GAAG,QAAe;AACtC;"}
@@ -0,0 +1,66 @@
1
+ import { flatClone } from "./clone.js";
2
+ function castArrayPath(path) {
3
+ if (Array.isArray(path)) {
4
+ return path;
5
+ }
6
+ if (path === "") {
7
+ return [];
8
+ }
9
+ return path.split(".");
10
+ }
11
+ function get(object, path) {
12
+ const _path = castArrayPath(path);
13
+ const [first, ...rest] = _path;
14
+ if (first === void 0 || !object) {
15
+ return object;
16
+ }
17
+ if (object instanceof Map) {
18
+ return get(object.get(first), rest);
19
+ }
20
+ if (object instanceof Set) {
21
+ return get(Array.from(object)[Number(first)], rest);
22
+ }
23
+ if (object instanceof Object) {
24
+ return get(object[first], rest);
25
+ }
26
+ throw new Error(`Could not get ${path} of ${object}`);
27
+ }
28
+ function set(object, path, value, rootPath = path) {
29
+ const _path = castArrayPath(path);
30
+ const [first, ...rest] = _path;
31
+ if (first === void 0) {
32
+ return value;
33
+ }
34
+ const updateChild = (child) => {
35
+ if (!child && rest.length > 0) {
36
+ const _rootPath = castArrayPath(rootPath);
37
+ const prefix = _rootPath.slice(0, -rest.length);
38
+ throw new Error(`Cannot set ${rootPath} because ${prefix.join(".")} is ${child}`);
39
+ }
40
+ return set(child, rest, value, rootPath);
41
+ };
42
+ if (object instanceof Map) {
43
+ const copy = flatClone(object);
44
+ const child = copy.get(first);
45
+ copy.set(first, updateChild(child));
46
+ return copy;
47
+ }
48
+ if (object instanceof Set) {
49
+ const copy = [...object];
50
+ const child = copy[Number(first)];
51
+ copy[Number(first)] = updateChild(child);
52
+ return new Set(copy);
53
+ }
54
+ if (object instanceof Object) {
55
+ const copy = flatClone(object);
56
+ copy[first] = updateChild(copy[first]);
57
+ return copy;
58
+ }
59
+ throw new Error(`Could not set ${path} of ${object}`);
60
+ }
61
+ export {
62
+ castArrayPath,
63
+ get,
64
+ set
65
+ };
66
+ //# sourceMappingURL=propAccess.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"propAccess.js","sources":["../../../src/lib/propAccess.ts"],"sourcesContent":["import type { Update } from '../core/commonTypes';\nimport { flatClone } from './clone';\nimport type { Path, Value } from './path';\n\nexport function castArrayPath(path: string | KeyType[]): KeyType[] {\n if (Array.isArray(path)) {\n return path as any;\n }\n\n if (path === '') {\n return [] as any;\n }\n\n return (path as string).split('.') as any;\n}\n\nexport function get<T, P extends Path<T>>(object: T, path: P): Value<T, P> {\n const _path = castArrayPath(path as any);\n const [first, ...rest] = _path;\n\n if (first === undefined || !object) {\n return object as Value<T, P>;\n }\n\n if (object instanceof Map) {\n return get(object.get(first), rest as any);\n }\n\n if (object instanceof Set) {\n return get(Array.from(object)[Number(first)], rest as any);\n }\n\n if (object instanceof Object) {\n return get(object[first as keyof T], rest as any) as Value<T, P>;\n }\n\n throw new Error(`Could not get ${path} of ${object}`);\n}\n\nexport function set<T, P extends Path<T>>(\n object: T,\n path: P,\n value: Update<Value<T, P>>,\n rootPath = path,\n): T {\n const _path = castArrayPath(path as any);\n const [first, ...rest] = _path;\n\n if (first === undefined) {\n return value as any;\n }\n\n const updateChild = (child: any) => {\n if (!child && rest.length > 0) {\n const _rootPath = castArrayPath(rootPath as any);\n\n const prefix = _rootPath.slice(0, -rest.length) as KeyType[];\n throw new Error(`Cannot set ${rootPath} because ${prefix.join('.')} is ${child}`);\n }\n\n return set(child, rest as any, value, rootPath);\n };\n\n if (object instanceof Map) {\n const copy = flatClone(object);\n const child = copy.get(first);\n copy.set(first, updateChild(child));\n return copy;\n }\n\n if (object instanceof Set) {\n const copy = [...object];\n const child = copy[Number(first)];\n copy[Number(first)] = updateChild(child);\n return new Set(copy) as any;\n }\n\n if (object instanceof Object) {\n const copy = flatClone(object);\n copy[first as keyof T] = updateChild(copy[first as keyof T]);\n return copy;\n }\n\n throw new Error(`Could not set ${path} of ${object}`);\n}\n\nexport function remove<T, P extends Path<T, true>>(object: T, path: P): T {\n const _path = castArrayPath(path as any);\n\n if (_path.length === 0) {\n return undefined as any;\n }\n\n const parentPath = _path.slice(0, -1);\n const key = _path[_path.length - 1];\n\n const parent = flatClone(get(object, parentPath as any));\n\n if (parent instanceof Map) {\n parent.delete(key);\n } else if (parent instanceof Set) {\n const value = Array.from(parent)[Number(key)];\n parent.delete(value);\n }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UAAA,OAAO,UAAU,MAAM;AACvB,UAAA,QAAQ,KAAK,IAAI,KAAK;AAC5B,SAAK,IAAI,OAAO,YAAY,KAAK,CAAC;AAC3B,WAAA;AAAA,EACT;AAEA,MAAI,kBAAkB,KAAK;AACnB,UAAA,OAAO,CAAC,GAAG,MAAM;AACvB,UAAM,QAAQ,KAAK,OAAO,KAAK,CAAC;AAChC,SAAK,OAAO,KAAK,CAAC,IAAI,YAAY,KAAK;AAChC,WAAA,IAAI,IAAI,IAAI;AAAA,EACrB;AAEA,MAAI,kBAAkB,QAAQ;AACtB,UAAA,OAAO,UAAU,MAAM;AAC7B,SAAK,KAAgB,IAAI,YAAY,KAAK,KAAgB,CAAC;AACpD,WAAA;AAAA,EACT;AAEA,QAAM,IAAI,MAAM,iBAAiB,WAAW,QAAQ;AACtD;"}
@@ -0,0 +1,61 @@
1
+ function queue() {
2
+ const q = [];
3
+ const completionListeners = /* @__PURE__ */ new Set();
4
+ let active = false;
5
+ const notify = () => {
6
+ for (const listener of completionListeners) {
7
+ listener();
8
+ }
9
+ completionListeners.clear();
10
+ };
11
+ const run = async () => {
12
+ if (!active) {
13
+ active = true;
14
+ let next;
15
+ while (next = q.shift()) {
16
+ try {
17
+ let result = next.action();
18
+ if (result instanceof Promise) {
19
+ result = await result;
20
+ }
21
+ next.resolve(result);
22
+ } catch (error) {
23
+ next.reject(error);
24
+ }
25
+ }
26
+ active = false;
27
+ notify();
28
+ }
29
+ };
30
+ return Object.assign(
31
+ (action, ref) => {
32
+ return new Promise((resolve, reject) => {
33
+ q.push({ action, resolve, reject, ref });
34
+ run();
35
+ });
36
+ },
37
+ {
38
+ clear() {
39
+ q.length = 0;
40
+ },
41
+ whenDone() {
42
+ if (!active) {
43
+ return Promise.resolve();
44
+ }
45
+ return new Promise((resolve) => {
46
+ completionListeners.add(resolve);
47
+ });
48
+ },
49
+ get size() {
50
+ return q.length;
51
+ },
52
+ getRefs() {
53
+ return q.map((item) => item.ref).filter((x) => x !== void 0);
54
+ }
55
+ }
56
+ );
57
+ }
58
+ export {
59
+ queue
60
+ };
61
+ //# sourceMappingURL=queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sources":["../../../src/lib/queue.ts"],"sourcesContent":["import type { MaybePromise } from './maybePromise';\nimport type { Listener } from '@core';\n\ntype Action<T> = () => MaybePromise<T>;\n\nexport interface Queue {\n <T>(action: Action<T>, ref?: any): Promise<T>;\n clear: () => void;\n whenDone: () => Promise<void>;\n size: number;\n getRefs: () => any[];\n}\n\nexport function queue(): Queue {\n const q: {\n action: Action<any>;\n resolve: (value: any) => void;\n reject: (error: unknown) => void;\n ref?: any;\n }[] = [];\n const completionListeners = new Set<Listener<void>>();\n let active = false;\n\n const notify = () => {\n for (const listener of completionListeners) {\n listener();\n }\n\n completionListeners.clear();\n };\n\n const run = async () => {\n if (!active) {\n active = true;\n\n let next;\n while ((next = q.shift())) {\n try {\n let result = next.action();\n if (result instanceof Promise) {\n result = await result;\n }\n\n next.resolve(result);\n } catch (error) {\n next.reject(error);\n }\n }\n\n active = false;\n notify();\n }\n };\n\n return Object.assign(\n <T>(action: Action<T>, ref?: any) => {\n return new Promise<T>((resolve, reject) => {\n q.push({ action, resolve, reject, ref });\n run();\n });\n },\n {\n clear() {\n q.length = 0;\n },\n\n whenDone() {\n if (!active) {\n return Promise.resolve();\n }\n\n return new Promise<void>((resolve) => {\n completionListeners.add(resolve);\n });\n },\n\n get size() {\n return q.length;\n },\n\n getRefs() {\n return q.map((item) => item.ref).filter((x) => x !== undefined);\n },\n },\n );\n}\n"],"names":[],"mappings":"AAaO,SAAS,QAAe;AAC7B,QAAM,IAKA,CAAA;AACA,QAAA,0CAA0B;AAChC,MAAI,SAAS;AAEb,QAAM,SAAS,MAAM;AACnB,eAAW,YAAY,qBAAqB;AACjC;IACX;AAEA,wBAAoB,MAAM;AAAA,EAAA;AAG5B,QAAM,MAAM,YAAY;AACtB,QAAI,CAAC,QAAQ;AACF,eAAA;AAEL,UAAA;AACI,aAAA,OAAO,EAAE,SAAU;AACrB,YAAA;AACE,cAAA,SAAS,KAAK;AAClB,cAAI,kBAAkB,SAAS;AAC7B,qBAAS,MAAM;AAAA,UACjB;AAEA,eAAK,QAAQ,MAAM;AAAA,iBACZ;AACP,eAAK,OAAO,KAAK;AAAA,QACnB;AAAA,MACF;AAES,eAAA;AACF;IACT;AAAA,EAAA;AAGF,SAAO,OAAO;AAAA,IACZ,CAAI,QAAmB,QAAc;AACnC,aAAO,IAAI,QAAW,CAAC,SAAS,WAAW;AACzC,UAAE,KAAK,EAAE,QAAQ,SAAS,QAAQ,KAAK;AACnC;MAAA,CACL;AAAA,IACH;AAAA,IACA;AAAA,MACE,QAAQ;AACN,UAAE,SAAS;AAAA,MACb;AAAA,MAEA,WAAW;AACT,YAAI,CAAC,QAAQ;AACX,iBAAO,QAAQ;QACjB;AAEO,eAAA,IAAI,QAAc,CAAC,YAAY;AACpC,8BAAoB,IAAI,OAAO;AAAA,QAAA,CAChC;AAAA,MACH;AAAA,MAEA,IAAI,OAAO;AACT,eAAO,EAAE;AAAA,MACX;AAAA,MAEA,UAAU;AACD,eAAA,EAAE,IAAI,CAAC,SAAS,KAAK,GAAG,EAAE,OAAO,CAAC,MAAM,MAAM,MAAS;AAAA,MAChE;AAAA,IACF;AAAA,EAAA;AAEJ;"}
@@ -0,0 +1,78 @@
1
+ const createArrayAction = (prop) => function arrayAction(...args) {
2
+ const newArray = this.get().slice();
3
+ const result = newArray[prop](...args);
4
+ this.set(newArray);
5
+ return result;
6
+ };
7
+ const arrayMethods = {
8
+ splice: createArrayAction("splice"),
9
+ push: createArrayAction("push"),
10
+ pop: createArrayAction("pop"),
11
+ shift: createArrayAction("shift"),
12
+ unshift: createArrayAction("unshift"),
13
+ reverse: createArrayAction("reverse"),
14
+ sort: createArrayAction("sort")
15
+ };
16
+ const recordMethods = {
17
+ // set<T extends Record<any, any>, P extends Path<T>>(
18
+ // this: Store<T>,
19
+ // path: P,
20
+ // value: Update<Value<T, P>>,
21
+ // ) {
22
+ // if (value instanceof Function) {
23
+ // value = value(get(this.get(), path));
24
+ // }
25
+ // this.set(set(this.get(), path, value));
26
+ // return this;
27
+ // },
28
+ delete(key) {
29
+ const copy = { ...this.get() };
30
+ delete copy[key];
31
+ this.set(copy);
32
+ },
33
+ clear() {
34
+ this.set({});
35
+ }
36
+ };
37
+ const mapMethods = {
38
+ // set<K, V>(this: Store<Map<K, V>>, key: K, value: UpdateFrom<V, [V | undefined]>) {
39
+ // if (value instanceof Function) {
40
+ // value = value(this.get().get(key));
41
+ // }
42
+ // const newMap = new Map(this.get());
43
+ // newMap.set(key, value);
44
+ // this.set(newMap);
45
+ // return this;
46
+ // },
47
+ delete(key) {
48
+ const newMap = new Map(this.get());
49
+ const result = newMap.delete(key);
50
+ this.set(newMap);
51
+ return result;
52
+ },
53
+ clear() {
54
+ this.set(/* @__PURE__ */ new Map());
55
+ }
56
+ };
57
+ const setMethods = {
58
+ add(value) {
59
+ const newSet = new Set(this.get());
60
+ newSet.add(value);
61
+ this.set(newSet);
62
+ },
63
+ delete(value) {
64
+ const newSet = new Set(this.get());
65
+ newSet.delete(value);
66
+ this.set(newSet);
67
+ },
68
+ clear() {
69
+ this.set(/* @__PURE__ */ new Set());
70
+ }
71
+ };
72
+ export {
73
+ arrayMethods,
74
+ mapMethods,
75
+ recordMethods,
76
+ setMethods
77
+ };
78
+ //# sourceMappingURL=standardMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standardMethods.js","sources":["../../../src/lib/standardMethods.ts"],"sourcesContent":["import type { Store } from '../core/store';\nimport type { OptionalPropertyOf } from './typeHelpers';\n\ntype Function_ = (...args: any) => any;\n\nconst createArrayAction = <P extends keyof Array<any>>(prop: P) =>\n function arrayAction<T extends Array<any>>(\n this: Store<T>,\n ...args: T[P] extends Function_ ? Parameters<T[P]> : never\n ): T[P] extends Function_ ? ReturnType<T[P]> : never {\n const newArray = this.get().slice() as T;\n const result = (newArray[prop] as Function_)(...(args as any));\n this.set(newArray);\n return result;\n };\n\nexport const arrayMethods = {\n splice: createArrayAction('splice'),\n push: createArrayAction('push'),\n pop: createArrayAction('pop'),\n shift: createArrayAction('shift'),\n unshift: createArrayAction('unshift'),\n reverse: createArrayAction('reverse'),\n sort: createArrayAction('sort'),\n};\n\nexport const recordMethods = {\n // set<T extends Record<any, any>, P extends Path<T>>(\n // this: Store<T>,\n // path: P,\n // value: Update<Value<T, P>>,\n // ) {\n // if (value instanceof Function) {\n // value = value(get(this.get(), path));\n // }\n\n // this.set(set(this.get(), path, value));\n // return this;\n // },\n\n delete<T extends Record<any, any>, K extends OptionalPropertyOf<T>>(this: Store<T>, key: K) {\n const copy = { ...this.get() };\n delete copy[key];\n this.set(copy);\n },\n\n clear<T extends Record<any, any>>(this: Store<Partial<T>>) {\n this.set({} as T);\n },\n};\n\nexport const mapMethods = {\n // set<K, V>(this: Store<Map<K, V>>, key: K, value: UpdateFrom<V, [V | undefined]>) {\n // if (value instanceof Function) {\n // value = value(this.get().get(key));\n // }\n\n // const newMap = new Map(this.get());\n // newMap.set(key, value);\n // this.set(newMap);\n // return this;\n // },\n\n delete<K, V>(this: Store<Map<K, V>>, key: K) {\n const newMap = new Map(this.get());\n const result = newMap.delete(key);\n this.set(newMap);\n return result;\n },\n\n clear<K, V>(this: Store<Map<K, V>>) {\n this.set(new Map());\n },\n};\n\nexport const setMethods = {\n add<T>(this: Store<Set<T>>, value: T) {\n const newSet = new Set(this.get());\n newSet.add(value);\n this.set(newSet);\n },\n\n delete<T>(this: Store<Set<T>>, value: T) {\n const newSet = new Set(this.get());\n newSet.delete(value);\n this.set(newSet);\n },\n\n clear<T>(this: Store<Set<T>>) {\n this.set(new Set());\n },\n};\n"],"names":[],"mappings":"AAKA,MAAM,oBAAoB,CAA6B,SACrD,SAAS,eAEJ,MACgD;AACnD,QAAM,WAAW,KAAK,IAAI,EAAE,MAAM;AAClC,QAAM,SAAU,SAAS,IAAI,EAAgB,GAAI,IAAY;AAC7D,OAA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A,EACpB;AACF;"}
@@ -0,0 +1,25 @@
1
+ import { calcDuration } from "./calcDuration.js";
2
+ function throttle(action, duration) {
3
+ const ms = calcDuration(duration);
4
+ let t = 0;
5
+ let timeout;
6
+ return (...args) => {
7
+ if (timeout !== void 0) {
8
+ clearTimeout(timeout);
9
+ }
10
+ const dt = t + ms - Date.now();
11
+ if (dt <= 0) {
12
+ action(...args);
13
+ t = Date.now();
14
+ return;
15
+ }
16
+ timeout = setTimeout(() => {
17
+ action(...args);
18
+ t = Date.now();
19
+ }, dt);
20
+ };
21
+ }
22
+ export {
23
+ throttle
24
+ };
25
+ //# sourceMappingURL=throttle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.js","sources":["../../../src/lib/throttle.ts"],"sourcesContent":["import { calcDuration } from './calcDuration';\nimport type { Duration } from '@core';\n\nexport function throttle<Args extends any[]>(\n action: (...args: Args) => void,\n duration: Duration,\n): (...args: Args) => void {\n const ms = calcDuration(duration);\n\n let t = 0;\n let timeout: ReturnType<typeof setTimeout> | undefined;\n\n return (...args: Args) => {\n if (timeout !== undefined) {\n clearTimeout(timeout);\n }\n\n const dt = t + ms - Date.now();\n if (dt <= 0) {\n action(...args);\n t = Date.now();\n return;\n }\n\n timeout = setTimeout(() => {\n action(...args);\n t = Date.now();\n }, dt);\n };\n}\n"],"names":[],"mappings":";AAGgB,SAAA,SACd,QACA,UACyB;AACnB,QAAA,KAAK,aAAa,QAAQ;AAEhC,MAAI,IAAI;AACJ,MAAA;AAEJ,SAAO,IAAI,SAAe;AACxB,QAAI,YAAY,QAAW;AACzB,mBAAa,OAAO;AAAA,IACtB;AAEA,UAAM,KAAK,IAAI,KAAK,KAAK,IAAI;AAC7B,QAAI,MAAM,GAAG;AACX,aAAO,GAAG,IAAI;AACd,UAAI,KAAK;AACT;AAAA,IACF;AAEA,cAAU,WAAW,MAAM;AACzB,aAAO,GAAG,IAAI;AACd,UAAI,KAAK;OACR,EAAE;AAAA,EAAA;AAET;"}
@@ -0,0 +1,39 @@
1
+ const ProxyKeys = [
2
+ "get",
3
+ "getOwnPropertyDescriptor",
4
+ "getPrototypeOf",
5
+ "has",
6
+ "isExtensible",
7
+ "ownKeys"
8
+ ];
9
+ const isPlainObject = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
10
+ function trackingProxy(value) {
11
+ if (!isPlainObject(value) && !Array.isArray(value)) {
12
+ return [value, (other) => other === value];
13
+ }
14
+ const deps = new Array();
15
+ const proxy = new Proxy(
16
+ value,
17
+ Object.fromEntries(
18
+ ProxyKeys.map((key) => [
19
+ key,
20
+ (currentValue, ...args) => {
21
+ const function_ = Reflect[key];
22
+ const [proxiedValue, equals] = trackingProxy(function_(currentValue, ...args));
23
+ deps.push((otherValue) => {
24
+ if (!isPlainObject(otherValue) && !Array.isArray(otherValue)) {
25
+ return false;
26
+ }
27
+ return equals(function_(otherValue, ...args));
28
+ });
29
+ return proxiedValue;
30
+ }
31
+ ])
32
+ )
33
+ );
34
+ return [proxy, (other) => !!other && deps.every((equals) => equals(other))];
35
+ }
36
+ export {
37
+ trackingProxy
38
+ };
39
+ //# sourceMappingURL=trackingProxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trackingProxy.js","sources":["../../../src/lib/trackingProxy.ts"],"sourcesContent":["export type TrackingProxy<T> = [value: T, equals: (newValue: T) => boolean];\ntype Object_ = Record<string | symbol, unknown>;\n\nconst ProxyKeys = [\n 'get',\n 'getOwnPropertyDescriptor',\n 'getPrototypeOf',\n 'has',\n 'isExtensible',\n 'ownKeys',\n] as const;\n\nconst isPlainObject = (value: unknown) =>\n typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype;\n\nexport function trackingProxy<T>(value: T): TrackingProxy<T> {\n if (!isPlainObject(value) && !Array.isArray(value)) {\n return [value, (other) => other === value];\n }\n\n const deps = new Array<TrackingProxy<any>[1]>();\n\n const proxy = new Proxy(\n value as T & Object_,\n Object.fromEntries(\n ProxyKeys.map((key) => [\n key,\n (currentValue: T & Object_, ...args: any[]) => {\n const function_ = Reflect[key] as any;\n const [proxiedValue, equals] = trackingProxy(function_(currentValue, ...args));\n\n deps.push((otherValue) => {\n if (!isPlainObject(otherValue) && !Array.isArray(otherValue)) {\n return false;\n }\n\n return equals(function_(otherValue, ...args));\n });\n\n return proxiedValue;\n },\n ]),\n ),\n );\n\n return [proxy, (other) => !!other && deps.every((equals) => equals(other))];\n}\n"],"names":[],"mappings":"AAGA,MAAM,YAAY;AAAA,EAChB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,gBAAgB,CAAC,UACrB,OAAO,UAAU,YAAY,UAAU,QAAQ,OAAO,eAAe,KAAK,MAAM,OAAO;AAElF,SAAS,cAAiB,OAA4B;AACvD,MAAA,CAAC,cAAc,KAAK,KAAK,CAAC,MAAM,QAAQ,KAAK,GAAG;AAClD,WAAO,CAAC,OAAO,CAAC,UAAU,UAAU,KAAK;AAAA,EAC3C;AAEM,QAAA,OAAO,IAAI;AAEjB,QAAM,QAAQ,IAAI;AAAA,IAChB;AAAA,IACA,OAAO;AAAA,MACL,UAAU,IAAI,CAAC,QAAQ;AAAA,QACrB;AAAA,QACA,CAAC,iBAA8B,SAAgB;AACvC,gBAAA,YAAY,QAAQ,GAAG;AACvB,gBAAA,CAAC,cAAc,MAAM,IAAI,cAAc,UAAU,cAAc,GAAG,IAAI,CAAC;AAExE,eAAA,KAAK,CAAC,eAAe;AACpB,gBAAA,CAAC,cAAc,UAAU,KAAK,CAAC,MAAM,QAAQ,UAAU,GAAG;AACrD,qBAAA;AAAA,YACT;AAEA,mBAAO,OAAO,UAAU,YAAY,GAAG,IAAI,CAAC;AAAA,UAAA,CAC7C;AAEM,iBAAA;AAAA,QACT;AAAA,MAAA,CACD;AAAA,IACH;AAAA,EAAA;AAGF,SAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,SAAS,KAAK,MAAM,CAAC,WAAW,OAAO,KAAK,CAAC,CAAC;AAC5E;"}
@@ -0,0 +1,15 @@
1
+ import { useCache } from "./useCache.js";
2
+ function read(cache, options) {
3
+ const { status, value, error } = useCache(cache, options);
4
+ if (status === "value") {
5
+ return value;
6
+ }
7
+ if (status === "error") {
8
+ throw error;
9
+ }
10
+ throw cache.state.once((state) => state.status !== "pending");
11
+ }
12
+ export {
13
+ read
14
+ };
15
+ //# sourceMappingURL=read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","sources":["../../../src/react/read.ts"],"sourcesContent":["import { useCache } from './useCache';\nimport type { UseStoreOptions } from './useStore';\nimport type { Cache } from '@core';\n\nexport function read<T>(cache: Cache<T>, options?: UseStoreOptions): T {\n const { status, value, error } = useCache(cache, options);\n\n if (status === 'value') {\n return value;\n }\n\n if (status === 'error') {\n throw error;\n }\n\n throw cache.state.once((state) => state.status !== 'pending');\n}\n"],"names":[],"mappings":";AAIgB,SAAA,KAAQ,OAAiB,SAA8B;AACrE,QAAM,EAAE,QAAQ,OAAO,MAAU,IAAA,SAAS,OAAO,OAAO;AAExD,MAAI,WAAW,SAAS;AACf,WAAA;AAAA,EACT;AAEA,MAAI,WAAW,SAAS;AAChB,UAAA;AAAA,EACR;AAEA,QAAM,MAAM,MAAM,KAAK,CAAC,UAAU,MAAM,WAAW,SAAS;AAC9D;"}
@@ -0,0 +1,31 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useMemo, useContext, createContext } from "react";
3
+ import { createStore } from "../core/store.js";
4
+ const contextMap = /* @__PURE__ */ new WeakMap();
5
+ function getStoreScopeContext(scope) {
6
+ let context = contextMap.get(scope);
7
+ if (!context) {
8
+ context = createContext(createStore(scope.defaultValue));
9
+ contextMap.set(scope, context);
10
+ }
11
+ return context;
12
+ }
13
+ function StoreScopeProvider({ scope, store: inputStore, children }) {
14
+ const context = getStoreScopeContext(scope);
15
+ const currentStore = useMemo(
16
+ () => inputStore ?? createStore(scope.defaultValue),
17
+ [scope, inputStore]
18
+ );
19
+ return /* @__PURE__ */ jsx(context.Provider, { value: currentStore, children });
20
+ }
21
+ function useStoreScope(scope) {
22
+ const context = getStoreScopeContext(scope);
23
+ return useContext(context);
24
+ }
25
+ export {
26
+ StoreScopeProvider,
27
+ contextMap,
28
+ getStoreScopeContext,
29
+ useStoreScope
30
+ };
31
+ //# sourceMappingURL=storeScope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storeScope.js","sources":["../../../src/react/storeScope.tsx"],"sourcesContent":["import type { Context, ReactNode } from 'react';\nimport { createContext, useContext, useMemo } from 'react';\nimport type { Store } from '@core/store';\nimport { createStore } from '@core/store';\nimport type { StoreScope } from '@core/storeScope';\n\nexport type StoreScopeProps<T> = { scope: StoreScope<T>; store?: Store<T>; children?: ReactNode };\n\nexport const contextMap = new WeakMap<StoreScope<any>, Context<Store<any>>>();\n\nexport function getStoreScopeContext<T>(scope: StoreScope<T>): Context<Store<T>> {\n let context = contextMap.get(scope);\n\n if (!context) {\n context = createContext<Store<T>>(createStore(scope.defaultValue));\n contextMap.set(scope, context);\n }\n\n return context;\n}\n\nexport function StoreScopeProvider<T>({ scope, store: inputStore, children }: StoreScopeProps<T>) {\n const context = getStoreScopeContext(scope);\n const currentStore = useMemo(\n () => inputStore ?? createStore(scope.defaultValue),\n [scope, inputStore],\n );\n\n return <context.Provider value={currentStore}>{children}</context.Provider>;\n}\n\nexport function useStoreScope<T>(scope: StoreScope<T>): Store<T> {\n const context = getStoreScopeContext(scope);\n return useContext(context);\n}\n"],"names":[],"mappings":";;;AAQa,MAAA,iCAAiB,QAA8C;AAErE,SAAS,qBAAwB,OAAyC;AAC3E,MAAA,UAAU,WAAW,IAAI,KAAK;AAElC,MAAI,CAAC,SAAS;AACZ,cAAU,cAAwB,YAAY,MAAM,YAAY,CAAC;AACtD,eAAA,IAAI,OAAO,OAAO;AAAA,EAC/B;AAEO,SAAA;AACT;AAEO,SAAS,mBAAsB,EAAE,OAAO,OAAO,YAAY,YAAgC;AAC1F,QAAA,UAAU,qBAAqB,KAAK;AAC1C,QAAM,eAAe;AAAA,IACnB,MAAM,cAAc,YAAY,MAAM,YAAY;AAAA,IAClD,CAAC,OAAO,UAAU;AAAA,EAAA;AAGpB,6BAAQ,QAAQ,UAAR,EAAiB,OAAO,cAAe,SAAS,CAAA;AAC1D;AAEO,SAAS,cAAiB,OAAgC;AACzD,QAAA,UAAU,qBAAqB,KAAK;AAC1C,SAAO,WAAW,OAAO;AAC3B;"}
@@ -0,0 +1,31 @@
1
+ import { useMemo, useEffect } from "react";
2
+ import { useStore } from "./useStore.js";
3
+ import { makeSelector } from "../lib/makeSelector.js";
4
+ function useCache(cache, { passive, ...options } = {}) {
5
+ const mappedState = useMemo(() => {
6
+ var _a;
7
+ const rootCache = ((_a = cache.derivedFromCache) == null ? void 0 : _a.cache) ?? cache;
8
+ let selector = (x) => x;
9
+ if (cache.derivedFromCache) {
10
+ selector = (value) => {
11
+ for (const s of cache.derivedFromCache.selectors) {
12
+ value = makeSelector(s)(value);
13
+ }
14
+ return value;
15
+ };
16
+ }
17
+ return rootCache.state.map((state) => {
18
+ const value = state.status === "value" ? selector(state.value) : void 0;
19
+ return Object.assign(
20
+ [value, state.error, state.isUpdating, state.isStale],
21
+ { ...state, value }
22
+ );
23
+ });
24
+ }, [cache]);
25
+ useEffect(() => !passive ? cache.sub(() => void 0) : void 0, [cache, passive]);
26
+ return useStore(mappedState, options);
27
+ }
28
+ export {
29
+ useCache
30
+ };
31
+ //# sourceMappingURL=useCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCache.js","sources":["../../../src/react/useCache.ts"],"sourcesContent":["import { useEffect, useMemo } from 'react';\nimport type { UseStoreOptions } from './useStore';\nimport { useStore } from './useStore';\nimport type { Cache } from '@core';\nimport type { CacheState } from '@lib/cacheState';\nimport { makeSelector } from '@lib/makeSelector';\n\nexport type UseCacheArray<T> = [\n value: T | undefined,\n error: unknown | undefined,\n isUpdating: boolean,\n isStale: boolean,\n];\n\nexport type UseCacheValue<T> = UseCacheArray<T> & CacheState<T>;\n\nexport interface UseCacheOptions extends UseStoreOptions {\n passive?: boolean;\n}\n\nexport function useCache<T>(\n cache: Cache<T>,\n { passive, ...options }: UseCacheOptions = {},\n): UseCacheValue<T> {\n const mappedState = useMemo(() => {\n const rootCache: Cache<any> = cache.derivedFromCache?.cache ?? cache;\n let selector = (x: any) => x;\n\n if (cache.derivedFromCache) {\n selector = (value: any) => {\n for (const s of cache.derivedFromCache!.selectors) {\n value = makeSelector(s)(value);\n }\n return value;\n };\n }\n\n return rootCache.state.map((state) => {\n const value = state.status === 'value' ? selector(state.value) : undefined;\n\n return Object.assign<UseCacheArray<T>, CacheState<T>>(\n [value, state.error, state.isUpdating, state.isStale],\n { ...state, value },\n );\n });\n }, [cache]);\n\n useEffect(() => (!passive ? cache.sub(() => undefined) : undefined), [cache, passive]);\n\n return useStore(mappedState, options);\n}\n"],"names":[],"mappings":";;;AAoBgB,SAAA,SACd,OACA,EAAE,SAAS,GAAG,QAAQ,IAAqB,CAAA,GACzB;AACZ,QAAA,cAAc,QAAQ,MAAM;;AAC1B,UAAA,cAAwB,WAAM,qBAAN,mBAAwB,UAAS;AAC3D,QAAA,WAAW,CAAC,MAAW;AAE3B,QAAI,MAAM,kBAAkB;AAC1B,iBAAW,CAAC,UAAe;AACd,mBAAA,KAAK,MAAM,iBAAkB,WAAW;AACzC,kBAAA,aAAa,CAAC,EAAE,KAAK;AAAA,QAC/B;AACO,eAAA;AAAA,MAAA;AAAA,IAEX;AAEA,WAAO,UAAU,MAAM,IAAI,CAAC,UAAU;AACpC,YAAM,QAAQ,MAAM,WAAW,UAAU,SAAS,MAAM,KAAK,IAAI;AAEjE,aAAO,OAAO;AAAA,QACZ,CAAC,OAAO,MAAM,OAAO,MAAM,YAAY,MAAM,OAAO;AAAA,QACpD,EAAE,GAAG,OAAO,MAAM;AAAA,MAAA;AAAA,IACpB,CACD;AAAA,EAAA,GACA,CAAC,KAAK,CAAC;AAEV,YAAU,MAAO,CAAC,UAAU,MAAM,IAAI,MAAM,MAAS,IAAI,QAAY,CAAC,OAAO,OAAO,CAAC;AAE9E,SAAA,SAAS,aAAa,OAAO;AACtC;"}
@@ -0,0 +1,9 @@
1
+ import { useStore } from "./useStore.js";
2
+ function useProp(store, options) {
3
+ const value = useStore(store, options);
4
+ return [value, store.set];
5
+ }
6
+ export {
7
+ useProp
8
+ };
9
+ //# sourceMappingURL=useProp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useProp.js","sources":["../../../src/react/useProp.ts"],"sourcesContent":["import type { UseStoreOptions } from './useStore';\nimport { useStore } from './useStore';\nimport type { Store } from '@core/store';\nimport type { UpdateFunction } from '@core/commonTypes';\n\nexport function useProp<T>(\n store: Store<T>,\n options?: UseStoreOptions,\n): [value: T, setValue: UpdateFunction<T>] {\n const value = useStore(store, options);\n\n return [value, store.set];\n}\n"],"names":[],"mappings":";AAKgB,SAAA,QACd,OACA,SACyC;AACnC,QAAA,QAAQ,SAAS,OAAO,OAAO;AAE9B,SAAA,CAAC,OAAO,MAAM,GAAG;AAC1B;"}
@@ -0,0 +1,51 @@
1
+ import { useRef, useMemo, useCallback, useLayoutEffect, useDebugValue } from "react";
2
+ import "../_virtual/with-selector.js";
3
+ import { hash } from "../lib/hash.js";
4
+ import { makeSelector } from "../lib/makeSelector.js";
5
+ import { trackingProxy } from "../lib/trackingProxy.js";
6
+ import { exports as withSelectorExports } from "../_virtual/with-selector2.js";
7
+ function useStore(store, options) {
8
+ const lastEqualsRef = useRef();
9
+ const { rootStore, selector } = useMemo(() => {
10
+ var _a;
11
+ const rootStore2 = ((_a = store.derivedFrom) == null ? void 0 : _a.store) ?? store;
12
+ let selector2 = (x) => x;
13
+ if (store.derivedFrom) {
14
+ selector2 = (value2) => {
15
+ for (const s of store.derivedFrom.selectors) {
16
+ value2 = makeSelector(s)(value2);
17
+ }
18
+ return value2;
19
+ };
20
+ }
21
+ return { rootStore: rootStore2, selector: selector2 };
22
+ }, [store]);
23
+ const subOptions = { ...options, runNow: false, equals: void 0, passive: false };
24
+ const subscribe = useCallback(
25
+ (listener) => {
26
+ return rootStore.sub(listener, subOptions);
27
+ },
28
+ [rootStore, hash(subOptions)]
29
+ );
30
+ const value = withSelectorExports.useSyncExternalStoreWithSelector(
31
+ //
32
+ subscribe,
33
+ rootStore.get,
34
+ void 0,
35
+ selector,
36
+ (options == null ? void 0 : options.equals) ?? ((_v, newValue) => {
37
+ var _a;
38
+ return ((_a = lastEqualsRef.current) == null ? void 0 : _a.call(lastEqualsRef, newValue)) ?? false;
39
+ })
40
+ );
41
+ const [proxiedValue, equals] = trackingProxy(value);
42
+ useLayoutEffect(() => {
43
+ lastEqualsRef.current = equals;
44
+ });
45
+ useDebugValue(value);
46
+ return proxiedValue;
47
+ }
48
+ export {
49
+ useStore
50
+ };
51
+ //# sourceMappingURL=useStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStore.js","sources":["../../../src/react/useStore.ts"],"sourcesContent":["import { useCallback, useDebugValue, useLayoutEffect, useMemo, useRef } from 'react';\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector.js';\nimport type { SubscribeOptions } from '@core/commonTypes';\nimport type { Store } from '@core/store';\nimport { hash } from '@lib/hash';\nimport { makeSelector } from '@lib/makeSelector';\nimport { trackingProxy } from '@lib/trackingProxy';\n\nexport type UseStoreOptions = Omit<SubscribeOptions, 'runNow' | 'passive'>;\n\nexport function useStore<T>(store: Store<T>, options?: UseStoreOptions): T {\n const lastEqualsRef = useRef<(newValue: T) => boolean>();\n\n const { rootStore, selector } = useMemo(() => {\n const rootStore = store.derivedFrom?.store ?? store;\n let selector = (x: any) => x;\n\n if (store.derivedFrom) {\n selector = (value: any) => {\n for (const s of store.derivedFrom!.selectors) {\n value = makeSelector(s)(value);\n }\n return value;\n };\n }\n\n return { rootStore, selector };\n }, [store]);\n\n const subOptions = { ...options, runNow: false, equals: undefined, passive: false };\n const subscribe = useCallback(\n (listener: () => void) => {\n return rootStore.sub(listener, subOptions);\n },\n [rootStore, hash(subOptions)],\n );\n\n const value = useSyncExternalStoreWithSelector<unknown, T>(\n //\n subscribe,\n rootStore.get,\n undefined,\n selector,\n options?.equals ?? ((_v, newValue) => lastEqualsRef.current?.(newValue) ?? false),\n );\n const [proxiedValue, equals] = trackingProxy(value);\n\n useLayoutEffect(() => {\n lastEqualsRef.current = equals;\n });\n\n useDebugValue(value);\n return proxiedValue;\n}\n"],"names":["rootStore","selector","value","useSyncExternalStoreWithSelector"],"mappings":";;;;;;AAUgB,SAAA,SAAY,OAAiB,SAA8B;AACzE,QAAM,gBAAgB;AAEtB,QAAM,EAAE,WAAW,SAAS,IAAI,QAAQ,MAAM;;AACtCA,UAAAA,eAAY,WAAM,gBAAN,mBAAmB,UAAS;AAC1CC,QAAAA,YAAW,CAAC,MAAW;AAE3B,QAAI,MAAM,aAAa;AACrBA,kBAAW,CAACC,WAAe;AACd,mBAAA,KAAK,MAAM,YAAa,WAAW;AAC5CA,mBAAQ,aAAa,CAAC,EAAEA,MAAK;AAAA,QAC/B;AACOA,eAAAA;AAAAA,MAAA;AAAA,IAEX;AAEA,WAAO,EAAE,WAAAF,YAAW,UAAAC,UAAS;AAAA,EAAA,GAC5B,CAAC,KAAK,CAAC;AAEJ,QAAA,aAAa,EAAE,GAAG,SAAS,QAAQ,OAAO,QAAQ,QAAW,SAAS;AAC5E,QAAM,YAAY;AAAA,IAChB,CAAC,aAAyB;AACjB,aAAA,UAAU,IAAI,UAAU,UAAU;AAAA,IAC3C;AAAA,IACA,CAAC,WAAW,KAAK,UAAU,CAAC;AAAA,EAAA;AAG9B,QAAM,QAAQE,oBAAA;AAAA;AAAA,IAEZ;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,KACA,mCAAS,YAAW,CAAC,IAAI,aAAa;;AAAA,kCAAc,YAAd,uCAAwB,cAAa;AAAA;AAAA,EAAA;AAE7E,QAAM,CAAC,cAAc,MAAM,IAAI,cAAc,KAAK;AAElD,kBAAgB,MAAM;AACpB,kBAAc,UAAU;AAAA,EAAA,CACzB;AAED,gBAAc,KAAK;AACZ,SAAA;AACT;"}
@@ -0,0 +1,14 @@
1
+ import { read } from "./react/read.js";
2
+ import { StoreScopeProvider, useStoreScope } from "./react/storeScope.js";
3
+ import { useCache } from "./react/useCache.js";
4
+ import { useProp } from "./react/useProp.js";
5
+ import { useStore } from "./react/useStore.js";
6
+ export {
7
+ StoreScopeProvider,
8
+ read,
9
+ useCache,
10
+ useProp,
11
+ useStore,
12
+ useStoreScope
13
+ };
14
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}