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