tinybase 6.2.0-beta.3 → 6.2.0

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 (76) hide show
  1. package/@types/common/index.d.ts +295 -0
  2. package/@types/common/with-schemas/index.d.ts +375 -0
  3. package/@types/mergeable-store/index.d.ts +5 -34
  4. package/@types/mergeable-store/with-schemas/index.d.ts +5 -34
  5. package/@types/omni/with-schemas/index.d.ts +3 -3
  6. package/common/index.js +62 -3
  7. package/common/with-schemas/index.js +62 -3
  8. package/index.js +133 -86
  9. package/mergeable-store/index.js +102 -94
  10. package/mergeable-store/with-schemas/index.js +102 -94
  11. package/min/common/index.js +1 -1
  12. package/min/common/index.js.gz +0 -0
  13. package/min/common/with-schemas/index.js +1 -1
  14. package/min/common/with-schemas/index.js.gz +0 -0
  15. package/min/index.js +1 -1
  16. package/min/index.js.gz +0 -0
  17. package/min/mergeable-store/index.js +1 -1
  18. package/min/mergeable-store/index.js.gz +0 -0
  19. package/min/mergeable-store/with-schemas/index.js +1 -1
  20. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  21. package/min/omni/index.js +1 -1
  22. package/min/omni/index.js.gz +0 -0
  23. package/min/omni/with-schemas/index.js +1 -1
  24. package/min/omni/with-schemas/index.js.gz +0 -0
  25. package/min/persisters/index.js +1 -1
  26. package/min/persisters/index.js.gz +0 -0
  27. package/min/persisters/persister-pglite/index.js +1 -1
  28. package/min/persisters/persister-pglite/index.js.gz +0 -0
  29. package/min/persisters/persister-pglite/with-schemas/index.js +1 -1
  30. package/min/persisters/persister-pglite/with-schemas/index.js.gz +0 -0
  31. package/min/persisters/persister-postgres/index.js +1 -1
  32. package/min/persisters/persister-postgres/index.js.gz +0 -0
  33. package/min/persisters/persister-postgres/with-schemas/index.js +1 -1
  34. package/min/persisters/persister-postgres/with-schemas/index.js.gz +0 -0
  35. package/min/persisters/with-schemas/index.js +1 -1
  36. package/min/persisters/with-schemas/index.js.gz +0 -0
  37. package/min/store/index.js +1 -1
  38. package/min/store/index.js.gz +0 -0
  39. package/min/store/with-schemas/index.js +1 -1
  40. package/min/store/with-schemas/index.js.gz +0 -0
  41. package/min/ui-react-inspector/index.js +1 -1
  42. package/min/ui-react-inspector/index.js.gz +0 -0
  43. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  44. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  45. package/min/with-schemas/index.js +1 -1
  46. package/min/with-schemas/index.js.gz +0 -0
  47. package/omni/index.js +148 -101
  48. package/omni/with-schemas/index.js +148 -101
  49. package/package.json +3 -3
  50. package/persisters/index.js +9 -9
  51. package/persisters/persister-durable-object-storage/index.js +12 -12
  52. package/persisters/persister-durable-object-storage/with-schemas/index.js +12 -12
  53. package/persisters/persister-pglite/index.js +11 -11
  54. package/persisters/persister-pglite/with-schemas/index.js +11 -11
  55. package/persisters/persister-postgres/index.js +11 -11
  56. package/persisters/persister-postgres/with-schemas/index.js +11 -11
  57. package/persisters/with-schemas/index.js +9 -9
  58. package/readme.md +2 -2
  59. package/releases.md +9 -10
  60. package/store/index.js +4 -4
  61. package/store/with-schemas/index.js +4 -4
  62. package/synchronizers/index.js +7 -7
  63. package/synchronizers/synchronizer-broadcast-channel/index.js +7 -7
  64. package/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +7 -7
  65. package/synchronizers/synchronizer-local/index.js +7 -7
  66. package/synchronizers/synchronizer-local/with-schemas/index.js +7 -7
  67. package/synchronizers/synchronizer-ws-client/index.js +7 -7
  68. package/synchronizers/synchronizer-ws-client/with-schemas/index.js +7 -7
  69. package/synchronizers/synchronizer-ws-server/index.js +7 -7
  70. package/synchronizers/synchronizer-ws-server/with-schemas/index.js +7 -7
  71. package/synchronizers/synchronizer-ws-server-durable-object/index.js +7 -7
  72. package/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +7 -7
  73. package/synchronizers/with-schemas/index.js +7 -7
  74. package/ui-react-inspector/index.js +1 -1
  75. package/ui-react-inspector/with-schemas/index.js +1 -1
  76. package/with-schemas/index.js +133 -86
@@ -176,8 +176,8 @@ const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
176
176
 
177
177
  const object = Object;
178
178
  const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
179
- const objEntries = object.entries;
180
179
  const objFrozen = object.isFrozen;
180
+ const objEntries = object.entries;
181
181
  const isObject = (obj) =>
182
182
  !isUndefined(obj) &&
183
183
  ifNotUndefined(
@@ -859,16 +859,60 @@ const getUniqueId$1 = (length = 16) =>
859
859
  '',
860
860
  );
