tinybase 6.2.0-beta.1 → 6.2.0-beta.2

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 (55) hide show
  1. package/@types/common/index.d.ts +124 -0
  2. package/@types/common/with-schemas/index.d.ts +124 -0
  3. package/@types/index.d.ts +1 -1
  4. package/@types/mergeable-store/index.d.ts +2 -14
  5. package/@types/mergeable-store/with-schemas/index.d.ts +2 -14
  6. package/@types/omni/index.d.ts +54 -0
  7. package/@types/omni/with-schemas/index.d.ts +54 -0
  8. package/@types/persisters/index.d.ts +2 -2
  9. package/@types/persisters/with-schemas/index.d.ts +2 -2
  10. package/@types/relationships/index.d.ts +2 -2
  11. package/@types/relationships/with-schemas/index.d.ts +2 -2
  12. package/@types/store/index.d.ts +1 -1
  13. package/@types/store/with-schemas/index.d.ts +1 -1
  14. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.ts +1 -1
  15. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.ts +1 -1
  16. package/@types/with-schemas/index.d.ts +1 -1
  17. package/common/index.js +108 -4
  18. package/common/with-schemas/index.js +108 -4
  19. package/index.js +107 -86
  20. package/mergeable-store/index.js +73 -54
  21. package/mergeable-store/with-schemas/index.js +73 -54
  22. package/min/common/index.js +1 -1
  23. package/min/common/index.js.gz +0 -0
  24. package/min/common/with-schemas/index.js +1 -1
  25. package/min/common/with-schemas/index.js.gz +0 -0
  26. package/min/index.js +1 -1
  27. package/min/index.js.gz +0 -0
  28. package/min/mergeable-store/index.js +1 -1
  29. package/min/mergeable-store/index.js.gz +0 -0
  30. package/min/mergeable-store/with-schemas/index.js +1 -1
  31. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  32. package/min/omni/index.js +1 -0
  33. package/min/omni/index.js.gz +0 -0
  34. package/min/omni/with-schemas/index.js +1 -0
  35. package/min/omni/with-schemas/index.js.gz +0 -0
  36. package/min/with-schemas/index.js +1 -1
  37. package/min/with-schemas/index.js.gz +0 -0
  38. package/omni/index.js +8413 -0
  39. package/omni/with-schemas/index.js +8413 -0
  40. package/package.json +432 -396
  41. package/readme.md +2 -2
  42. package/releases.md +2 -2
  43. package/synchronizers/index.js +0 -1
  44. package/synchronizers/synchronizer-broadcast-channel/index.js +0 -1
  45. package/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +0 -1
  46. package/synchronizers/synchronizer-local/index.js +0 -1
  47. package/synchronizers/synchronizer-local/with-schemas/index.js +0 -1
  48. package/synchronizers/synchronizer-ws-client/index.js +0 -1
  49. package/synchronizers/synchronizer-ws-client/with-schemas/index.js +0 -1
  50. package/synchronizers/synchronizer-ws-server/index.js +0 -1
  51. package/synchronizers/synchronizer-ws-server/with-schemas/index.js +0 -1
  52. package/synchronizers/synchronizer-ws-server-durable-object/index.js +0 -1
  53. package/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +0 -1
  54. package/synchronizers/with-schemas/index.js +0 -1
  55. package/with-schemas/index.js +107 -86
package/common/index.js CHANGED
@@ -4,23 +4,32 @@ const strSplit = (str, separator = EMPTY_STRING, limit) =>
4
4
 
5
5
  const GLOBAL = globalThis;
6
6
  const math = Math;
7
+ const mathMax = math.max;
7
8
  const mathFloor = math.floor;
9
+ const isUndefined = (thing) => thing == void 0;
10
+ const ifNotUndefined = (value, then, otherwise) =>
11
+ isUndefined(value) ? otherwise?.() : then(value);
8
12
 
13
+ const arrayForEach = (array, cb) => array.forEach(cb);
9
14
  const arrayMap = (array, cb) => array.map(cb);
10
15
  const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
11
16
 
17
+ const mapNew = (entries) => new Map(entries);
18
+ const mapGet = (map, key) => map?.get(key);
19
+
12
20
  const MASK6 = 63;
13
21
  const ENCODE = /* @__PURE__ */ strSplit(
14
22
  '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
15
23
  );
24
+ const DECODE = /* @__PURE__ */ mapNew(
25
+ /* @__PURE__ */ arrayMap(ENCODE, (char, index) => [char, index]),
26
+ );
16
27
  const encode = (num) => ENCODE[num & MASK6];
17
-
28
+ const decode = (str, pos) => mapGet(DECODE, str[pos]) ?? 0;
18
29
  const getRandomValues = GLOBAL.crypto
