@absolutejs/sync 1.23.0 → 1.24.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
@@ -616,6 +616,16 @@ var defineSearchCollection = (definition) => ({
616
616
  kind: "search"
617
617
  });
618
618
 
619
+ // src/engine/migrate.ts
620
+ class EngineFencedError extends Error {
621
+ reason;
622
+ constructor(reason) {
623
+ super(`[sync] Engine is fenced for migration: ${reason}`);
624
+ this.name = "EngineFencedError";
625
+ this.reason = reason;
626
+ }
627
+ }
628
+
619
629
  // src/engine/syncEngine.ts
620
630
  class UnauthorizedError extends Error {
621
631
  constructor(subject) {
@@ -818,6 +828,7 @@ var createSyncEngine = (options = {}) => {
818
828
  let mutationsInFlight = 0;
819
829
  const mutationWaiters = [];
820
830
  let mutationsQueued = 0;
831
+ const activeFences = new Set;
821
832
  const acquireMutationSlot = async () => {
822
833
  const limit = options.mutationConcurrency;
823
834
  if (limit === undefined) {
@@ -1936,6 +1947,10 @@ var createSyncEngine = (options = {}) => {
1936
1947
  }
1937
1948
  });
1938
1949
  try {
1950
+ if (activeFences.size > 0) {
1951
+ const oldest = activeFences.values().next().value;
1952
+ throw new EngineFencedError(oldest.reason);
1953
+ }
1939
1954
  const mutation = mutations.get(name);
1940
1955
  if (mutation === undefined) {
1941
1956
  throw new Error(`Unknown mutation "${name}"`);
@@ -2311,6 +2326,70 @@ var createSyncEngine = (options = {}) => {
2311
2326
  }
2312
2327
  return { asOfAt, asOfVersion, rows, truncated };
2313
2328
  },
2329
+ fence: ({ reason }) => {
2330
+ const handle = {
2331
+ fencedAt: Date.now(),
2332
+ reason,
2333
+ lift: () => {
2334
+ activeFences.delete(handle);
2335
+ }
2336
+ };
2337
+ activeFences.add(handle);
2338
+ return handle;
2339
+ },
2340
+ exportSnapshot: async ({ tables, ctx = {} } = {}) => {
2341
+ const tableFilter = tables !== undefined ? new Set(tables) : undefined;
2342
+ const rows = {};
2343
+ for (const [table, reader] of readers) {
2344
+ if (tableFilter !== undefined && !tableFilter.has(table)) {
2345
+ continue;
2346
+ }
2347
+ const iterable = await reader.all(ctx);
2348
+ rows[table] = [...iterable];
2349
+ }
2350
+ return {
2351
+ exportedAt: Date.now(),
2352
+ sourceInstanceId: instanceId,
2353
+ tables: rows,
2354
+ version
2355
+ };
2356
+ },
2357
+ importSnapshot: async (snapshot, { tables, onProgress, ctx = {} } = {}) => {
2358
+ const tableFilter = tables !== undefined ? new Set(tables) : undefined;
2359
+ const perTable = {};
2360
+ const skipped = [];
2361
+ let tablesImported = 0;
2362
+ let rowsImported = 0;
2363
+ for (const [table, snapshotRows] of Object.entries(snapshot.tables)) {
2364
+ if (tableFilter !== undefined && !tableFilter.has(table)) {
2365
+ continue;
2366
+ }
2367
+ const writer = writers.get(table);
2368
+ if (writer === undefined) {
2369
+ skipped.push(table);
2370
+ continue;
2371
+ }
2372
+ const total = snapshotRows.length;
2373
+ let done = 0;
2374
+ for (const row of snapshotRows) {
2375
+ await writer.insert(row, ctx, undefined);
2376
+ done += 1;
2377
+ rowsImported += 1;
2378
+ if (onProgress !== undefined) {
2379
+ onProgress(table, done, total);
2380
+ }
2381
+ }
2382
+ perTable[table] = done;
2383
+ if (done > 0)
2384
+ tablesImported += 1;
2385
+ }
2386
+ return {
2387
+ perTable,
2388
+ rowsImported,
2389
+ skipped,
2390
+ tablesImported
2391
+ };
2392
+ },
2314
2393
  metrics: () => {
2315
2394
  const now = Date.now();
2316
2395
  const byCollection = {};
@@ -2455,5 +2534,5 @@ export {
2455
2534
  createTestEngine
2456
2535
  };
2457
2536
 
2458
- //# debugId=19605A03FFBA9EF964756E2164756E21
2537
+ //# debugId=B5794206A61554BB64756E2164756E21
2459
2538
  //# sourceMappingURL=testing.js.map