@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.
package/dist/testing.js CHANGED
@@ -782,6 +782,31 @@ var createSyncEngine = (options = {}) => {
782
782
  const runInTransaction = options.transaction;
783
783
  const instanceId = options.instanceId ?? globalThis.crypto?.randomUUID?.() ?? `i${Math.random()}`;
784
784
  let clusterBus;
785
+ const importChangeLog = (snapshot) => {
786
+ if (version !== 0) {
787
+ throw new Error(`[sync] importChangeLog: engine already has version ${version}; ` + `restore must happen before any local writes commit.`);
788
+ }
789
+ if (snapshot.instanceId !== instanceId) {
790
+ throw new Error(`[sync] importChangeLog: snapshot instanceId "${snapshot.instanceId}" ` + `does not match this engine's instanceId "${instanceId}". ` + `Pass options.instanceId = "${snapshot.instanceId}" to createSyncEngine.`);
791
+ }
792
+ version = snapshot.version;
793
+ for (const entry of snapshot.entries) {
794
+ changeLog.push(entry);
795
+ }
796
+ while (changeLog.length > changeLogSize) {
797
+ changeLog.shift();
798
+ }
799
+ if (changeLogRetainMs !== null && changeLogRetainMs > 0) {
800
+ const cutoff = Date.now() - changeLogRetainMs;
801
+ while (changeLog.length > 0 && changeLog[0].at < cutoff) {
802
+ changeLog.shift();
803
+ }
804
+ }
805
+ return snapshot.entries.length;
806
+ };
807
+ if (options.initialChangeLog !== undefined) {
808
+ importChangeLog(options.initialChangeLog);
809
+ }
785
810
  const broadcast = (changes, originVersion) => {
786
811
  if (clusterBus !== undefined && changes.length > 0) {
787
812
  clusterBus.publish({ changes, origin: instanceId, originVersion });
@@ -1305,13 +1330,20 @@ var createSyncEngine = (options = {}) => {
1305
1330
  oldestPerOrigin.set(entry.origin, entry.originVersion);
1306
1331
  }
1307
1332
  }
1333
+ const oldestLogVersion = changeLog[0]?.version;
1308
1334
  for (const [origin, lastSeen] of Object.entries(sinceVec)) {
1309
1335
  if (origin === instanceId) {
1310
1336
  if (lastSeen >= version)
1311
1337
  continue;
1312
1338
  const oldestLocal = oldestPerOrigin.get(instanceId);
1313
- if (oldestLocal === undefined || oldestLocal > lastSeen + 1)
1339
+ if (oldestLocal !== undefined) {
1340
+ if (oldestLocal > lastSeen + 1)
1341
+ return false;
1342
+ continue;
1343
+ }
1344
+ if (oldestLogVersion !== undefined && oldestLogVersion > lastSeen + 1) {
1314
1345
  return false;
1346
+ }
1315
1347
  } else {
1316
1348
  const oldestPeer = oldestPerOrigin.get(origin);
1317
1349
  if (oldestPeer === undefined) {
@@ -2044,6 +2076,13 @@ var createSyncEngine = (options = {}) => {
2044
2076
  }))
2045
2077
  };
2046
2078
  },
2079
+ exportChangeLog: () => ({
2080
+ entries: changeLog.slice(),
2081
+ exportedAt: Date.now(),
2082
+ instanceId,
2083
+ version
2084
+ }),
2085
+ importChangeLog,
2047
2086
  metrics: () => {
2048
2087
  const now = Date.now();
2049
2088
  const byCollection = {};
@@ -2186,5 +2225,5 @@ export {
2186
2225
  createTestEngine
2187
2226
  };
2188
2227
 
2189
- //# debugId=7FD404FA7B2BBC1864756E2164756E21
2228
+ //# debugId=D332A9D90117FA7264756E2164756E21
2190
2229
  //# sourceMappingURL=testing.js.map