19
30
  ? (array) => GLOBAL.crypto.getRandomValues(array)
20
31
  : /* istanbul ignore next */
21
32
  (array) => arrayMap(array, () => mathFloor(math.random() * 256));
22
- const defaultSorter = (sortKey1, sortKey2) =>
23
- (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
24
33
  const getUniqueId = (length = 16) =>
25
34
  arrayReduce(
26
35
  getRandomValues(new Uint8Array(length)),
@@ -28,4 +37,99 @@ const getUniqueId = (length = 16) =>
28
37
  '',
29
38
  );
30
39
 
31
- export {defaultSorter, getUniqueId};
40
+ const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
41
+ const getHash = (value) => {
42
+ let hash = 2166136261;
43
+ arrayForEach(textEncoder.encode(value), (char) => {
44
+ hash ^= char;
45
+ hash +=
46
+ (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
47
+ });
48
+ return hash >>> 0;
49
+ };
50
+
51
+ const SHIFT36 = 2 ** 36;
52
+ const SHIFT30 = 2 ** 30;
53
+ const SHIFT24 = 2 ** 24;
54
+ const SHIFT18 = 2 ** 18;
55
+ const SHIFT12 = 2 ** 12;
56
+ const SHIFT6 = 2 ** 6;
57
+ const getClientIdFromUniqueId = (uniqueId) => {
58
+ const clientHash30 = getHash(uniqueId);
59
+ return (
60
+ encode(clientHash30 / SHIFT24) +
61
+ encode(clientHash30 / SHIFT18) +
62
+ encode(clientHash30 / SHIFT12) +
63
+ encode(clientHash30 / SHIFT6) +
64
+ encode(clientHash30)
65
+ );
66
+ };
67
+ const getHlcFunctions = (uniqueId, getNow = Date.now) => {
68
+ let lastLogicalTime = 0;
69
+ let lastCounter = -1;
70
+ const thisClientId = ifNotUndefined(uniqueId, getClientIdFromUniqueId, () =>
71
+ getUniqueId(5),
72
+ );
73
+ const getNextHlc = () => {
74
+ seenHlc();
75
+ return encodeHlc(lastLogicalTime, ++lastCounter);
76
+ };
77
+ const seenHlc = (hlc) => {
78
+ const previousLogicalTime = lastLogicalTime;
79
+ const [remoteLogicalTime, remoteCounter] =
80
+ isUndefined(hlc) || hlc == '' ? [0, 0] : decodeHlc(hlc);
81
+ lastLogicalTime = mathMax(previousLogicalTime, remoteLogicalTime, getNow());
82
+ lastCounter =
83
+ lastLogicalTime == previousLogicalTime
84
+ ? lastLogicalTime == remoteLogicalTime
85
+ ? mathMax(lastCounter, remoteCounter)
86
+ : lastCounter
87
+ : lastLogicalTime == remoteLogicalTime
88
+ ? remoteCounter
89
+ : -1;
90
+ };
91
+ const encodeHlc = (logicalTime42, counter24, clientId) =>
92
+ encode(logicalTime42 / SHIFT36) +
93
+ encode(logicalTime42 / SHIFT30) +
94
+ encode(logicalTime42 / SHIFT24) +
95
+ encode(logicalTime42 / SHIFT18) +
96
+ encode(logicalTime42 / SHIFT12) +
97
+ encode(logicalTime42 / SHIFT6) +
98
+ encode(logicalTime42) +
99
+ encode(counter24 / SHIFT18) +
100
+ encode(counter24 / SHIFT12) +
101
+ encode(counter24 / SHIFT6) +
102
+ encode(counter24) +
103
+ (isUndefined(clientId) ? thisClientId : getClientIdFromUniqueId(clientId));
104
+ const decodeHlc = (hlc16) => [
105
+ decode(hlc16, 0) * SHIFT36 +
106
+ decode(hlc16, 1) * SHIFT30 +
107
+ decode(hlc16, 2) * SHIFT24 +
108
+ decode(hlc16, 3) * SHIFT18 +
109
+ decode(hlc16, 4) * SHIFT12 +
110
+ decode(hlc16, 5) * SHIFT6 +
111
+ decode(hlc16, 6),
112
+ decode(hlc16, 7) * SHIFT18 +
113
+ decode(hlc16, 8) * SHIFT12 +
114
+ decode(hlc16, 9) * SHIFT6 +
115
+ decode(hlc16, 10),
116
+ hlc16.slice(11),
117
+ ];
118
+ const getLastLogicalTime = () => lastLogicalTime;
119
+ const getLastCounter = () => lastCounter;
120
+ const getClientId = () => thisClientId;
121
+ return [
122
+ getNextHlc,
123
+ seenHlc,
124
+ encodeHlc,
125
+ decodeHlc,
126
+ getLastLogicalTime,
127
+ getLastCounter,
128
+ getClientId,
129
+ ];
130
+ };
131
+
132
+ const defaultSorter = (sortKey1, sortKey2) =>
133
+ (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
134
+
135
+ export {defaultSorter, getHlcFunctions, getRandomValues, getUniqueId};
@@ -4,23 +4,32 @@ const strSplit = (str, separator = EMPTY_STRING, limit) =>
4
4
 
5
5
  const GLOBAL = globalThis;
6
6
  const math = Math;
7
+ const mathMax = math.max;
7
8
  const mathFloor = math.floor;
9
+ const isUndefined = (thing) => thing == void 0;
10
+ const ifNotUndefined = (value, then, otherwise) =>
11
+ isUndefined(value) ? otherwise?.() : then(value);
8
12
 
13
+ const arrayForEach = (array, cb) => array.forEach(cb);
9
14
  const arrayMap = (array, cb) => array.map(cb);
10
15
  const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
11
16
 
17
+ const mapNew = (entries) => new Map(entries);
18
+ const mapGet = (map, key) => map?.get(key);
19
+
12
20
  const MASK6 = 63;
13
21
  const ENCODE = /* @__PURE__ */ strSplit(
14
22
  '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz',
15
23
  );
24
+ const DECODE = /* @__PURE__ */ mapNew(
25
+ /* @__PURE__ */ arrayMap(ENCODE, (char, index) => [char, index]),
26
+ );
16
27
  const encode = (num) => ENCODE[num & MASK6];
17
-
28
+ const decode = (str, pos) => mapGet(DECODE, str[pos]) ?? 0;
18
29
  const getRandomValues = GLOBAL.crypto
19
30
  ? (array) => GLOBAL.crypto.getRandomValues(array)
20
31
  : /* istanbul ignore next */
21
32
  (array) => arrayMap(array, () => mathFloor(math.random() * 256));
22
- const defaultSorter = (sortKey1, sortKey2) =>
23
- (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
24
33
  const getUniqueId = (length = 16) =>
25
34
  arrayReduce(
26
35
  getRandomValues(new Uint8Array(length)),
@@ -28,4 +37,99 @@ const getUniqueId = (length = 16) =>
28
37
  '',
29
38
  );
30
39
 
31
- export {defaultSorter, getUniqueId};
40
+ const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
41
+ const getHash = (value) => {
42
+ let hash = 2166136261;
43
+ arrayForEach(textEncoder.encode(value), (char) => {
44
+ hash ^= char;
45
+ hash +=
46
+ (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
47
+ });
48
+ return hash >>> 0;
49
+ };
50
+
51
+ const SHIFT36 = 2 ** 36;
52
+ const SHIFT30 = 2 ** 30;
53
+ const SHIFT24 = 2 ** 24;
54
+ const SHIFT18 = 2 ** 18;
55
+ const SHIFT12 = 2 ** 12;
56
+ const SHIFT6 = 2 ** 6;
57
+ const getClientIdFromUniqueId = (uniqueId) => {
58
+ const clientHash30 = getHash(uniqueId);
59
+ return (
60
+ encode(clientHash30 / SHIFT24) +
61
+ encode(clientHash30 / SHIFT18) +
62
+ encode(clientHash30 / SHIFT12) +
63
+ encode(clientHash30 / SHIFT6) +
64
+ encode(clientHash30)
65
+ );
66
+ };
67
+ const getHlcFunctions = (uniqueId, getNow = Date.now) => {
68
+ let lastLogicalTime = 0;
69
+ let lastCounter = -1;
70
+ const thisClientId = ifNotUndefined(uniqueId, getClientIdFromUniqueId, () =>
71
+ getUniqueId(5),
72
+ );
73
+ const getNextHlc = () => {
74
+ seenHlc();
75
+ return encodeHlc(lastLogicalTime, ++lastCounter);
76
+ };
77
+ const seenHlc = (hlc) => {
78
+ const previousLogicalTime = lastLogicalTime;
79
+ const [remoteLogicalTime, remoteCounter] =
80
+ isUndefined(hlc) || hlc == '' ? [0, 0] : decodeHlc(hlc);
81
+ lastLogicalTime = mathMax(previousLogicalTime, remoteLogicalTime, getNow());
82
+ lastCounter =
83
+ lastLogicalTime == previousLogicalTime
84
+ ? lastLogicalTime == remoteLogicalTime
85
+ ? mathMax(lastCounter, remoteCounter)
86
+ : lastCounter
87
+ : lastLogicalTime == remoteLogicalTime
88
+ ? remoteCounter
89
+ : -1;
90
+ };
91
+ const encodeHlc = (logicalTime42, counter24, clientId) =>
92
+ encode(logicalTime42 / SHIFT36) +
93
+ encode(logicalTime42 / SHIFT30) +
94
+ encode(logicalTime42 / SHIFT24) +
95
+ encode(logicalTime42 / SHIFT18) +
96
+ encode(logicalTime42 / SHIFT12) +
97
+ encode(logicalTime42 / SHIFT6) +
98
+ encode(logicalTime42) +
99
+ encode(counter24 / SHIFT18) +
100
+ encode(counter24 / SHIFT12) +
101
+ encode(counter24 / SHIFT6) +
102
+ encode(counter24) +
103
+ (isUndefined(clientId) ? thisClientId : getClientIdFromUniqueId(clientId));
104
+ const decodeHlc = (hlc16) => [
105
+ decode(hlc16, 0) * SHIFT36 +
106
+ decode(hlc16, 1) * SHIFT30 +
107
+ decode(hlc16, 2) * SHIFT24 +
108
+ decode(hlc16, 3) * SHIFT18 +
109
+ decode(hlc16, 4) * SHIFT12 +
110
+ decode(hlc16, 5) * SHIFT6 +
111
+ decode(hlc16, 6),
112
+ decode(hlc16, 7) * SHIFT18 +
113
+ decode(hlc16, 8) * SHIFT12 +
114
+ decode(hlc16, 9) * SHIFT6 +
115
+ decode(hlc16, 10),
116
+ hlc16.slice(11),
117
+ ];
118
+ const getLastLogicalTime = () => lastLogicalTime;
119
+ const getLastCounter = () => lastCounter;
120
+ const getClientId = () => thisClientId;
121
+ return [
122
+ getNextHlc,
123
+ seenHlc,
124
+ encodeHlc,
125
+ decodeHlc,
126
+ getLastLogicalTime,
127
+ getLastCounter,
128
+ getClientId,
129
+ ];
130
+ };
131
+
132
+ const defaultSorter = (sortKey1, sortKey2) =>
133
+ (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
134
+
135
+ export {defaultSorter, getHlcFunctions, getRandomValues, getUniqueId};
package/index.js CHANGED
@@ -774,13 +774,10 @@ const DECODE = /* @__PURE__ */ mapNew(
774
774
  );
775
775
  const encode = (num) => ENCODE[num & MASK6];
776
776
  const decode = (str, pos) => mapGet(DECODE, str[pos]) ?? 0;
777
-
778
777
  const getRandomValues = GLOBAL.crypto
779
778
  ? (array) => GLOBAL.crypto.getRandomValues(array)
780
779
  : /* istanbul ignore next */
781
780
  (array) => arrayMap(array, () => mathFloor(math.random() * 256));
782
- const defaultSorter = (sortKey1, sortKey2) =>
783
- (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
784
781
  const getUniqueId = (length = 16) =>
785
782
  arrayReduce(
786
783
  getRandomValues(new Uint8Array(length)),
@@ -788,6 +785,101 @@ const getUniqueId = (length = 16) =>
788
785
  '',
789
786
  );
790
787
 
788
+ const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
789
+ const getHash = (value) => {
790
+ let hash = 2166136261;
791
+ arrayForEach(textEncoder.encode(value), (char) => {
792
+ hash ^= char;
793
+ hash +=
794
+ (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
795
+ });
796
+ return hash >>> 0;
797
+ };
798
+
799
+ const SHIFT36 = 2 ** 36;
800
+ const SHIFT30 = 2 ** 30;
801
+ const SHIFT24 = 2 ** 24;
802
+ const SHIFT18 = 2 ** 18;
803
+ const SHIFT12 = 2 ** 12;
804
+ const SHIFT6 = 2 ** 6;
805
+ const getClientIdFromUniqueId = (uniqueId) => {
806
+ const clientHash30 = getHash(uniqueId);
807
+ return (
808
+ encode(clientHash30 / SHIFT24) +
809
+ encode(clientHash30 / SHIFT18) +
810
+ encode(clientHash30 / SHIFT12) +
811
+ encode(clientHash30 / SHIFT6) +
812
+ encode(clientHash30)
813
+ );
814
+ };
815
+ const getHlcFunctions = (uniqueId, getNow = Date.now) => {
816
+ let lastLogicalTime = 0;
817
+ let lastCounter = -1;
818
+ const thisClientId = ifNotUndefined(uniqueId, getClientIdFromUniqueId, () =>
819
+ getUniqueId(5),
820
+ );
821
+ const getNextHlc = () => {
822
+ seenHlc();
823
+ return encodeHlc(lastLogicalTime, ++lastCounter);
824
+ };
825
+ const seenHlc = (hlc) => {
826
+ const previousLogicalTime = lastLogicalTime;
827
+ const [remoteLogicalTime, remoteCounter] =
828
+ isUndefined(hlc) || hlc == '' ? [0, 0] : decodeHlc(hlc);
829
+ lastLogicalTime = mathMax(previousLogicalTime, remoteLogicalTime, getNow());
830
+ lastCounter =
831
+ lastLogicalTime == previousLogicalTime
832
+ ? lastLogicalTime == remoteLogicalTime
833
+ ? mathMax(lastCounter, remoteCounter)
834
+ : lastCounter
835
+ : lastLogicalTime == remoteLogicalTime
836
+ ? remoteCounter
837
+ : -1;
838
+ };
839
+ const encodeHlc = (logicalTime42, counter24, clientId) =>
840
+ encode(logicalTime42 / SHIFT36) +
841
+ encode(logicalTime42 / SHIFT30) +
842
+ encode(logicalTime42 / SHIFT24) +
843
+ encode(logicalTime42 / SHIFT18) +
844
+ encode(logicalTime42 / SHIFT12) +
845
+ encode(logicalTime42 / SHIFT6) +
846
+ encode(logicalTime42) +
847
+ encode(counter24 / SHIFT18) +
848
+ encode(counter24 / SHIFT12) +
849
+ encode(counter24 / SHIFT6) +
850
+ encode(counter24) +
851
+ (isUndefined(clientId) ? thisClientId : getClientIdFromUniqueId(clientId));
852
+ const decodeHlc = (hlc16) => [
853
+ decode(hlc16, 0) * SHIFT36 +
854
+ decode(hlc16, 1) * SHIFT30 +
855
+ decode(hlc16, 2) * SHIFT24 +
856
+ decode(hlc16, 3) * SHIFT18 +
857
+ decode(hlc16, 4) * SHIFT12 +
858
+ decode(hlc16, 5) * SHIFT6 +
859
+ decode(hlc16, 6),
860
+ decode(hlc16, 7) * SHIFT18 +
861
+ decode(hlc16, 8) * SHIFT12 +
862
+ decode(hlc16, 9) * SHIFT6 +
863
+ decode(hlc16, 10),
864
+ hlc16.slice(11),
865
+ ];
866
+ const getLastLogicalTime = () => lastLogicalTime;
867
+ const getLastCounter = () => lastCounter;
868
+ const getClientId = () => thisClientId;
869
+ return [
870
+ getNextHlc,
871
+ seenHlc,
872
+ encodeHlc,
873
+ decodeHlc,
874
+ getLastLogicalTime,
875
+ getLastCounter,
876
+ getClientId,
877
+ ];
878
+ };
879
+
880
+ const defaultSorter = (sortKey1, sortKey2) =>
881
+ (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
882
+
791
883
  const createIndexes = getCreateFunction((store) => {
792
884
  const sliceIdsListeners = mapNew();
793
885
  const sliceRowIdsListeners = mapNew();
@@ -998,86 +1090,6 @@ const createIndexes = getCreateFunction((store) => {
998
1090
  return objFreeze(indexes);
999
1091
  });
1000
1092
 
1001
- const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
1002
- const getHash = (value) => {
1003
- let hash = 2166136261;
1004
- arrayForEach(textEncoder.encode(value), (char) => {
1005
- hash ^= char;
1006
- hash +=
1007
- (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
1008
- });
1009
- return hash >>> 0;
1010
- };
1011
-
1012
- const SHIFT36 = 2 ** 36;
1013
- const SHIFT30 = 2 ** 30;
1014
- const SHIFT24 = 2 ** 24;
1015
- const SHIFT18 = 2 ** 18;
1016
- const SHIFT12 = 2 ** 12;
1017
- const SHIFT6 = 2 ** 6;
1018
- const encodeTimeAndCounter = (logicalTime42, counter24) =>
1019
- encode(logicalTime42 / SHIFT36) +
1020
- encode(logicalTime42 / SHIFT30) +
1021
- encode(logicalTime42 / SHIFT24) +
1022
- encode(logicalTime42 / SHIFT18) +
1023
- encode(logicalTime42 / SHIFT12) +
1024
- encode(logicalTime42 / SHIFT6) +
1025
- encode(logicalTime42) +
1026
- encode(counter24 / SHIFT18) +
1027
- encode(counter24 / SHIFT12) +
1028
- encode(counter24 / SHIFT6) +
1029
- encode(counter24);
1030
- const decodeTimeAndCounter = (hlc16) => [
1031
- decode(hlc16, 0) * SHIFT36 +
1032
- decode(hlc16, 1) * SHIFT30 +
1033
- decode(hlc16, 2) * SHIFT24 +
1034
- decode(hlc16, 3) * SHIFT18 +
1035
- decode(hlc16, 4) * SHIFT12 +
1036
- decode(hlc16, 5) * SHIFT6 +
1037
- decode(hlc16, 6),
1038
- decode(hlc16, 7) * SHIFT18 +
1039
- decode(hlc16, 8) * SHIFT12 +
1040
- decode(hlc16, 9) * SHIFT6 +
1041
- decode(hlc16, 10),
1042
- ];
1043
- const getHlcFunctions = (uniqueId, getNow = Date.now) => {
1044
- let logicalTime = 0;
1045
- let lastCounter = -1;
1046
- const clientPart = ifNotUndefined(
1047
- uniqueId,
1048
- (uniqueId2) => {
1049
- const clientHash30 = getHash(uniqueId2);
1050
- return (
1051
- encode(clientHash30 / SHIFT24) +
1052
- encode(clientHash30 / SHIFT18) +
1053
- encode(clientHash30 / SHIFT12) +
1054
- encode(clientHash30 / SHIFT6) +
1055
- encode(clientHash30)
1056
- );
1057
- },
1058
- () => getUniqueId(5),
1059
- );
1060
- const getHlc = () => {
1061
- seenHlc();
1062
- return encodeTimeAndCounter(logicalTime, ++lastCounter) + clientPart;
1063
- };
1064
- const seenHlc = (hlc) => {
1065
- const previousLogicalTime = logicalTime;
1066
- const [remoteLogicalTime, remoteCounter] =
1067
- isUndefined(hlc) || hlc == '' ? [0, 0] : decodeTimeAndCounter(hlc);
1068
- logicalTime = mathMax(previousLogicalTime, remoteLogicalTime, getNow());
1069
- lastCounter =
1070
- logicalTime == previousLogicalTime
1071
- ? logicalTime == remoteLogicalTime
1072
- ? mathMax(lastCounter, remoteCounter)
1073
- : lastCounter
1074
- : logicalTime == remoteLogicalTime
1075
- ? remoteCounter
1076
- : -1;
1077
- };
1078
- return [getHlc, seenHlc];
1079
- };
1080
-
1081
1093
  const jsonString = JSON.stringify;
1082
1094
  const jsonParse = JSON.parse;
1083
1095
  const jsonStringWithMap = (obj) =>
@@ -2541,7 +2553,7 @@ const createMergeableStore = (uniqueId, getNow) => {
2541
2553
  let defaultingContent = 0;
2542
2554
  const touchedCells = mapNew();
2543
2555
  const touchedValues = setNew();
2544
- const [getHlc, seenHlc] = getHlcFunctions(uniqueId, getNow);
2556
+ const [getNextHlc, seenHlc] = getHlcFunctions(uniqueId, getNow);
2545
2557
  const store = createStore();
2546
2558
  const disableListeningToRawStoreChanges = (actions) => {
2547
2559
  const wasListening = listeningToRawStoreChanges;
@@ -2676,7 +2688,7 @@ const createMergeableStore = (uniqueId, getNow) => {
2676
2688
  {
2677
2689
  [cellId]: [
2678
2690
  newCell,
2679
- defaultingContent ? EMPTY_STRING : getHlc(),
2691
+ defaultingContent ? EMPTY_STRING : getNextHlc(),
2680
2692
  ],
2681
2693
  },
2682
2694
  ],
@@ -2694,7 +2706,14 @@ const createMergeableStore = (uniqueId, getNow) => {
2694
2706
  if (listeningToRawStoreChanges) {
2695
2707
  mergeContentOrChanges([
2696
2708
  [{}],
2697
- [{[valueId]: [newValue, defaultingContent ? EMPTY_STRING : getHlc()]}],
2709
+ [
2710
+ {
2711
+ [valueId]: [
2712
+ newValue,
2713
+ defaultingContent ? EMPTY_STRING : getNextHlc(),
2714
+ ],
2715
+ },
2716
+ ],
2698
2717
  1,
2699
2718
  ]);
2700
2719
  }
@@ -3784,5 +3803,7 @@ export {
3784
3803
  createRelationships,
3785
3804
  createStore,
3786
3805
  defaultSorter,
3806
+ getHlcFunctions,
3807
+ getRandomValues,
3787
3808
  getUniqueId,
3788
3809
  };
@@ -249,13 +249,10 @@ const DECODE = /* @__PURE__ */ mapNew(
249
249
  );
250
250
  const encode = (num) => ENCODE[num & MASK6];
251
251
  const decode = (str, pos) => mapGet(DECODE, str[pos]) ?? 0;
252
-
253
252
  const getRandomValues = GLOBAL.crypto
254
253
  ? (array) => GLOBAL.crypto.getRandomValues(array)
255
254
  : /* istanbul ignore next */
256
255
  (array) => arrayMap(array, () => mathFloor(math.random() * 256));
257
- const defaultSorter = (sortKey1, sortKey2) =>
258
- (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
259
256
  const getUniqueId = (length = 16) =>
260
257
  arrayReduce(
261
258
  getRandomValues(new Uint8Array(length)),
@@ -269,67 +266,79 @@ const SHIFT24 = 2 ** 24;
269
266
  const SHIFT18 = 2 ** 18;
270
267
  const SHIFT12 = 2 ** 12;
271
268
  const SHIFT6 = 2 ** 6;
272
- const encodeTimeAndCounter = (logicalTime42, counter24) =>
273
- encode(logicalTime42 / SHIFT36) +
274
- encode(logicalTime42 / SHIFT30) +
275
- encode(logicalTime42 / SHIFT24) +
276
- encode(logicalTime42 / SHIFT18) +
277
- encode(logicalTime42 / SHIFT12) +
278
- encode(logicalTime42 / SHIFT6) +
279
- encode(logicalTime42) +
280
- encode(counter24 / SHIFT18) +
281
- encode(counter24 / SHIFT12) +
282
- encode(counter24 / SHIFT6) +
283
- encode(counter24);
284
- const decodeTimeAndCounter = (hlc16) => [
285
- decode(hlc16, 0) * SHIFT36 +
286
- decode(hlc16, 1) * SHIFT30 +
287
- decode(hlc16, 2) * SHIFT24 +
288
- decode(hlc16, 3) * SHIFT18 +
289
- decode(hlc16, 4) * SHIFT12 +
290
- decode(hlc16, 5) * SHIFT6 +
291
- decode(hlc16, 6),
292
- decode(hlc16, 7) * SHIFT18 +
293
- decode(hlc16, 8) * SHIFT12 +
294
- decode(hlc16, 9) * SHIFT6 +
295
- decode(hlc16, 10),
296
- ];
269
+ const getClientIdFromUniqueId = (uniqueId) => {
270
+ const clientHash30 = getHash(uniqueId);
271
+ return (
272
+ encode(clientHash30 / SHIFT24) +
273
+ encode(clientHash30 / SHIFT18) +
274
+ encode(clientHash30 / SHIFT12) +
275
+ encode(clientHash30 / SHIFT6) +
276
+ encode(clientHash30)
277
+ );
278
+ };
297
279
  const getHlcFunctions = (uniqueId, getNow = Date.now) => {
298
- let logicalTime = 0;
280
+ let lastLogicalTime = 0;
299
281
  let lastCounter = -1;
300
- const clientPart = ifNotUndefined(
301
- uniqueId,
302
- (uniqueId2) => {
303
- const clientHash30 = getHash(uniqueId2);
304
- return (
305
- encode(clientHash30 / SHIFT24) +
306
- encode(clientHash30 / SHIFT18) +
307
- encode(clientHash30 / SHIFT12) +
308
- encode(clientHash30 / SHIFT6) +
309
- encode(clientHash30)
310
- );
311
- },
312
- () => getUniqueId(5),
282
+ const thisClientId = ifNotUndefined(uniqueId, getClientIdFromUniqueId, () =>
283
+ getUniqueId(5),
313
284
  );
314
- const getHlc = () => {
285
+ const getNextHlc = () => {
315
286
  seenHlc();
316
- return encodeTimeAndCounter(logicalTime, ++lastCounter) + clientPart;
287
+ return encodeHlc(lastLogicalTime, ++lastCounter);
317
288
  };
318
289
  const seenHlc = (hlc) => {
319
- const previousLogicalTime = logicalTime;
290
+ const previousLogicalTime = lastLogicalTime;
320
291
  const [remoteLogicalTime, remoteCounter] =
321
- isUndefined(hlc) || hlc == '' ? [0, 0] : decodeTimeAndCounter(hlc);
322
- logicalTime = mathMax(previousLogicalTime, remoteLogicalTime, getNow());
292
+ isUndefined(hlc) || hlc == '' ? [0, 0] : decodeHlc(hlc);
293
+ lastLogicalTime = mathMax(previousLogicalTime, remoteLogicalTime, getNow());
323
294
  lastCounter =
324
- logicalTime == previousLogicalTime
325
- ? logicalTime == remoteLogicalTime
295
+ lastLogicalTime == previousLogicalTime
296
+ ? lastLogicalTime == remoteLogicalTime
326
297
  ? mathMax(lastCounter, remoteCounter)
327
298
  : lastCounter
328
- : logicalTime == remoteLogicalTime
299
+ : lastLogicalTime == remoteLogicalTime
329
300
  ? remoteCounter
330
301
  : -1;
331
302
  };
332
- return [getHlc, seenHlc];
303
+ const encodeHlc = (logicalTime42, counter24, clientId) =>
304
+ encode(logicalTime42 / SHIFT36) +
305
+ encode(logicalTime42 / SHIFT30) +
306
+ encode(logicalTime42 / SHIFT24) +
307
+ encode(logicalTime42 / SHIFT18) +
308
+ encode(logicalTime42 / SHIFT12) +
309
+ encode(logicalTime42 / SHIFT6) +
310
+ encode(logicalTime42) +
311
+ encode(counter24 / SHIFT18) +
312
+ encode(counter24 / SHIFT12) +
313
+ encode(counter24 / SHIFT6) +
314
+ encode(counter24) +
315
+ (isUndefined(clientId) ? thisClientId : getClientIdFromUniqueId(clientId));
316
+ const decodeHlc = (hlc16) => [
317
+ decode(hlc16, 0) * SHIFT36 +
318
+ decode(hlc16, 1) * SHIFT30 +
319
+ decode(hlc16, 2) * SHIFT24 +
320
+ decode(hlc16, 3) * SHIFT18 +
321
+ decode(hlc16, 4) * SHIFT12 +
322
+ decode(hlc16, 5) * SHIFT6 +
323
+ decode(hlc16, 6),
324
+ decode(hlc16, 7) * SHIFT18 +
325
+ decode(hlc16, 8) * SHIFT12 +
326
+ decode(hlc16, 9) * SHIFT6 +
327
+ decode(hlc16, 10),
328
+ hlc16.slice(11),
329
+ ];
330
+ const getLastLogicalTime = () => lastLogicalTime;
331
+ const getLastCounter = () => lastCounter;
332
+ const getClientId = () => thisClientId;
333
+ return [
334
+ getNextHlc,
335
+ seenHlc,
336
+ encodeHlc,
337
+ decodeHlc,
338
+ getLastLogicalTime,
339
+ getLastCounter,
340
+ getClientId,
341
+ ];
333
342
  };
334
343
 
335
344
  const jsonString = JSON.stringify;
@@ -380,6 +389,9 @@ const stampValidate = (stamp, validateThing) =>
380
389
  isFiniteNumber(stamp[2]) &&
381
390
  validateThing(stamp[0]);
382
391
 
392
+ const defaultSorter = (sortKey1, sortKey2) =>
393
+ (sortKey1 ?? 0) < (sortKey2 ?? 0) ? -1 : 1;
394
+
383
395
  const INTEGER = /^\d+$/;
384
396
  const getPoolFunctions = () => {
385
397
  const pool = [];
@@ -1889,7 +1901,7 @@ const createMergeableStore = (uniqueId, getNow) => {
1889
1901
  let defaultingContent = 0;
1890
1902
  const touchedCells = mapNew();
1891
1903
  const touchedValues = setNew();
1892
- const [getHlc, seenHlc] = getHlcFunctions(uniqueId, getNow);
1904
+ const [getNextHlc, seenHlc] = getHlcFunctions(uniqueId, getNow);
1893
1905
  const store = createStore();
1894
1906
  const disableListeningToRawStoreChanges = (actions) => {
1895
1907
  const wasListening = listeningToRawStoreChanges;
@@ -2024,7 +2036,7 @@ const createMergeableStore = (uniqueId, getNow) => {
2024
2036
  {
2025
2037
  [cellId]: [
2026
2038
  newCell,
2027
- defaultingContent ? EMPTY_STRING : getHlc(),
2039
+ defaultingContent ? EMPTY_STRING : getNextHlc(),
2028
2040
  ],
2029
2041
  },
2030
2042
  ],
@@ -2042,7 +2054,14 @@ const createMergeableStore = (uniqueId, getNow) => {
2042
2054
  if (listeningToRawStoreChanges) {
2043
2055
  mergeContentOrChanges([
2044
2056
  [{}],
2045
- [{[valueId]: [newValue, defaultingContent ? EMPTY_STRING : getHlc()]}],
2057
+ [
2058
+ {
2059
+ [valueId]: [
2060
+ newValue,
2061
+ defaultingContent ? EMPTY_STRING : getNextHlc(),
2062
+ ],
2063
+ },
2064
+ ],
2046
2065
  1,
2047
2066
  ]);
2048
2067
  }