@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.
@@ -49,6 +49,8 @@ export { exponentialBackoff, isSerializationFailure, RetriesExhaustedError } fro
49
49
  export type { ExponentialBackoffOptions, RetryPolicy } from './retry';
50
50
  export { CdcConsumerSlowError, createSyncEngine, MissedChangesError, MutationQueueOverflowError, SchemaError, SubscriptionLimitError, UnauthorizedError } from './syncEngine';
51
51
  export type { ChangeLogSnapshot, CrdtFields, LoggedChange, StreamChangesOptions, SubscribeArgs, Subscription, SyncEngine, SyncEngineOptions } from './syncEngine';
52
+ export { EngineFencedError } from './migrate';
53
+ export type { EngineSnapshot, ExportSnapshotOptions, FenceHandle, ImportSnapshotOptions, MigrationImportResult } from './migrate';
52
54
  export { syncCdc } from './cdc';
53
55
  export type { SyncCdcOptions } from './cdc';
54
56
  export type { CrdtMergeable } from '../crdt';
@@ -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) {
@@ -2721,6 +2732,10 @@ var createSyncEngine = (options = {}) => {
2721
2732
  }
2722
2733
  });
2723
2734
  try {
2735
+ if (activeFences.size > 0) {
2736
+ const oldest = activeFences.values().next().value;
2737
+ throw new EngineFencedError(oldest.reason);
2738
+ }
2724
2739
  const mutation = mutations.get(name);
2725
2740
  if (mutation === undefined) {
2726
2741
  throw new Error(`Unknown mutation "${name}"`);
@@ -3096,6 +3111,70 @@ var createSyncEngine = (options = {}) => {
3096
3111
  }
3097
3112
  return { asOfAt, asOfVersion, rows, truncated };
3098
3113
  },
3114
+ fence: ({ reason }) => {
3115
+ const handle = {
3116
+ fencedAt: Date.now(),
3117
+ reason,
3118
+ lift: () => {
3119
+ activeFences.delete(handle);
3120
+ }
3121
+ };
3122
+ activeFences.add(handle);
3123
+ return handle;
3124
+ },
3125
+ exportSnapshot: async ({ tables, ctx = {} } = {}) => {
3126
+ const tableFilter = tables !== undefined ? new Set(tables) : undefined;
3127
+ const rows = {};
3128
+ for (const [table, reader] of readers) {
3129
+ if (tableFilter !== undefined && !tableFilter.has(table)) {
3130
+ continue;
3131
+ }
3132
+ const iterable = await reader.all(ctx);
3133
+ rows[table] = [...iterable];
3134
+ }
3135
+ return {
3136
+ exportedAt: Date.now(),
3137
+ sourceInstanceId: instanceId,
3138
+ tables: rows,
3139
+ version
3140
+ };
3141
+ },
3142
+ importSnapshot: async (snapshot, { tables, onProgress, ctx = {} } = {}) => {
3143
+ const tableFilter = tables !== undefined ? new Set(tables) : undefined;
3144
+ const perTable = {};
3145
+ const skipped = [];
3146
+ let tablesImported = 0;
3147
+ let rowsImported = 0;
3148
+ for (const [table, snapshotRows] of Object.entries(snapshot.tables)) {
3149
+ if (tableFilter !== undefined && !tableFilter.has(table)) {
3150
+ continue;
3151
+ }
3152
+ const writer = writers.get(table);
3153
+ if (writer === undefined) {
3154
+ skipped.push(table);
3155
+ continue;
3156
+ }
3157
+ const total = snapshotRows.length;
3158
+ let done = 0;
3159
+ for (const row of snapshotRows) {
3160
+ await writer.insert(row, ctx, undefined);
3161
+ done += 1;
3162
+ rowsImported += 1;
3163
+ if (onProgress !== undefined) {
3164
+ onProgress(table, done, total);
3165
+ }
3166
+ }
3167
+ perTable[table] = done;
3168
+ if (done > 0)
3169
+ tablesImported += 1;
3170
+ }
3171
+ return {
3172
+ perTable,
3173
+ rowsImported,
3174
+ skipped,
3175
+ tablesImported
3176
+ };
3177
+ },
3099
3178
  metrics: () => {
3100
3179
  const now = Date.now();
3101
3180
  const byCollection = {};
@@ -3667,8 +3746,9 @@ export {
3667
3746
  PackMissingDependencyError,
3668
3747
  MutationQueueOverflowError,
3669
3748
  MissedChangesError,
3749
+ EngineFencedError,
3670
3750
  CdcConsumerSlowError
3671
3751
  };
3672
3752
 
3673
- //# debugId=04E0C6606054D14564756E2164756E21
3753
+ //# debugId=A8C35E8D4A587BE064756E2164756E21
3674
3754
  //# sourceMappingURL=index.js.map