tinybase 5.0.0-beta.27 → 5.0.0-beta.28

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 (249) hide show
  1. package/@types/_internal/ui-react/with-schemas/index.d.cts +274 -0
  2. package/@types/_internal/ui-react/with-schemas/index.d.ts +274 -0
  3. package/@types/checkpoints/index.d.cts +30 -1
  4. package/@types/checkpoints/index.d.ts +30 -1
  5. package/@types/checkpoints/with-schemas/index.d.cts +30 -1
  6. package/@types/checkpoints/with-schemas/index.d.ts +30 -1
  7. package/@types/common/index.d.cts +9 -0
  8. package/@types/common/index.d.ts +9 -0
  9. package/@types/common/with-schemas/index.d.cts +9 -0
  10. package/@types/common/with-schemas/index.d.ts +9 -0
  11. package/@types/index.d.cts +1 -0
  12. package/@types/index.d.ts +1 -0
  13. package/@types/indexes/index.d.cts +32 -1
  14. package/@types/indexes/index.d.ts +32 -1
  15. package/@types/indexes/with-schemas/index.d.cts +32 -1
  16. package/@types/indexes/with-schemas/index.d.ts +32 -1
  17. package/@types/mergeable-store/index.d.cts +0 -2
  18. package/@types/mergeable-store/index.d.ts +0 -2
  19. package/@types/mergeable-store/with-schemas/index.d.cts +2 -2
  20. package/@types/mergeable-store/with-schemas/index.d.ts +2 -2
  21. package/@types/metrics/index.d.cts +30 -4
  22. package/@types/metrics/index.d.ts +30 -4
  23. package/@types/metrics/with-schemas/index.d.cts +30 -4
  24. package/@types/metrics/with-schemas/index.d.ts +30 -4
  25. package/@types/persisters/index.d.cts +87 -25
  26. package/@types/persisters/index.d.ts +87 -25
  27. package/@types/persisters/persister-automerge/index.d.cts +4 -4
  28. package/@types/persisters/persister-automerge/index.d.ts +4 -4
  29. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +4 -4
  30. package/@types/persisters/persister-automerge/with-schemas/index.d.ts +4 -4
  31. package/@types/persisters/persister-browser/index.d.cts +7 -4
  32. package/@types/persisters/persister-browser/index.d.ts +7 -4
  33. package/@types/persisters/persister-browser/with-schemas/index.d.cts +7 -4
  34. package/@types/persisters/persister-browser/with-schemas/index.d.ts +7 -4
  35. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +4 -4
  36. package/@types/persisters/persister-cr-sqlite-wasm/index.d.ts +4 -4
  37. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +4 -4
  38. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.ts +4 -4
  39. package/@types/persisters/persister-electric-sql/index.d.cts +4 -4
  40. package/@types/persisters/persister-electric-sql/index.d.ts +4 -4
  41. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +4 -4
  42. package/@types/persisters/persister-electric-sql/with-schemas/index.d.ts +4 -4
  43. package/@types/persisters/persister-expo-sqlite/index.d.cts +4 -4
  44. package/@types/persisters/persister-expo-sqlite/index.d.ts +4 -4
  45. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +4 -4
  46. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.ts +4 -4
  47. package/@types/persisters/persister-file/index.d.cts +6 -4
  48. package/@types/persisters/persister-file/index.d.ts +6 -4
  49. package/@types/persisters/persister-file/with-schemas/index.d.cts +6 -4
  50. package/@types/persisters/persister-file/with-schemas/index.d.ts +6 -4
  51. package/@types/persisters/persister-indexed-db/index.d.cts +5 -4
  52. package/@types/persisters/persister-indexed-db/index.d.ts +5 -4
  53. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +5 -4
  54. package/@types/persisters/persister-indexed-db/with-schemas/index.d.ts +5 -4
  55. package/@types/persisters/persister-libsql/index.d.cts +5 -4
  56. package/@types/persisters/persister-libsql/index.d.ts +5 -4
  57. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +5 -4
  58. package/@types/persisters/persister-libsql/with-schemas/index.d.ts +5 -4
  59. package/@types/persisters/persister-partykit-client/index.d.cts +12 -6
  60. package/@types/persisters/persister-partykit-client/index.d.ts +12 -6
  61. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +12 -6
  62. package/@types/persisters/persister-partykit-client/with-schemas/index.d.ts +12 -6
  63. package/@types/persisters/persister-partykit-server/index.d.cts +11 -3
  64. package/@types/persisters/persister-partykit-server/index.d.ts +11 -3
  65. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +11 -3
  66. package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +11 -3
  67. package/@types/persisters/persister-powersync/index.d.cts +5 -4
  68. package/@types/persisters/persister-powersync/index.d.ts +5 -4
  69. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +5 -4
  70. package/@types/persisters/persister-powersync/with-schemas/index.d.ts +5 -4
  71. package/@types/persisters/persister-remote/index.d.cts +6 -4
  72. package/@types/persisters/persister-remote/index.d.ts +6 -4
  73. package/@types/persisters/persister-remote/with-schemas/index.d.cts +6 -4
  74. package/@types/persisters/persister-remote/with-schemas/index.d.ts +6 -4
  75. package/@types/persisters/persister-sqlite-wasm/index.d.cts +4 -4
  76. package/@types/persisters/persister-sqlite-wasm/index.d.ts +4 -4
  77. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +4 -4
  78. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.ts +4 -4
  79. package/@types/persisters/persister-sqlite3/index.d.cts +4 -4
  80. package/@types/persisters/persister-sqlite3/index.d.ts +4 -4
  81. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +4 -4
  82. package/@types/persisters/persister-sqlite3/with-schemas/index.d.ts +4 -4
  83. package/@types/persisters/persister-yjs/index.d.cts +4 -4
  84. package/@types/persisters/persister-yjs/index.d.ts +4 -4
  85. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +4 -4
  86. package/@types/persisters/persister-yjs/with-schemas/index.d.ts +4 -4
  87. package/@types/persisters/with-schemas/index.d.cts +87 -25
  88. package/@types/persisters/with-schemas/index.d.ts +87 -25
  89. package/@types/queries/index.d.cts +64 -4
  90. package/@types/queries/index.d.ts +64 -4
  91. package/@types/queries/with-schemas/index.d.cts +64 -4
  92. package/@types/queries/with-schemas/index.d.ts +64 -4
  93. package/@types/relationships/index.d.cts +34 -1
  94. package/@types/relationships/index.d.ts +34 -1
  95. package/@types/relationships/with-schemas/index.d.cts +34 -1
  96. package/@types/relationships/with-schemas/index.d.ts +34 -1
  97. package/@types/store/index.d.cts +125 -0
  98. package/@types/store/index.d.ts +125 -0
  99. package/@types/store/with-schemas/index.d.cts +125 -0
  100. package/@types/store/with-schemas/index.d.ts +125 -0
  101. package/@types/synchronizers/index.d.cts +32 -10
  102. package/@types/synchronizers/index.d.ts +32 -10
  103. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +114 -0
  104. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.ts +114 -0
  105. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +128 -0
  106. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.ts +128 -0
  107. package/@types/synchronizers/synchronizer-local/index.d.cts +16 -17
  108. package/@types/synchronizers/synchronizer-local/index.d.ts +16 -17
  109. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +16 -17
  110. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.ts +16 -17
  111. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +66 -6
  112. package/@types/synchronizers/synchronizer-ws-client/index.d.ts +66 -6
  113. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +77 -6
  114. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.ts +77 -6
  115. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +486 -7
  116. package/@types/synchronizers/synchronizer-ws-server/index.d.ts +486 -7
  117. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +486 -7
  118. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.ts +486 -7
  119. package/@types/synchronizers/with-schemas/index.d.cts +29 -11
  120. package/@types/synchronizers/with-schemas/index.d.ts +29 -11
  121. package/@types/tools/index.d.cts +22 -0
  122. package/@types/tools/index.d.ts +22 -0
  123. package/@types/tools/with-schemas/index.d.cts +22 -0
  124. package/@types/tools/with-schemas/index.d.ts +22 -0
  125. package/@types/ui-react/index.d.cts +375 -1
  126. package/@types/ui-react/index.d.ts +375 -1
  127. package/@types/ui-react/with-schemas/index.d.cts +100 -1
  128. package/@types/ui-react/with-schemas/index.d.ts +100 -1
  129. package/@types/ui-react-dom/index.d.cts +112 -0
  130. package/@types/ui-react-dom/index.d.ts +112 -0
  131. package/@types/ui-react-dom/with-schemas/index.d.cts +112 -0
  132. package/@types/ui-react-dom/with-schemas/index.d.ts +112 -0
  133. package/@types/ui-react-inspector/index.d.cts +6 -2
  134. package/@types/ui-react-inspector/index.d.ts +6 -2
  135. package/@types/ui-react-inspector/with-schemas/index.d.cts +118 -2
  136. package/@types/ui-react-inspector/with-schemas/index.d.ts +118 -2
  137. package/@types/with-schemas/index.d.cts +1 -0
  138. package/@types/with-schemas/index.d.ts +1 -0
  139. package/cjs/es6/index.cjs +2 -2
  140. package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/index.cjs +1 -0
  141. package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/index.cjs.gz +0 -0
  142. package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +1 -0
  143. package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs.gz +0 -0
  144. package/cjs/es6/min/synchronizers/synchronizer-ws-client/index.cjs +1 -1
  145. package/cjs/es6/min/synchronizers/synchronizer-ws-client/index.cjs.gz +0 -0
  146. package/cjs/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +1 -1
  147. package/cjs/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs.gz +0 -0
  148. package/cjs/es6/synchronizers/index.cjs +2 -2
  149. package/cjs/es6/synchronizers/synchronizer-broadcast-channel/index.cjs +628 -0
  150. package/cjs/es6/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +628 -0
  151. package/cjs/es6/synchronizers/synchronizer-local/index.cjs +4 -4
  152. package/cjs/es6/synchronizers/synchronizer-local/with-schemas/index.cjs +4 -4
  153. package/cjs/es6/synchronizers/synchronizer-ws-client/index.cjs +15 -21
  154. package/cjs/es6/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +15 -21
  155. package/cjs/es6/synchronizers/with-schemas/index.cjs +2 -2
  156. package/cjs/es6/with-schemas/index.cjs +2 -2
  157. package/cjs/index.cjs +2 -2
  158. package/cjs/min/synchronizers/synchronizer-broadcast-channel/index.cjs +1 -0
  159. package/cjs/min/synchronizers/synchronizer-broadcast-channel/index.cjs.gz +0 -0
  160. package/cjs/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +1 -0
  161. package/cjs/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs.gz +0 -0
  162. package/cjs/min/synchronizers/synchronizer-ws-client/index.cjs +1 -1
  163. package/cjs/min/synchronizers/synchronizer-ws-client/index.cjs.gz +0 -0
  164. package/cjs/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +1 -1
  165. package/cjs/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs.gz +0 -0
  166. package/cjs/synchronizers/index.cjs +2 -2
  167. package/cjs/synchronizers/synchronizer-broadcast-channel/index.cjs +500 -0
  168. package/cjs/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +500 -0
  169. package/cjs/synchronizers/synchronizer-local/index.cjs +4 -4
  170. package/cjs/synchronizers/synchronizer-local/with-schemas/index.cjs +4 -4
  171. package/cjs/synchronizers/synchronizer-ws-client/index.cjs +15 -21
  172. package/cjs/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +15 -21
  173. package/cjs/synchronizers/with-schemas/index.cjs +2 -2
  174. package/cjs/with-schemas/index.cjs +2 -2
  175. package/es6/index.js +2 -2
  176. package/es6/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
  177. package/es6/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
  178. package/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
  179. package/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
  180. package/es6/min/synchronizers/synchronizer-ws-client/index.js +1 -1
  181. package/es6/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
  182. package/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
  183. package/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
  184. package/es6/synchronizers/index.js +2 -2
  185. package/es6/synchronizers/synchronizer-broadcast-channel/index.js +626 -0
  186. package/es6/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +626 -0
  187. package/es6/synchronizers/synchronizer-local/index.js +4 -4
  188. package/es6/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
  189. package/es6/synchronizers/synchronizer-ws-client/index.js +15 -21
  190. package/es6/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
  191. package/es6/synchronizers/with-schemas/index.js +2 -2
  192. package/es6/with-schemas/index.js +2 -2
  193. package/index.js +2 -2
  194. package/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
  195. package/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
  196. package/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
  197. package/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
  198. package/min/synchronizers/synchronizer-ws-client/index.js +1 -1
  199. package/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
  200. package/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
  201. package/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
  202. package/package.json +105 -1
  203. package/readme.md +9 -9
  204. package/releases.md +4 -4
  205. package/synchronizers/index.js +2 -2
  206. package/synchronizers/synchronizer-broadcast-channel/index.js +498 -0
  207. package/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +498 -0
  208. package/synchronizers/synchronizer-local/index.js +4 -4
  209. package/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
  210. package/synchronizers/synchronizer-ws-client/index.js +15 -21
  211. package/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
  212. package/synchronizers/with-schemas/index.js +2 -2
  213. package/umd/es6/index.js +2 -2
  214. package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
  215. package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
  216. package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
  217. package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
  218. package/umd/es6/min/synchronizers/synchronizer-ws-client/index.js +1 -1
  219. package/umd/es6/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
  220. package/umd/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
  221. package/umd/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
  222. package/umd/es6/synchronizers/index.js +2 -2
  223. package/umd/es6/synchronizers/synchronizer-broadcast-channel/index.js +654 -0
  224. package/umd/es6/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +654 -0
  225. package/umd/es6/synchronizers/synchronizer-local/index.js +4 -4
  226. package/umd/es6/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
  227. package/umd/es6/synchronizers/synchronizer-ws-client/index.js +15 -21
  228. package/umd/es6/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
  229. package/umd/es6/synchronizers/with-schemas/index.js +2 -2
  230. package/umd/es6/with-schemas/index.js +2 -2
  231. package/umd/index.js +2 -2
  232. package/umd/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
  233. package/umd/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
  234. package/umd/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
  235. package/umd/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
  236. package/umd/min/synchronizers/synchronizer-ws-client/index.js +1 -1
  237. package/umd/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
  238. package/umd/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
  239. package/umd/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
  240. package/umd/synchronizers/index.js +2 -2
  241. package/umd/synchronizers/synchronizer-broadcast-channel/index.js +516 -0
  242. package/umd/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +516 -0
  243. package/umd/synchronizers/synchronizer-local/index.js +4 -4
  244. package/umd/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
  245. package/umd/synchronizers/synchronizer-ws-client/index.js +15 -21
  246. package/umd/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
  247. package/umd/synchronizers/with-schemas/index.js +2 -2
  248. package/umd/with-schemas/index.js +2 -2
  249. package/with-schemas/index.js +2 -2