861
861
 
862
+ const jsonString = JSON.stringify;
863
+ const jsonParse = JSON.parse;
864
+ const jsonStringWithMap = (obj) =>
865
+ jsonString(obj, (_key, value) =>
866
+ isInstanceOf(value, Map) ? object.fromEntries([...value]) : value,
867
+ );
868
+ const jsonStringWithUndefined = (obj) =>
869
+ jsonString(obj, (_key, value) => (value === void 0 ? UNDEFINED : value));
870
+ const jsonParseWithUndefined = (str) =>
871
+ jsonParse(str, (_key, value) => (value === UNDEFINED ? void 0 : value));
872
+
862
873
  const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
863
- const getHash = (value) => {
874
+ const getHash = (string) => {
864
875
  let hash = 2166136261;
865
- arrayForEach(textEncoder.encode(value), (char) => {
876
+ arrayForEach(textEncoder.encode(string), (char) => {
866
877
  hash ^= char;
867
878
  hash +=
868
879
  (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
869
880
  });
870
881
  return hash >>> 0;
871
882
  };
883
+ const addOrRemoveHash = (hash1, hash2) => (hash1 ^ hash2) >>> 0;
884
+ const getValuesHash = (valueHashes) =>
885
+ arrayReduce(
886
+ objEntries(valueHashes),
887
+ (valuesHash, [valueId, valueHash]) =>
888
+ addOrRemoveHash(
889
+ valuesHash,
890
+ getValueInValuesHash(valueId, valueHash) ^
891
+ getValueInValuesHash(valueId, 0),
892
+ // legacy v5; remove in v7
893
+ ),
894
+ 0,
895
+ // legacy v5; valuesHlc in v7?
896
+ );
897
+ const getValueInValuesHash = (valueId, valueHash) =>
898
+ getHash(valueId + ':' + valueHash);
899
+ const getValueHash = (value, valueHlc) =>
900
+ getHash(jsonStringWithMap(value ?? null) + ':' + valueHlc);
901
+ const getCellHash = getValueHash;
902
+ const getCellInRowHash = getValueInValuesHash;
903
+ const getRowHash = getValuesHash;
904
+ const getRowInTableHash = getValueInValuesHash;
905
+ const getTableHash = (rowHashes) =>
906
+ // alias to getValuesHash in v7
907
+ arrayReduce(
908
+ objEntries(rowHashes),
909
+ (valuesHash, [rowId, rowHash]) =>
910
+ addOrRemoveHash(valuesHash, getValueInValuesHash(rowId, rowHash)),
911
+ 0,
912
+ // legacy v5; rowHlc in v7?
913
+ );
914
+ const getTableInTablesHash = getValueInValuesHash;
915
+ const getTablesHash = getTableHash;
872
916
 
873
917
  const SHIFT36 = 2 ** 36;
874
918
  const SHIFT30 = 2 ** 30;
@@ -1164,42 +1208,30 @@ const createIndexes = getCreateFunction((store) => {
1164
1208
  return objFreeze(indexes);
1165
1209
  });
1166
1210
 
1167
- const jsonString = JSON.stringify;
1168
- const jsonParse = JSON.parse;
1169
- const jsonStringWithMap = (obj) =>
1170
- jsonString(obj, (_key, value) =>
1171
- isInstanceOf(value, Map) ? object.fromEntries([...value]) : value,
1172
- );
1173
- const jsonStringWithUndefined = (obj) =>
1174
- jsonString(obj, (_key, value) => (value === void 0 ? UNDEFINED : value));
1175
- const jsonParseWithUndefined = (str) =>
1176
- jsonParse(str, (_key, value) => (value === UNDEFINED ? void 0 : value));
1177
-
1178
- const stampClone = ([value, time]) => stampNew(value, time);
1179
- const stampCloneWithHash = ([value, time, hash]) => [value, time, hash];
1180
- const stampNew = (value, time) => (time ? [value, time] : [value]);
1181
- const stampNewWithHash = (value, time, hash) => [value, time, hash];
1211
+ const stampClone = ([value, hlc]) => stampNew(value, hlc);
1212
+ const stampCloneWithHash = ([value, hlc, hash]) => [value, hlc, hash];
1213
+ const stampNew = (value, hlc) => (hlc ? [value, hlc] : [value]);
1214
+ const stampNewWithHash = (value, hlc, hash) => [value, hlc, hash];
1182
1215
  const getStampHash = (stamp) => stamp[2];
1183
- const hashIdAndHash = (id, hash) => getHash(id + ':' + hash);
1184
- const replaceTimeHash = (oldTime, newTime) =>
1185
- newTime > oldTime ? (oldTime ? getHash(oldTime) : 0) ^ getHash(newTime) : 0;
1186
- const getLatestTime = (time1, time2) =>
1216
+ const replaceHlcHash = (oldHlc, newHlc) =>
1217
+ newHlc > oldHlc ? (oldHlc ? getHash(oldHlc) : 0) ^ getHash(newHlc) : 0;
1218
+ const getLatestHlc = (hlc1, hlc2) =>
1187
1219
  /* istanbul ignore next */
1188
- ((time1 ?? '') > (time2 ?? '') ? time1 : time2) ?? '';
1189
- const stampUpdate = (stamp, time, hash) => {
1190
- if (time > stamp[1]) {
1191
- stamp[1] = time;
1220
+ ((hlc1 ?? '') > (hlc2 ?? '') ? hlc1 : hlc2) ?? '';
1221
+ const stampUpdate = (stamp, hlc, hash) => {
1222
+ if (hlc > stamp[1]) {
1223
+ stamp[1] = hlc;
1192
1224
  }
1193
1225
  stamp[2] = hash >>> 0;
1194
1226
  };
1195
- const stampNewObj = (time = EMPTY_STRING) => stampNew(objNew(), time);
1196
- const stampNewMap = (time = EMPTY_STRING) => [mapNew(), time, 0];
1227
+ const stampNewObj = (hlc = EMPTY_STRING) => stampNew(objNew(), hlc);
1228
+ const stampNewMap = (hlc = EMPTY_STRING) => [mapNew(), hlc, 0];
1197
1229
  const stampMapToObjWithHash = (
1198
- [map, time, hash],
1230
+ [map, hlc, hash],
1199
1231
  mapper = stampCloneWithHash,
1200
- ) => [mapToObj(map, mapper), time, hash];
1201
- const stampMapToObjWithoutHash = ([map, time], mapper = stampClone) =>
1202
- stampNew(mapToObj(map, mapper), time);
1232
+ ) => [mapToObj(map, mapper), hlc, hash];
1233
+ const stampMapToObjWithoutHash = ([map, hlc], mapper = stampClone) =>
1234
+ stampNew(mapToObj(map, mapper), hlc);
1203
1235
  const stampValidate = (stamp, validateThing) =>
1204
1236
  isArray(stamp) &&
1205
1237
  size(stamp) == 3 &&
@@ -2644,25 +2676,25 @@ const createMergeableStore = (uniqueId, getNow) => {
2644
2676
  const tablesChanges = {};
2645
2677
  const valuesChanges = {};
2646
2678
  const [
2647
- [tablesObj, incomingTablesTime = EMPTY_STRING, incomingTablesHash = 0],
2679
+ [tablesObj, incomingTablesHlc = EMPTY_STRING, incomingTablesHash = 0],
2648
2680
  values,
2649
2681
  ] = contentOrChanges;
2650
2682
  const [tablesStampMap, valuesStampMap] = contentStampMap;
2651
- const [tableStampMaps, oldTablesTime, oldTablesHash] = tablesStampMap;
2683
+ const [tableStampMaps, oldTablesHlc, oldTablesHash] = tablesStampMap;
2652
2684
  let tablesHash = isContent ? incomingTablesHash : oldTablesHash;
2653
- let tablesTime = incomingTablesTime;
2685
+ let tablesHlc = incomingTablesHlc;
2654
2686
  objForEach(
2655
2687
  tablesObj,
2656
2688
  (
2657
- [rowsObj, incomingTableTime = EMPTY_STRING, incomingTableHash = 0],
2689
+ [rowsObj, incomingTableHlc = EMPTY_STRING, incomingTableHash = 0],
2658
2690
  tableId,
2659
2691
  ) => {
2660
2692
  const tableStampMap = mapEnsure(tableStampMaps, tableId, stampNewMap);
2661
- const [rowStampMaps, oldTableTime, oldTableHash] = tableStampMap;
2693
+ const [rowStampMaps, oldTableHlc, oldTableHash] = tableStampMap;
2662
2694
  let tableHash = isContent ? incomingTableHash : oldTableHash;
2663
- let tableTime = incomingTableTime;
2695
+ let tableHlc = incomingTableHlc;
2664
2696
  objForEach(rowsObj, (row, rowId) => {
2665
- const [rowTime, oldRowHash, rowHash] = mergeCellsOrValues(
2697
+ const [rowHlc, oldRowHash, rowHash] = mergeCellsOrValues(
2666
2698
  row,
2667
2699
  mapEnsure(rowStampMaps, rowId, stampNewMap),
2668
2700
  objEnsure(objEnsure(tablesChanges, tableId, objNew), rowId, objNew),
@@ -2670,32 +2702,36 @@ const createMergeableStore = (uniqueId, getNow) => {
2670
2702
  );
2671
2703
  tableHash ^= isContent
2672
2704
  ? 0
2673
- : (oldRowHash ? hashIdAndHash(rowId, oldRowHash) : 0) ^
2674
- hashIdAndHash(rowId, rowHash);
2675
- tableTime = getLatestTime(tableTime, rowTime);
2705
+ : addOrRemoveHash(
2706
+ oldRowHash ? getValueInValuesHash(rowId, oldRowHash) : 0,
2707
+ getValueInValuesHash(rowId, rowHash),
2708
+ );
2709
+ tableHlc = getLatestHlc(tableHlc, rowHlc);
2676
2710
  });
2677
2711
  tableHash ^= isContent
2678
2712
  ? 0
2679
- : replaceTimeHash(oldTableTime, incomingTableTime);
2680
- stampUpdate(tableStampMap, incomingTableTime, tableHash);
2713
+ : replaceHlcHash(oldTableHlc, incomingTableHlc);
2714
+ stampUpdate(tableStampMap, incomingTableHlc, tableHash);
2681
2715
  tablesHash ^= isContent
2682
2716
  ? 0
2683
- : (oldTableHash ? hashIdAndHash(tableId, oldTableHash) : 0) ^
2684
- hashIdAndHash(tableId, tableStampMap[2]);
2685
- tablesTime = getLatestTime(tablesTime, tableTime);
2717
+ : addOrRemoveHash(
2718
+ oldTableHash ? getValueInValuesHash(tableId, oldTableHash) : 0,
2719
+ getValueInValuesHash(tableId, tableStampMap[2]),
2720
+ );
2721
+ tablesHlc = getLatestHlc(tablesHlc, tableHlc);
2686
2722
  },
2687
2723
  );
2688
2724
  tablesHash ^= isContent
2689
2725
  ? 0
2690
- : replaceTimeHash(oldTablesTime, incomingTablesTime);
2691
- stampUpdate(tablesStampMap, incomingTablesTime, tablesHash);
2692
- const [valuesTime] = mergeCellsOrValues(
2726
+ : replaceHlcHash(oldTablesHlc, incomingTablesHlc);
2727
+ stampUpdate(tablesStampMap, incomingTablesHlc, tablesHash);
2728
+ const [valuesHlc] = mergeCellsOrValues(
2693
2729
  values,
2694
2730
  valuesStampMap,
2695
2731
  valuesChanges,
2696
2732
  isContent,
2697
2733
  );
2698
- seenHlc(getLatestTime(tablesTime, valuesTime));
2734
+ seenHlc(getLatestHlc(tablesHlc, valuesHlc));
2699
2735
  return [tablesChanges, valuesChanges, 1];
2700
2736
  };
2701
2737
  const mergeCellsOrValues = (
@@ -2706,44 +2742,44 @@ const createMergeableStore = (uniqueId, getNow) => {
2706
2742
  ) => {
2707
2743
  const [
2708
2744
  thingsObj,
2709
- incomingThingsTime = EMPTY_STRING,
2745
+ incomingThingsHlc = EMPTY_STRING,
2710
2746
  incomingThingsHash = 0,
2711
2747
  ] = things;
2712
- const [thingStampMaps, oldThingsTime, oldThingsHash] = thingsStampMap;
2713
- let thingsTime = incomingThingsTime;
2748
+ const [thingStampMaps, oldThingsHlc, oldThingsHash] = thingsStampMap;
2749
+ let thingsHlc = incomingThingsHlc;
2714
2750
  let thingsHash = isContent ? incomingThingsHash : oldThingsHash;
2715
2751
  objForEach(
2716
2752
  thingsObj,
2717
- ([thing, thingTime = EMPTY_STRING, incomingThingHash = 0], thingId) => {
2753
+ ([thing, thingHlc = EMPTY_STRING, incomingThingHash = 0], thingId) => {
2718
2754
  const thingStampMap = mapEnsure(thingStampMaps, thingId, () => [
2719
2755
  void 0,
2720
2756
  EMPTY_STRING,
2721
2757
  0,
2722
2758
  ]);
2723
- const [, oldThingTime, oldThingHash] = thingStampMap;
2724
- if (!oldThingTime || thingTime > oldThingTime) {
2759
+ const [, oldThingHlc, oldThingHash] = thingStampMap;
2760
+ if (!oldThingHlc || thingHlc > oldThingHlc) {
2725
2761
  stampUpdate(
2726
2762
  thingStampMap,
2727
- thingTime,
2728
- isContent
2729
- ? incomingThingHash
2730
- : getHash(jsonStringWithMap(thing ?? null) + ':' + thingTime),
2763
+ thingHlc,
2764
+ isContent ? incomingThingHash : getValueHash(thing, thingHlc),
2731
2765
  );
2732
2766
  thingStampMap[0] = thing;
2733
2767
  thingsChanges[thingId] = thing;
2734
2768
  thingsHash ^= isContent
2735
2769
  ? 0
2736
- : hashIdAndHash(thingId, oldThingHash) ^
2737
- hashIdAndHash(thingId, thingStampMap[2]);
2738
- thingsTime = getLatestTime(thingsTime, thingTime);
2770
+ : addOrRemoveHash(
2771
+ getValueInValuesHash(thingId, oldThingHash),
2772
+ getValueInValuesHash(thingId, thingStampMap[2]),
2773
+ );
2774
+ thingsHlc = getLatestHlc(thingsHlc, thingHlc);
2739
2775
  }
2740
2776
  },
2741
2777
  );
2742
2778
  thingsHash ^= isContent
2743
2779
  ? 0
2744
- : replaceTimeHash(oldThingsTime, incomingThingsTime);
2745
- stampUpdate(thingsStampMap, incomingThingsTime, thingsHash);
2746
- return [thingsTime, oldThingsHash, thingsStampMap[2]];
2780
+ : replaceHlcHash(oldThingsHlc, incomingThingsHlc);
2781
+ stampUpdate(thingsStampMap, incomingThingsHlc, thingsHash);
2782
+ return [thingsHlc, oldThingsHash, thingsStampMap[2]];
2747
2783
  };
2748
2784
  const preStartTransaction = noop;
2749
2785
  const preFinishTransaction = noop;
@@ -2815,13 +2851,13 @@ const createMergeableStore = (uniqueId, getNow) => {
2815
2851
  const differingTableHashes = {};
2816
2852
  mapForEach(
2817
2853
  contentStampMap[0][0],
2818
- (tableId, [tableStampMap, tableTime, hash]) =>
2854
+ (tableId, [tableStampMap, tableHlc, hash]) =>
2819
2855
  objHas(otherTableHashes, tableId)
2820
2856
  ? hash != otherTableHashes[tableId]
2821
2857
  ? (differingTableHashes[tableId] = hash)
2822
2858
  : 0
2823
2859
  : (newTables[0][tableId] = stampMapToObjWithoutHash(
2824
- [tableStampMap, tableTime],
2860
+ [tableStampMap, tableHlc],
2825
2861
  (rowStampMap) => stampMapToObjWithoutHash(rowStampMap),
2826
2862
  )),
2827
2863
  );
@@ -2850,13 +2886,13 @@ const createMergeableStore = (uniqueId, getNow) => {
2850
2886
  objForEach(otherTableRowHashes, (otherRowHashes, tableId) =>
2851
2887
  mapForEach(
2852
2888
  mapGet(contentStampMap[0][0], tableId)?.[0],
2853
- (rowId, [rowStampMap, rowTime, hash]) =>
2889
+ (rowId, [rowStampMap, rowHlc, hash]) =>
2854
2890
  objHas(otherRowHashes, rowId)
2855
2891
  ? hash !== otherRowHashes[rowId]
2856
2892
  ? (objEnsure(differingRowHashes, tableId, objNew)[rowId] = hash)
2857
2893
  : 0
2858
2894
  : (objEnsure(newRows[0], tableId, stampNewObj)[0][rowId] =
2859
- stampMapToObjWithoutHash([rowStampMap, rowTime])),
2895
+ stampMapToObjWithoutHash([rowStampMap, rowHlc])),
2860
2896
  ),
2861
2897
  );
2862
2898
  return [newRows, differingRowHashes];
@@ -2887,43 +2923,43 @@ const createMergeableStore = (uniqueId, getNow) => {
2887
2923
  return cellHashes;
2888
2924
  };
2889
2925
  const getMergeableCellDiff = (otherTableRowCellHashes) => {
2890
- const [[tableStampMaps, tablesTime]] = contentStampMap;
2926
+ const [[tableStampMaps, tablesHlc]] = contentStampMap;
2891
2927
  const tablesObj = {};
2892
2928
  objForEach(otherTableRowCellHashes, (otherRowCellHashes, tableId) =>
2893
2929
  objForEach(otherRowCellHashes, (otherCellHashes, rowId) =>
2894
2930
  ifNotUndefined(
2895
2931
  mapGet(tableStampMaps, tableId),
2896
- ([rowStampMaps, tableTime]) =>
2932
+ ([rowStampMaps, tableHlc]) =>
2897
2933
  ifNotUndefined(
2898
2934
  mapGet(rowStampMaps, rowId),
2899
- ([cellStampMaps, rowTime]) =>
2900
- mapForEach(cellStampMaps, (cellId, [cell, cellTime, hash]) =>
2935
+ ([cellStampMaps, rowHlc]) =>
2936
+ mapForEach(cellStampMaps, (cellId, [cell, cellHlc, hash]) =>
2901
2937
  hash !== otherCellHashes[cellId]
2902
2938
  ? (objEnsure(
2903
2939
  objEnsure(tablesObj, tableId, () =>
2904
- stampNewObj(tableTime),
2940
+ stampNewObj(tableHlc),
2905
2941
  )[0],
2906
2942
  rowId,
2907
- () => stampNewObj(rowTime),
2908
- )[0][cellId] = [cell, cellTime])
2943
+ () => stampNewObj(rowHlc),
2944
+ )[0][cellId] = [cell, cellHlc])
2909
2945
  : 0,
2910
2946
  ),
2911
2947
  ),
2912
2948
  ),
2913
2949
  ),
2914
2950
  );
2915
- return stampNew(tablesObj, tablesTime);
2951
+ return stampNew(tablesObj, tablesHlc);
2916
2952
  };
2917
2953
  const getMergeableValueHashes = () =>
2918
2954
  mapToObj(contentStampMap[1][0], getStampHash);
2919
2955
  const getMergeableValueDiff = (otherValueHashes) => {
2920
- const [, [valueStampMaps, valuesTime]] = contentStampMap;
2956
+ const [, [valueStampMaps, valuesHlc]] = contentStampMap;
2921
2957
  const values = mapToObj(
2922
2958
  valueStampMaps,
2923
2959
  stampClone,
2924
2960
  ([, , hash], valueId) => hash == otherValueHashes?.[valueId],
2925
2961
  );
2926
- return stampNew(values, valuesTime);
2962
+ return stampNew(values, valuesHlc);
2927
2963
  };
2928
2964
  const setMergeableContent = (mergeableContent) =>
2929
2965
  disableListeningToRawStoreChanges(() =>
@@ -2945,20 +2981,20 @@ const createMergeableStore = (uniqueId, getNow) => {
2945
2981
  };
2946
2982
  const getTransactionMergeableChanges = (withHashes = false) => {
2947
2983
  const [
2948
- [tableStampMaps, tablesTime, tablesHash],
2949
- [valueStampMaps, valuesTime, valuesHash],
2984
+ [tableStampMaps, tablesHlc, tablesHash],
2985
+ [valueStampMaps, valuesHlc, valuesHash],
2950
2986
  ] = contentStampMap;
2951
2987
  const newStamp = withHashes ? stampNewWithHash : stampNew;
2952
2988
  const tablesObj = {};
2953
2989
  collForEach(touchedCells, (touchedTable, tableId) =>
2954
2990
  ifNotUndefined(
2955
2991
  mapGet(tableStampMaps, tableId),
2956
- ([rowStampMaps, tableTime, tableHash]) => {
2992
+ ([rowStampMaps, tableHlc, tableHash]) => {
2957
2993
  const tableObj = {};
2958
2994
  collForEach(touchedTable, (touchedRow, rowId) =>
2959
2995
  ifNotUndefined(
2960
2996
  mapGet(rowStampMaps, rowId),
2961
- ([cellStampMaps, rowTime, rowHash]) => {
2997
+ ([cellStampMaps, rowHlc, rowHash]) => {
2962
2998
  const rowObj = {};
2963
2999
  collForEach(touchedRow, (cellId) => {
2964
3000
  ifNotUndefined(
@@ -2967,11 +3003,11 @@ const createMergeableStore = (uniqueId, getNow) => {
2967
3003
  (rowObj[cellId] = newStamp(cell, time, hash)),
2968
3004
  );
2969
3005
  });
2970
- tableObj[rowId] = newStamp(rowObj, rowTime, rowHash);
3006
+ tableObj[rowId] = newStamp(rowObj, rowHlc, rowHash);
2971
3007
  },
2972
3008
  ),
2973
3009
  );
2974
- tablesObj[tableId] = newStamp(tableObj, tableTime, tableHash);
3010
+ tablesObj[tableId] = newStamp(tableObj, tableHlc, tableHash);
2975
3011
  },
2976
3012
  ),
2977
3013
  );
@@ -2984,8 +3020,8 @@ const createMergeableStore = (uniqueId, getNow) => {
2984
3020
  ),
2985
3021
  );
2986
3022
  return [
2987
- newStamp(tablesObj, tablesTime, tablesHash),
2988
- newStamp(valuesObj, valuesTime, valuesHash),
3023
+ newStamp(tablesObj, tablesHlc, tablesHash),
3024
+ newStamp(valuesObj, valuesHlc, valuesHash),
2989
3025
  1,
2990
3026
  ];
2991
3027
  };
@@ -4608,18 +4644,18 @@ const createDurableObjectStoragePersister = (
4608
4644
  const setPersisted = async (
4609
4645
  getContent,
4610
4646
  [
4611
- [tablesObj, tablesTime, tablesHash],
4612
- [valuesObj, valuesTime, valuesHash],
4647
+ [tablesObj, tablesHlc, tablesHash],
4648
+ [valuesObj, valuesHlc, valuesHash],
4613
4649
  ] = getContent(),
4614
4650
  ) => {
4615
4651
  const keysToSet = mapNew();
4616
- mapSet(keysToSet, constructKey(T), [0, tablesTime, tablesHash]);
4617
- objForEach(tablesObj, ([tableObj, tableTime, tableHash], tableId) => {
4618
- mapSet(keysToSet, constructKey(T, tableId), [0, tableTime, tableHash]);
4619
- objForEach(tableObj, ([rowObj, rowTime, rowHash], rowId) => {
4652
+ mapSet(keysToSet, constructKey(T), [0, tablesHlc, tablesHash]);
4653
+ objForEach(tablesObj, ([tableObj, tableHlc, tableHash], tableId) => {
4654
+ mapSet(keysToSet, constructKey(T, tableId), [0, tableHlc, tableHash]);
4655
+ objForEach(tableObj, ([rowObj, rowHlc, rowHash], rowId) => {
4620
4656
  mapSet(keysToSet, constructKey(T, tableId, rowId), [
4621
4657
  0,
4622
- rowTime,
4658
+ rowHlc,
4623
4659
  rowHash,
4624
4660
  ]);
4625
4661
  objForEach(rowObj, (cellStamp, cellId) =>
@@ -4627,7 +4663,7 @@ const createDurableObjectStoragePersister = (
4627
4663
  );
4628
4664
  });
4629
4665
  });
4630
- mapSet(keysToSet, constructKey(V), [0, valuesTime, valuesHash]);
4666
+ mapSet(keysToSet, constructKey(V), [0, valuesHlc, valuesHash]);
4631
4667
  objForEach(valuesObj, (valueStamp, valueId) =>
4632
4668
  mapSet(keysToSet, constructKey(V, valueId), valueStamp),
4633
4669
  );
@@ -6335,11 +6371,11 @@ const createCustomSynchronizer = (
6335
6371
  ([cell2, cellTime2], cellId) =>
6336
6372
  (rowStamp[0][cellId] = stampNew(cell2, cellTime2)),
6337
6373
  );
6338
- rowStamp[1] = getLatestTime(rowStamp[1], rowTime2);
6374
+ rowStamp[1] = getLatestHlc(rowStamp[1], rowTime2);
6339
6375
  });
6340
- tableStamp[1] = getLatestTime(tableStamp[1], tableTime2);
6376
+ tableStamp[1] = getLatestHlc(tableStamp[1], tableTime2);
6341
6377
  });
6342
- tablesStamp[1] = getLatestTime(tablesStamp[1], tablesTime2);
6378
+ tablesStamp[1] = getLatestHlc(tablesStamp[1], tablesTime2);
6343
6379
  };
6344
6380
  const getChangesFromOtherStore = (
6345
6381
  otherClientId = null,
@@ -10364,6 +10400,7 @@ const Inspector = ({position = 'right', open = false}) => {
10364
10400
  };
10365
10401
 
10366
10402
  export {
10403
+ addOrRemoveHash,
10367
10404
  BackwardCheckpointsView,
10368
10405
  broadcastChanges,
10369
10406
  CellView,
@@ -10408,9 +10445,19 @@ export {
10408
10445
  EditableCellView,
10409
10446
  EditableValueView,
10410
10447
  ForwardCheckpointsView,
10448
+ getCellHash,
10449
+ getCellInRowHash,
10450
+ getHash,
10411
10451
  getHlcFunctions,
10412
- getRandomValues,
10452
+ getRowHash,
10453
+ getRowInTableHash,
10454
+ getTableHash,
10455
+ getTableInTablesHash,
10456
+ getTablesHash,
10413
10457
  getUniqueId$1 as getUniqueId,
10458
+ getValueHash,
10459
+ getValueInValuesHash,
10460
+ getValuesHash,
10414
10461
  getWsServerDurableObjectFetch,
10415
10462
  hasStoreInStorage,
10416
10463
  IndexView$1 as IndexView,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinybase",
3
- "version": "6.2.0-beta.3",
3
+ "version": "6.2.0",
4
4
  "author": "jamesgpearce",
5
5
  "repository": "github:tinyplex/tinybase",
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "sideEffects": false,
23
23
  "peerDependencies": {
24
24
  "@automerge/automerge-repo": "^1.2.1",
25
- "@cloudflare/workers-types": "^4.20250529.0",
25
+ "@cloudflare/workers-types": "^4.20250531.0",
26
26
  "@electric-sql/pglite": "^0.2.17",
27
- "@libsql/client": "^0.15.7",
27
+ "@libsql/client": "^0.15.8",
28
28
  "@powersync/common": "^1.31.0",
29
29
  "@sqlite.org/sqlite-wasm": "^3.49.2-build1",
30
30
  "@vlcn.io/crsqlite-wasm": "^0.16.0",
@@ -468,10 +468,17 @@ const createCustomPersister = (
468
468
  return objFreeze(persister);
469
469
  };
470
470
 
471
+ const jsonString = JSON.stringify;
472
+ const jsonParse = JSON.parse;
473
+ const jsonStringWithUndefined = (obj) =>
474
+ jsonString(obj, (_key, value) => (value === void 0 ? UNDEFINED : value));
475
+ const jsonParseWithUndefined = (str) =>
476
+ jsonParse(str, (_key, value) => (value === UNDEFINED ? void 0 : value));
477
+
471
478
  const textEncoder = /* @__PURE__ */ new GLOBAL.TextEncoder();
472
- const getHash = (value) => {
479
+ const getHash = (string) => {
473
480
  let hash = 2166136261;
474
- arrayForEach(textEncoder.encode(value), (char) => {
481
+ arrayForEach(textEncoder.encode(string), (char) => {
475
482
  hash ^= char;
476
483
  hash +=
477
484
  (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
@@ -479,13 +486,6 @@ const getHash = (value) => {
479
486
  return hash >>> 0;
480
487
  };
481
488
 
482
- const jsonString = JSON.stringify;
483
- const jsonParse = JSON.parse;
484
- const jsonStringWithUndefined = (obj) =>
485
- jsonString(obj, (_key, value) => (value === void 0 ? UNDEFINED : value));
486
- const jsonParseWithUndefined = (str) =>
487
- jsonParse(str, (_key, value) => (value === UNDEFINED ? void 0 : value));
488
-
489
489
  const SINGLE_ROW_ID = '_';
490
490
  const DEFAULT_ROW_ID_COLUMN_NAME = '_id';
491
491
  const SELECT = 'SELECT';
@@ -113,10 +113,10 @@ const visitTree = (node, path, ensureLeaf, pruneLeaf, p = 0) =>
113
113
  },
114
114
  );
115
115
 
116
- const stampNewWithHash = (value, time, hash) => [value, time, hash];
117
- const stampUpdate = (stamp, time, hash) => {
118
- if (time > stamp[1]) {
119
- stamp[1] = time;
116
+ const stampNewWithHash = (value, hlc, hash) => [value, hlc, hash];
117
+ const stampUpdate = (stamp, hlc, hash) => {
118
+ if (hlc > stamp[1]) {
119
+ stamp[1] = hlc;
120
120
  }
121
121
  stamp[2] = hash >>> 0;
122
122
  };
@@ -523,18 +523,18 @@ const createDurableObjectStoragePersister = (
523
523
  const setPersisted = async (
524
524
  getContent,
525
525
  [
526
- [tablesObj, tablesTime, tablesHash],
527
- [valuesObj, valuesTime, valuesHash],
526
+ [tablesObj, tablesHlc, tablesHash],
527
+ [valuesObj, valuesHlc, valuesHash],
528
528
  ] = getContent(),
529
529
  ) => {
530
530
  const keysToSet = mapNew();
531
- mapSet(keysToSet, constructKey(T), [0, tablesTime, tablesHash]);
532
- objForEach(tablesObj, ([tableObj, tableTime, tableHash], tableId) => {
533
- mapSet(keysToSet, constructKey(T, tableId), [0, tableTime, tableHash]);
534
- objForEach(tableObj, ([rowObj, rowTime, rowHash], rowId) => {
531
+ mapSet(keysToSet, constructKey(T), [0, tablesHlc, tablesHash]);
532
+ objForEach(tablesObj, ([tableObj, tableHlc, tableHash], tableId) => {
533
+ mapSet(keysToSet, constructKey(T, tableId), [0, tableHlc, tableHash]);
534
+ objForEach(tableObj, ([rowObj, rowHlc, rowHash], rowId) => {
535
535
  mapSet(keysToSet, constructKey(T, tableId, rowId), [
536
536
  0,
537
- rowTime,
537
+ rowHlc,
538
538
  rowHash,
539
539
  ]);
540
540
  objForEach(rowObj, (cellStamp, cellId) =>
@@ -542,7 +542,7 @@ const createDurableObjectStoragePersister = (
542
542
  );
543
543
  });
544
544
  });
545
- mapSet(keysToSet, constructKey(V), [0, valuesTime, valuesHash]);
545
+ mapSet(keysToSet, constructKey(V), [0, valuesHlc, valuesHash]);
546
546
  objForEach(valuesObj, (valueStamp, valueId) =>
547
547
  mapSet(keysToSet, constructKey(V, valueId), valueStamp),
548
548
  );
@@ -113,10 +113,10 @@ const visitTree = (node, path, ensureLeaf, pruneLeaf, p = 0) =>
113
113
  },
114
114
  );
115
115
 
116
- const stampNewWithHash = (value, time, hash) => [value, time, hash];
117
- const stampUpdate = (stamp, time, hash) => {
118
- if (time > stamp[1]) {
119
- stamp[1] = time;
116
+ const stampNewWithHash = (value, hlc, hash) => [value, hlc, hash];
117
+ const stampUpdate = (stamp, hlc, hash) => {
118
+ if (hlc > stamp[1]) {
119
+ stamp[1] = hlc;
120
120
  }
121
121
  stamp[2] = hash >>> 0;
122
122
  };
@@ -523,18 +523,18 @@ const createDurableObjectStoragePersister = (
523
523
  const setPersisted = async (
524
524
  getContent,
525
525
  [
526
- [tablesObj, tablesTime, tablesHash],
527
- [valuesObj, valuesTime, valuesHash],
526
+ [tablesObj, tablesHlc, tablesHash],
527
+ [valuesObj, valuesHlc, valuesHash],
528
528
  ] = getContent(),
529
529
  ) => {
530
530
  const keysToSet = mapNew();
531
- mapSet(keysToSet, constructKey(T), [0, tablesTime, tablesHash]);
532
- objForEach(tablesObj, ([tableObj, tableTime, tableHash], tableId) => {
533
- mapSet(keysToSet, constructKey(T, tableId), [0, tableTime, tableHash]);
534
- objForEach(tableObj, ([rowObj, rowTime, rowHash], rowId) => {
531
+ mapSet(keysToSet, constructKey(T), [0, tablesHlc, tablesHash]);
532
+ objForEach(tablesObj, ([tableObj, tableHlc, tableHash], tableId) => {
533
+ mapSet(keysToSet, constructKey(T, tableId), [0, tableHlc, tableHash]);
534
+ objForEach(tableObj, ([rowObj, rowHlc, rowHash], rowId) => {
535
535
  mapSet(keysToSet, constructKey(T, tableId, rowId), [
536
536
  0,
537
- rowTime,
537
+ rowHlc,
538
538
  rowHash,
539
539
  ]);
540
540
  objForEach(rowObj, (cellStamp, cellId) =>
@@ -542,7 +542,7 @@ const createDurableObjectStoragePersister = (
542
542
  );
543
543
  });
544
544
  });
545
- mapSet(keysToSet, constructKey(V), [0, valuesTime, valuesHash]);
545
+ mapSet(keysToSet, constructKey(V), [0, valuesHlc, valuesHash]);
546
546
  objForEach(valuesObj, (valueStamp, valueId) =>
547
547
  mapSet(keysToSet, constructKey(V, valueId), valueStamp),
548
548
  );