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