@absolutejs/sync 1.18.1 → 1.19.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.
@@ -1567,6 +1567,31 @@ var createSyncEngine = (options = {}) => {
1567
1567
  const runInTransaction = options.transaction;
1568
1568
  const instanceId = options.instanceId ?? globalThis.crypto?.randomUUID?.() ?? `i${Math.random()}`;
1569
1569
  let clusterBus;
1570
+ const importChangeLog = (snapshot) => {
1571
+ if (version !== 0) {
1572
+ throw new Error(`[sync] importChangeLog: engine already has version ${version}; ` + `restore must happen before any local writes commit.`);
1573
+ }
1574
+ if (snapshot.instanceId !== instanceId) {
1575
+ throw new Error(`[sync] importChangeLog: snapshot instanceId "${snapshot.instanceId}" ` + `does not match this engine's instanceId "${instanceId}". ` + `Pass options.instanceId = "${snapshot.instanceId}" to createSyncEngine.`);
1576
+ }
1577
+ version = snapshot.version;
1578
+ for (const entry of snapshot.entries) {
1579
+ changeLog.push(entry);
1580
+ }
1581
+ while (changeLog.length > changeLogSize) {
1582
+ changeLog.shift();
1583
+ }
1584
+ if (changeLogRetainMs !== null && changeLogRetainMs > 0) {
1585
+ const cutoff = Date.now() - changeLogRetainMs;
1586
+ while (changeLog.length > 0 && changeLog[0].at < cutoff) {
1587
+ changeLog.shift();
1588
+ }
1589
+ }
1590
+ return snapshot.entries.length;
1591
+ };
1592
+ if (options.initialChangeLog !== undefined) {
1593
+ importChangeLog(options.initialChangeLog);
1594
+ }
1570
1595
  const broadcast = (changes, originVersion) => {
1571
1596
  if (clusterBus !== undefined && changes.length > 0) {
1572
1597
  clusterBus.publish({ changes, origin: instanceId, originVersion });
@@ -2090,13 +2115,20 @@ var createSyncEngine = (options = {}) => {
2090
2115
  oldestPerOrigin.set(entry.origin, entry.originVersion);
2091
2116
  }
2092
2117
  }
2118
+ const oldestLogVersion = changeLog[0]?.version;
2093
2119
  for (const [origin, lastSeen] of Object.entries(sinceVec)) {
2094
2120
  if (origin === instanceId) {
2095
2121
  if (lastSeen >= version)
2096
2122
  continue;
2097
2123
  const oldestLocal = oldestPerOrigin.get(instanceId);
2098
- if (oldestLocal === undefined || oldestLocal > lastSeen + 1)
2124
+ if (oldestLocal !== undefined) {
2125
+ if (oldestLocal > lastSeen + 1)
2126
+ return false;
2127
+ continue;
2128
+ }
2129
+ if (oldestLogVersion !== undefined && oldestLogVersion > lastSeen + 1) {
2099
2130
  return false;
2131
+ }
2100
2132
  } else {
2101
2133
  const oldestPeer = oldestPerOrigin.get(origin);
2102
2134
  if (oldestPeer === undefined) {
@@ -2829,6 +2861,13 @@ var createSyncEngine = (options = {}) => {
2829
2861
  }))
2830
2862
  };
2831
2863
  },
2864
+ exportChangeLog: () => ({
2865
+ entries: changeLog.slice(),
2866
+ exportedAt: Date.now(),
2867
+ instanceId,
2868
+ version
2869
+ }),
2870
+ importChangeLog,
2832
2871
  metrics: () => {
2833
2872
  const now = Date.now();
2834
2873
  const byCollection = {};
@@ -3399,5 +3438,5 @@ export {
3399
3438
  CdcConsumerSlowError
3400
3439
  };
3401
3440
 
3402
- //# debugId=2B4AA96446522F9164756E2164756E21
3441
+ //# debugId=079CCC84C9743C1064756E2164756E21
3403
3442
  //# sourceMappingURL=index.js.map