@absolutejs/sync 1.23.0 → 1.25.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.
@@ -1401,6 +1401,16 @@ var fireMetrics = (hook, record) => {
1401
1401
  }
1402
1402
  };
1403
1403
 
1404
+ // src/engine/migrate.ts
1405
+ class EngineFencedError extends Error {
1406
+ reason;
1407
+ constructor(reason) {
1408
+ super(`[sync] Engine is fenced for migration: ${reason}`);
1409
+ this.name = "EngineFencedError";
1410
+ this.reason = reason;
1411
+ }
1412
+ }
1413
+
1404
1414
  // src/engine/syncEngine.ts
1405
1415
  class UnauthorizedError extends Error {
1406
1416
  constructor(subject) {
@@ -1603,6 +1613,7 @@ var createSyncEngine = (options = {}) => {
1603
1613
  let mutationsInFlight = 0;
1604
1614
  const mutationWaiters = [];
1605
1615
  let mutationsQueued = 0;
1616
+ const activeFences = new Set;
1606
1617
  const acquireMutationSlot = async () => {
1607
1618
  const limit = options.mutationConcurrency;
1608
1619
  if (limit === undefined) {
@@ -1729,7 +1740,11 @@ var createSyncEngine = (options = {}) => {
1729
1740
  }
1730
1741
  const broadcast = (changes, originVersion) => {
1731
1742
  if (clusterBus !== undefined && changes.length > 0) {
1732
- clusterBus.publish({ changes, origin: instanceId, originVersion });
1743
+ clusterBus.publish({
1744
+ changes,
1745
+ origin: instanceId,
1746
+ originVersion
1747
+ });
1733
1748
  }
1734
1749
  };
1735
1750
  const subsFor = (collection) => {
@@ -2492,7 +2507,14 @@ var createSyncEngine = (options = {}) => {
2492
2507
  registerSearch: (collection) => {
2493
2508
  registry.set(collection.name, collection);
2494
2509
  },
2495
- subscribe: async ({ collection, params, ctx, onDiff, since, signal }) => {
2510
+ subscribe: async ({
2511
+ collection,
2512
+ params,
2513
+ ctx,
2514
+ onDiff,
2515
+ since,
2516
+ signal
2517
+ }) => {
2496
2518
  const subscribeSpan = tracer.startSpan("sync.subscribe", {
2497
2519
  attributes: {
2498
2520
  [ABS_ATTRS.engineId]: instanceId,
@@ -2521,7 +2543,10 @@ var createSyncEngine = (options = {}) => {
2521
2543
  releaseSubscriptionSlot(tenantSlot);
2522
2544
  innerUnsubscribe();
2523
2545
  };
2524
- const wrapped = { ...sub, unsubscribe: wrappedUnsubscribe };
2546
+ const wrapped = {
2547
+ ...sub,
2548
+ unsubscribe: wrappedUnsubscribe
2549
+ };
2525
2550
  linkAbortToUnsubscribe(signal, wrappedUnsubscribe);
2526
2551
  slotHandedOff = true;
2527
2552
  return wrapped;
@@ -2556,7 +2581,9 @@ var createSyncEngine = (options = {}) => {
2556
2581
  const scopedTable = tables.length === 1 ? tables[0] : undefined;
2557
2582
  const readRule = scopedTable !== undefined ? readRuleFor(scopedTable) : undefined;
2558
2583
  const rehydrate = async () => {
2559
- const raw = [...await definition.hydrate(params, ctx)];
2584
+ const raw = [
2585
+ ...await definition.hydrate(params, ctx)
2586
+ ];
2560
2587
  const rows = scopedTable !== undefined ? raw.map((row) => migrateRow(scopedTable, row)) : raw;
2561
2588
  return readRule ? rows.filter((row) => readRule(ctx, row)) : rows;
2562
2589
  };
@@ -2721,6 +2748,10 @@ var createSyncEngine = (options = {}) => {
2721
2748
  }
2722
2749
  });
2723
2750
  try {
2751
+ if (activeFences.size > 0) {
2752
+ const oldest = activeFences.values().next().value;
2753
+ throw new EngineFencedError(oldest.reason);
2754
+ }
2724
2755
  const mutation = mutations.get(name);
2725
2756
  if (mutation === undefined) {
2726
2757
  throw new Error(`Unknown mutation "${name}"`);
@@ -3096,6 +3127,73 @@ var createSyncEngine = (options = {}) => {
3096
3127
  }
3097
3128
  return { asOfAt, asOfVersion, rows, truncated };
3098
3129
  },
3130
+ fence: ({ reason }) => {
3131
+ const handle = {
3132
+ fencedAt: Date.now(),
3133
+ reason,
3134
+ lift: () => {
3135
+ activeFences.delete(handle);
3136
+ }
3137
+ };
3138
+ activeFences.add(handle);
3139
+ return handle;
3140
+ },
3141
+ exportSnapshot: async ({
3142
+ tables,
3143
+ ctx = {}
3144
+ } = {}) => {
3145
+ const tableFilter = tables !== undefined ? new Set(tables) : undefined;
3146
+ const rows = {};
3147
+ for (const [table, reader] of readers) {
3148
+ if (tableFilter !== undefined && !tableFilter.has(table)) {
3149
+ continue;
3150
+ }
3151
+ const iterable = await reader.all(ctx);
3152
+ rows[table] = [...iterable];
3153
+ }
3154
+ return {
3155
+ exportedAt: Date.now(),
3156
+ sourceInstanceId: instanceId,
3157
+ tables: rows,
3158
+ version
3159
+ };
3160
+ },
3161
+ importSnapshot: async (snapshot, { tables, onProgress, ctx = {} } = {}) => {
3162
+ const tableFilter = tables !== undefined ? new Set(tables) : undefined;
3163
+ const perTable = {};
3164
+ const skipped = [];
3165
+ let tablesImported = 0;
3166
+ let rowsImported = 0;
3167
+ for (const [table, snapshotRows] of Object.entries(snapshot.tables)) {
3168
+ if (tableFilter !== undefined && !tableFilter.has(table)) {
3169
+ continue;
3170
+ }
3171
+ const writer = writers.get(table);
3172
+ if (writer === undefined) {
3173
+ skipped.push(table);
3174
+ continue;
3175
+ }
3176
+ const total = snapshotRows.length;
3177
+ let done = 0;
3178
+ for (const row of snapshotRows) {
3179
+ await writer.insert(row, ctx, undefined);
3180
+ done += 1;
3181
+ rowsImported += 1;
3182
+ if (onProgress !== undefined) {
3183
+ onProgress(table, done, total);
3184
+ }
3185
+ }
3186
+ perTable[table] = done;
3187
+ if (done > 0)
3188
+ tablesImported += 1;
3189
+ }
3190
+ return {
3191
+ perTable,
3192
+ rowsImported,
3193
+ skipped,
3194
+ tablesImported
3195
+ };
3196
+ },
3099
3197
  metrics: () => {
3100
3198
  const now = Date.now();
3101
3199
  const byCollection = {};
@@ -3667,8 +3765,9 @@ export {
3667
3765
  PackMissingDependencyError,
3668
3766
  MutationQueueOverflowError,
3669
3767
  MissedChangesError,
3768
+ EngineFencedError,
3670
3769
  CdcConsumerSlowError
3671
3770
  };
3672
3771
 
3673
- //# debugId=04E0C6606054D14564756E2164756E21
3772
+ //# debugId=6D23BAC246E5201264756E2164756E21
3674
3773
  //# sourceMappingURL=index.js.map