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
@@ -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
  };