@@ -0,0 +1,498 @@
1
+ const EMPTY_STRING = '';
2
+
3
+ const promise = Promise;
4
+ const GLOBAL = globalThis;
5
+ const math = Math;
6
+ const mathFloor = math.floor;
7
+ const isUndefined = (thing) => thing == void 0;
8
+ const ifNotUndefined = (value, then, otherwise) =>
9
+ isUndefined(value) ? otherwise?.() : then(value);
10
+ const isArray = (thing) => Array.isArray(thing);
11
+ const size = (arrayOrString) => arrayOrString.length;
12
+ const promiseNew = (resolver) => new promise(resolver);
13
+ const errorNew = (message) => {
14
+ throw new Error(message);
15
+ };
16
+
17
+ const arrayForEach = (array, cb) => array.forEach(cb);
18
+ const arrayMap = (array, cb) => array.map(cb);
19
+ const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
20
+ const arrayPush = (array, ...values) => array.push(...values);
21
+ const arrayShift = (array) => array.shift();
22
+
23
+ const object = Object;
24
+ const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
25
+ const objEntries = object.entries;
26
+ const isObject = (obj) =>
27
+ !isUndefined(obj) &&
28
+ ifNotUndefined(
29
+ getPrototypeOf(obj),
30
+ (objPrototype) =>
31
+ objPrototype == object.prototype ||
32
+ isUndefined(getPrototypeOf(objPrototype)),
33
+
34
+ /* istanbul ignore next */
35
+ () => true,
36
+ );
37
+ const objIds = object.keys;
38
+ const objFreeze = object.freeze;
39
+ const objNew = (entries = []) => object.fromEntries(entries);
40
+ const objHas = (obj, id) => id in obj;
41
+ const objForEach = (obj, cb) =>
42
+ arrayForEach(objEntries(obj), ([id, value]) => cb(value, id));
43
+ const objSize = (obj) => size(objIds(obj));
44
+ const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
45
+ const objEnsure = (obj, id, getDefaultValue) => {
46
+ if (!objHas(obj, id)) {
47
+ obj[id] = getDefaultValue();
48
+ }
49
+ return obj[id];
50
+ };
51
+
52
+ const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
53
+ const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
54
+
55
+ const mapNew = (entries) => new Map(entries);
56
+ const mapGet = (map, key) => map?.get(key);
57
+ const mapSet = (map, key, value) =>
58
+ isUndefined(value) ? (collDel(map, key), map) : map?.set(key, value);
59
+ const mapEnsure = (map, key, getDefaultValue, hadExistingValue) => {
60
+ if (!collHas(map, key)) {
61
+ mapSet(map, key, getDefaultValue());
62
+ }
63
+ return mapGet(map, key);
64
+ };
65
+
66
+ const Persists = {
67
+ StoreOnly: 1,
68
+ MergeableStoreOnly: 2,
69
+ StoreOrMergeableStore: 3,
70
+ };
71
+ const scheduleRunning = mapNew();
72
+ const scheduleActions = mapNew();
73
+ const getStoreFunctions = (persist = Persists.StoreOnly, store) =>
74
+ persist != Persists.StoreOnly && store.isMergeable()
75
+ ? [
76
+ 1,
77
+ store.getMergeableContent,
78
+ store.getTransactionMergeableChanges,
79
+ ([[changedTables], [changedValues]]) =>
80
+ !objIsEmpty(changedTables) || !objIsEmpty(changedValues),
81
+ store.setDefaultContent,
82
+ ]
83
+ : persist != Persists.MergeableStoreOnly
84
+ ? [
85
+ 0,
86
+ store.getContent,
87
+ store.getTransactionChanges,
88
+ ([changedTables, changedValues]) =>
89
+ !objIsEmpty(changedTables) || !objIsEmpty(changedValues),
90
+ store.setContent,
91
+ ]
92
+ : errorNew('Store type not supported by this Persister');
93
+ const createCustomPersister = (
94
+ store,
95
+ getPersisted,
96
+ setPersisted,
97
+ addPersisterListener,
98
+ delPersisterListener,
99
+ onIgnoredError,
100
+ persist,
101
+ extra = {},
102
+ scheduleId = [],
103
+ ) => {
104
+ let loadSave = 0;
105
+ let loads = 0;
106
+ let saves = 0;
107
+ let action;
108
+ let autoLoadHandle;
109
+ let autoSaveListenerId;
110
+ mapEnsure(scheduleRunning, scheduleId, () => 0);
111
+ mapEnsure(scheduleActions, scheduleId, () => []);
112
+ const [
113
+ isMergeableStore,
114
+ getContent,
115
+ getChanges,
116
+ hasChanges,
117
+ setDefaultContent,
118
+ ] = getStoreFunctions(persist, store);
119
+ const run = async () => {
120
+ /* istanbul ignore else */
121
+ if (!mapGet(scheduleRunning, scheduleId)) {
122
+ mapSet(scheduleRunning, scheduleId, 1);
123
+ while (
124
+ !isUndefined((action = arrayShift(mapGet(scheduleActions, scheduleId))))
125
+ ) {
126
+ try {
127
+ await action();
128
+ } catch (error) {
129
+ /* istanbul ignore next */
130
+ onIgnoredError?.(error);
131
+ }
132
+ }
133
+ mapSet(scheduleRunning, scheduleId, 0);
134
+ }
135
+ };
136
+ const setContentOrChanges = (contentOrChanges) => {
137
+ (isMergeableStore && isArray(contentOrChanges?.[0])
138
+ ? contentOrChanges?.[2] === 1
139
+ ? store.applyMergeableChanges
140
+ : store.setMergeableContent
141
+ : contentOrChanges?.[2] === 1
142
+ ? store.applyChanges
143
+ : store.setContent)(contentOrChanges);
144
+ };
145
+ const load = async (initialContent) => {
146
+ /* istanbul ignore else */
147
+ if (loadSave != 2) {
148
+ loadSave = 1;
149
+ loads++;
150
+ await schedule(async () => {
151
+ try {
152
+ setContentOrChanges(await getPersisted());
153
+ } catch (error) {
154
+ onIgnoredError?.(error);
155
+ if (initialContent) {
156
+ setDefaultContent(initialContent);
157
+ }
158
+ }
159
+ loadSave = 0;
160
+ });
161
+ }
162
+ return persister;
163
+ };
164
+ const startAutoLoad = async (initialContent) => {
165
+ await stopAutoLoad().load(initialContent);
166
+ autoLoadHandle = addPersisterListener(async (content, changes) => {
167
+ if (changes || content) {
168
+ /* istanbul ignore else */
169
+ if (loadSave != 2) {
170
+ loadSave = 1;
171
+ loads++;
172
+ setContentOrChanges(changes ?? content);
173
+ loadSave = 0;
174
+ }
175
+ } else {
176
+ await load();
177
+ }
178
+ });
179
+ return persister;
180
+ };
181
+ const stopAutoLoad = () => {
182
+ if (autoLoadHandle) {
183
+ delPersisterListener(autoLoadHandle);
184
+ autoLoadHandle = void 0;
185
+ }
186
+ return persister;
187
+ };
188
+ const isAutoLoading = () => !isUndefined(autoLoadHandle);
189
+ const save = async (changes) => {
190
+ /* istanbul ignore else */
191
+ if (loadSave != 1) {
192
+ loadSave = 2;
193
+ saves++;
194
+ await schedule(async () => {
195
+ try {
196
+ await setPersisted(getContent, changes);
197
+ } catch (error) {
198
+ /* istanbul ignore next */
199
+ onIgnoredError?.(error);
200
+ }
201
+ loadSave = 0;
202
+ });
203
+ }
204
+ return persister;
205
+ };
206
+ const startAutoSave = async () => {
207
+ await stopAutoSave().save();
208
+ autoSaveListenerId = store.addDidFinishTransactionListener(() => {
209
+ const changes = getChanges();
210
+ if (hasChanges(changes)) {
211
+ save(changes);
212
+ }
213
+ });
214
+ return persister;
215
+ };
216
+ const stopAutoSave = () => {
217
+ ifNotUndefined(autoSaveListenerId, store.delListener);
218
+ autoSaveListenerId = void 0;
219
+ return persister;
220
+ };
221
+ const isAutoSaving = () => !isUndefined(autoSaveListenerId);
222
+ const schedule = async (...actions) => {
223
+ arrayPush(mapGet(scheduleActions, scheduleId), ...actions);
224
+ await run();
225
+ return persister;
226
+ };
227
+ const getStore = () => store;
228
+ const destroy = () => stopAutoLoad().stopAutoSave();
229
+ const getStats = () => ({loads, saves});
230
+ const persister = {
231
+ load,
232
+ startAutoLoad,
233
+ stopAutoLoad,
234
+ isAutoLoading,
235
+ save,
236
+ startAutoSave,
237
+ stopAutoSave,
238
+ isAutoSaving,
239
+ schedule,
240
+ getStore,
241
+ destroy,
242
+ getStats,
243
+ ...extra,
244
+ };
245
+ return objFreeze(persister);
246
+ };
247
+
248
+ new GLOBAL.TextEncoder();
249
+
250
+ const newStamp = (value, time) => (time ? [value, time] : [value]);
251
+ const getLatestTime = (time1, time2) =>
252
+ ((time1 ?? '') > (time2 ?? '') ? time1 : time2) ?? '';
253
+ const stampNewObj = (time = EMPTY_STRING) => newStamp(objNew(), time);
254
+
255
+ const MASK6 = 63;
256
+ const ENCODE =
257
+ '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'.split(
258
+ EMPTY_STRING,
259
+ );
260
+ mapNew(arrayMap(ENCODE, (char, index) => [char, index]));
261
+ const encode = (num) => ENCODE[num & MASK6];
262
+
263
+ const getRandomValues = GLOBAL.crypto
264
+ ? (array) => GLOBAL.crypto.getRandomValues(array)
265
+ : /* istanbul ignore next */
266
+ (array) => arrayMap(array, () => mathFloor(math.random() * 256));
267
+ const getUniqueId = (length = 16) =>
268
+ arrayReduce(
269
+ getRandomValues(new Uint8Array(length)),
270
+ (uniqueId, number) => uniqueId + encode(number),
271
+ '',
272
+ );
273
+
274
+ const Message = {
275
+ Response: 0,
276
+ GetContentHashes: 1,
277
+ ContentHashes: 2,
278
+ ContentDiff: 3,
279
+ GetTableDiff: 4,
280
+ GetRowDiff: 5,
281
+ GetCellDiff: 6,
282
+ GetValueDiff: 7,
283
+ };
284
+ const createCustomSynchronizer = (
285
+ store,
286
+ send,
287
+ registerReceive,
288
+ destroyImpl,
289
+ requestTimeoutSeconds,
290
+ onIgnoredError,
291
+ extra = {},
292
+ ) => {
293
+ let persisterListener;
294
+ let sends = 0;
295
+ let receives = 0;
296
+ const pendingRequests = mapNew();
297
+ registerReceive((fromClientId, requestId, message, body) => {
298
+ receives++;
299
+ if (message == Message.Response) {
300
+ ifNotUndefined(
301
+ mapGet(pendingRequests, requestId),
302
+ ([toClientId, handleResponse]) =>
303
+ isUndefined(toClientId) || toClientId == fromClientId
304
+ ? handleResponse(body, fromClientId)
305
+ : /* istanbul ignore next */
306
+ 0,
307
+ );
308
+ } else if (message == Message.ContentHashes && persister.isAutoLoading()) {
309
+ getChangesFromOtherStore(fromClientId, body).then((changes) => {
310
+ persisterListener?.(void 0, changes);
311
+ });
312
+ } else if (message == Message.ContentDiff && persister.isAutoLoading()) {
313
+ persisterListener?.(void 0, body);
314
+ } else {
315
+ ifNotUndefined(
316
+ message == Message.GetContentHashes && persister.isAutoSaving()
317
+ ? store.getMergeableContentHashes()
318
+ : message == Message.GetTableDiff
319
+ ? store.getMergeableTableDiff(body)
320
+ : message == Message.GetRowDiff
321
+ ? store.getMergeableRowDiff(body)
322
+ : message == Message.GetCellDiff
323
+ ? store.getMergeableCellDiff(body)
324
+ : message == Message.GetValueDiff
325
+ ? store.getMergeableValueDiff(body)
326
+ : void 0,
327
+ (response) => {
328
+ sends++;
329
+ send(fromClientId, requestId, Message.Response, response);
330
+ },
331
+ );
332
+ }
333
+ });
334
+ const request = async (toClientId, message, body = EMPTY_STRING) =>
335
+ promiseNew((resolve, reject) => {
336
+ const requestId = getUniqueId();
337
+ const timeout = setTimeout(() => {
338
+ collDel(pendingRequests, requestId);
339
+ reject(`No response from ${toClientId ?? 'anyone'} to '${message}'`);
340
+ }, requestTimeoutSeconds * 1e3);
341
+ mapSet(pendingRequests, requestId, [
342
+ toClientId,
343
+ (response, fromClientId) => {
344
+ clearTimeout(timeout);
345
+ collDel(pendingRequests, requestId);
346
+ resolve([response, fromClientId]);
347
+ },
348
+ ]);
349
+ sends++;
350
+ send(toClientId, requestId, message, body);
351
+ });
352
+ const mergeTablesStamps = (tablesStamp, [tableStamps2, tablesTime2]) => {
353
+ objForEach(tableStamps2, ([rowStamps2, tableTime2], tableId) => {
354
+ const tableStamp = objEnsure(tablesStamp[0], tableId, stampNewObj);
355
+ objForEach(rowStamps2, ([cellStamps2, rowTime2], rowId) => {
356
+ const rowStamp = objEnsure(tableStamp[0], rowId, stampNewObj);
357
+ objForEach(
358
+ cellStamps2,
359
+ ([cell2, cellTime2], cellId) =>
360
+ (rowStamp[0][cellId] = newStamp(cell2, cellTime2)),
361
+ );
362
+ rowStamp[1] = getLatestTime(rowStamp[1], rowTime2);
363
+ });
364
+ tableStamp[1] = getLatestTime(tableStamp[1], tableTime2);
365
+ });
366
+ tablesStamp[1] = getLatestTime(tablesStamp[1], tablesTime2);
367
+ };
368
+ const getChangesFromOtherStore = async (
369
+ otherClientId = null,
370
+ otherContentHashes,
371
+ ) => {
372
+ if (isUndefined(otherContentHashes)) {
373
+ [otherContentHashes, otherClientId] = await request(
374
+ otherClientId,
375
+ Message.GetContentHashes,
376
+ );
377
+ }
378
+ const [otherTablesHash, otherValuesHash] = otherContentHashes;
379
+ const [tablesHash, valuesHash] = store.getMergeableContentHashes();
380
+ let tablesChanges = stampNewObj();
381
+ if (tablesHash != otherTablesHash) {
382
+ const [newTables, differentTableHashes] = (
383
+ await request(
384
+ otherClientId,
385
+ Message.GetTableDiff,
386
+ store.getMergeableTableHashes(),
387
+ )
388
+ )[0];
389
+ tablesChanges = newTables;
390
+ if (!objIsEmpty(differentTableHashes)) {
391
+ const [newRows, differentRowHashes] = (
392
+ await request(
393
+ otherClientId,
394
+ Message.GetRowDiff,
395
+ store.getMergeableRowHashes(differentTableHashes),
396
+ )
397
+ )[0];
398
+ mergeTablesStamps(tablesChanges, newRows);
399
+ if (!objIsEmpty(differentRowHashes)) {
400
+ const newCells = (
401
+ await request(
402
+ otherClientId,
403
+ Message.GetCellDiff,
404
+ store.getMergeableCellHashes(differentRowHashes),
405
+ )
406
+ )[0];
407
+ mergeTablesStamps(tablesChanges, newCells);
408
+ }
409
+ }
410
+ }
411
+ return [
412
+ tablesChanges,
413
+ valuesHash == otherValuesHash
414
+ ? stampNewObj()
415
+ : (
416
+ await request(
417
+ otherClientId,
418
+ Message.GetValueDiff,
419
+ store.getMergeableValueHashes(),
420
+ )
421
+ )[0],
422
+ 1,
423
+ ];
424
+ };
425
+ const getPersisted = async () => {
426
+ const changes = await getChangesFromOtherStore();
427
+ return !objIsEmpty(changes[0][0]) || !objIsEmpty(changes[1][0])
428
+ ? changes
429
+ : void 0;
430
+ };
431
+ const setPersisted = async (_getContent, changes) => {
432
+ sends++;
433
+ if (changes) {
434
+ send(null, null, Message.ContentDiff, changes);
435
+ } else {
436
+ send(
437
+ null,
438
+ null,
439
+ Message.ContentHashes,
440
+ store.getMergeableContentHashes(),
441
+ );
442
+ }
443
+ };
444
+ const addPersisterListener = (listener) => (persisterListener = listener);
445
+ const delPersisterListener = () => (persisterListener = void 0);
446
+ const startSync = async (initialContent) =>
447
+ await (await persister.startAutoLoad(initialContent)).startAutoSave();
448
+ const stopSync = () => persister.stopAutoLoad().stopAutoSave();
449
+ const destroy = () => {
450
+ destroyImpl();
451
+ return persister.stopSync();
452
+ };
453
+ const getSynchronizerStats = () => ({sends, receives});
454
+ const persister = createCustomPersister(
455
+ store,
456
+ getPersisted,
457
+ setPersisted,
458
+ addPersisterListener,
459
+ delPersisterListener,
460
+ onIgnoredError,
461
+ Persists.MergeableStoreOnly,
462
+ {startSync, stopSync, destroy, getSynchronizerStats, ...extra},
463
+ );
464
+ return persister;
465
+ };
466
+
467
+ const createBroadcastChannelSynchronizer = (
468
+ store,
469
+ channelName,
470
+ onIgnoredError,
471
+ ) => {
472
+ const clientId = getUniqueId();
473
+ const channel = new BroadcastChannel(channelName);
474
+ const send = (toClientId, requestId, message, body) =>
475
+ channel.postMessage([clientId, toClientId, requestId, message, body]);
476
+ const registerReceive = (receive) => {
477
+ channel.onmessage = ({
478
+ data: [fromClientId, toClientId, requestId, message, body],
479
+ }) =>
480
+ isUndefined(toClientId) || toClientId == clientId
481
+ ? receive(fromClientId, requestId, message, body)
482
+ : 0;
483
+ };
484
+ const destroy = () => {
485
+ channel.close();
486
+ };
487
+ return createCustomSynchronizer(
488
+ store,
489
+ send,
490
+ registerReceive,
491
+ destroy,
492
+ 0.01,
493
+ onIgnoredError,
494
+ {getChannelName: () => channelName},
495
+ );
496
+ };
497
+
498
+ export {createBroadcastChannelSynchronizer};
@@ -287,7 +287,7 @@ const Message = {
287
287
  const createCustomSynchronizer = (
288
288
  store,
289
289
  send,
290
- onReceive,
290
+ registerReceive,
291
291
  destroyImpl,
292
292
  requestTimeoutSeconds,
293
293
  onIgnoredError,
@@ -297,7 +297,7 @@ const createCustomSynchronizer = (
297
297
  let sends = 0;
298
298
  let receives = 0;
299
299
  const pendingRequests = mapNew();
300
- onReceive((fromClientId, requestId, message, body) => {
300
+ registerReceive((fromClientId, requestId, message, body) => {
301
301
  receives++;
302
302
  if (message == Message.Response) {
303
303
  ifNotUndefined(
@@ -483,7 +483,7 @@ const createLocalSynchronizer = (store, onIgnoredError) => {
483
483
  0,
484
484
  );
485
485
  };
486
- const onReceive = (receive) => {
486
+ const registerReceive = (receive) => {
487
487
  mapSet(clients, clientId, receive);
488
488
  };
489
489
  const destroy = () => {
@@ -492,7 +492,7 @@ const createLocalSynchronizer = (store, onIgnoredError) => {
492
492
  return createCustomSynchronizer(
493
493
  store,
494
494
  send,
495
- onReceive,
495
+ registerReceive,
496
496
  destroy,
497
497
  0.01,
498
498
  onIgnoredError,
@@ -287,7 +287,7 @@ const Message = {
287
287
  const createCustomSynchronizer = (
288
288
  store,
289
289
  send,
290
- onReceive,
290
+ registerReceive,
291
291
  destroyImpl,
292
292
  requestTimeoutSeconds,
293
293
  onIgnoredError,
@@ -297,7 +297,7 @@ const createCustomSynchronizer = (
297
297
  let sends = 0;
298
298
  let receives = 0;
299
299
  const pendingRequests = mapNew();
300
- onReceive((fromClientId, requestId, message, body) => {
300
+ registerReceive((fromClientId, requestId, message, body) => {
301
301
  receives++;
302
302
  if (message == Message.Response) {
303
303
  ifNotUndefined(
@@ -483,7 +483,7 @@ const createLocalSynchronizer = (store, onIgnoredError) => {
483
483
  0,
484
484
  );
485
485
  };
486
- const onReceive = (receive) => {
486
+ const registerReceive = (receive) => {
487
487
  mapSet(clients, clientId, receive);
488
488
  };
489
489
  const destroy = () => {
@@ -492,7 +492,7 @@ const createLocalSynchronizer = (store, onIgnoredError) => {
492
492
  return createCustomSynchronizer(
493
493
  store,
494
494
  send,
495
- onReceive,
495
+ registerReceive,
496
496
  destroy,
497
497
  0.01,
498
498
  onIgnoredError,
@@ -294,7 +294,7 @@ const Message = {
294
294
  const createCustomSynchronizer = (
295
295
  store,
296
296
  send,
297
- onReceive,
297
+ registerReceive,
298
298
  destroyImpl,
299
299
  requestTimeoutSeconds,
300
300
  onIgnoredError,
@@ -304,7 +304,7 @@ const createCustomSynchronizer = (
304
304
  let sends = 0;
305
305
  let receives = 0;
306
306
  const pendingRequests = mapNew();
307
- onReceive((fromClientId, requestId, message, body) => {
307
+ registerReceive((fromClientId, requestId, message, body) => {
308
308
  receives++;
309
309
  if (message == Message.Response) {
310
310
  ifNotUndefined(
@@ -480,43 +480,37 @@ const createWsSynchronizer = async (
480
480
  requestTimeoutSeconds = 1,
481
481
  onIgnoredError,
482
482
  ) => {
483
- let currentReceive;
484
483
  const addEventListener = (event, handler) =>
485
484
  webSocket.addEventListener(event, handler);
486
- const onReceive = (receive) => {
487
- currentReceive = receive;
488
- };
489
- const send = (toClientId, ...args) => {
485
+ const registerReceive = (receive) =>
486
+ addEventListener('message', ({data}) => {
487
+ const payload = data.toString(UTF8);
488
+ const splitAt = payload.indexOf(MESSAGE_SEPARATOR);
489
+ if (splitAt !== -1) {
490
+ receive(
491
+ slice(data, 0, splitAt),
492
+ ...jsonParseWithUndefined(slice(data, splitAt + 1)),
493
+ );
494
+ }
495
+ });
496
+ const send = (toClientId, ...args) =>
490
497
  webSocket.send(
491
498
  (toClientId ?? EMPTY_STRING) +
492
499
  MESSAGE_SEPARATOR +
493
500
  jsonStringWithUndefined(args),
494
501
  );
495
- };
496
502
  const destroy = () => {
497
503
  webSocket.close();
498
504
  };
499
505
  const synchronizer = createCustomSynchronizer(
500
506
  store,
501
507
  send,
502
- onReceive,
508
+ registerReceive,
503
509
  destroy,
504
510
  requestTimeoutSeconds,
505
511
  onIgnoredError,
506
512
  {getWebSocket: () => webSocket},
507
513
  );
508
- addEventListener('message', ({data}) => {
509
- if (!isUndefined(currentReceive)) {
510
- const payload = data.toString(UTF8);
511
- const splitAt = payload.indexOf(MESSAGE_SEPARATOR);
512
- if (splitAt !== -1) {
513
- currentReceive(
514
- slice(data, 0, splitAt),
515
- ...jsonParseWithUndefined(slice(data, splitAt + 1)),
516
- );
517
- }
518
- }
519
- });
520
514
  return promiseNew((resolve, reject) => {
521
515
  if (webSocket.readyState != webSocket.OPEN) {
522
516
  addEventListener('error', reject);