@aws-amplify/datastore 3.14.1-unstable.2 → 3.14.1

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/aws-amplify-datastore.js +2798 -1458
  3. package/dist/aws-amplify-datastore.js.map +1 -1
  4. package/dist/aws-amplify-datastore.min.js +10 -10
  5. package/dist/aws-amplify-datastore.min.js.map +1 -1
  6. package/lib/authModeStrategies/multiAuthStrategy.js +11 -0
  7. package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
  8. package/lib/datastore/datastore.js +524 -323
  9. package/lib/datastore/datastore.js.map +1 -1
  10. package/lib/storage/adapter/IndexedDBAdapter.js +76 -25
  11. package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
  12. package/lib/storage/storage.js +2 -2
  13. package/lib/storage/storage.js.map +1 -1
  14. package/lib/sync/datastoreConnectivity.js +45 -0
  15. package/lib/sync/datastoreConnectivity.js.map +1 -1
  16. package/lib/sync/index.js +518 -395
  17. package/lib/sync/index.js.map +1 -1
  18. package/lib/sync/merger.js +6 -0
  19. package/lib/sync/merger.js.map +1 -1
  20. package/lib/sync/outbox.js +66 -62
  21. package/lib/sync/outbox.js.map +1 -1
  22. package/lib/sync/processors/mutation.js +207 -165
  23. package/lib/sync/processors/mutation.js.map +1 -1
  24. package/lib/sync/processors/subscription.js +210 -175
  25. package/lib/sync/processors/subscription.js.map +1 -1
  26. package/lib/sync/processors/sync.js +95 -72
  27. package/lib/sync/processors/sync.js.map +1 -1
  28. package/lib/sync/utils.js +1 -3
  29. package/lib/sync/utils.js.map +1 -1
  30. package/lib/util.js +89 -0
  31. package/lib/util.js.map +1 -1
  32. package/lib-esm/authModeStrategies/multiAuthStrategy.d.ts +11 -0
  33. package/lib-esm/authModeStrategies/multiAuthStrategy.js +11 -0
  34. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
  35. package/lib-esm/datastore/datastore.d.ts +95 -2
  36. package/lib-esm/datastore/datastore.js +524 -323
  37. package/lib-esm/datastore/datastore.js.map +1 -1
  38. package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +21 -0
  39. package/lib-esm/storage/adapter/IndexedDBAdapter.js +77 -26
  40. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
  41. package/lib-esm/storage/storage.js +2 -2
  42. package/lib-esm/storage/storage.js.map +1 -1
  43. package/lib-esm/sync/datastoreConnectivity.d.ts +1 -0
  44. package/lib-esm/sync/datastoreConnectivity.js +45 -0
  45. package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
  46. package/lib-esm/sync/index.d.ts +9 -1
  47. package/lib-esm/sync/index.js +519 -396
  48. package/lib-esm/sync/index.js.map +1 -1
  49. package/lib-esm/sync/merger.d.ts +6 -0
  50. package/lib-esm/sync/merger.js +6 -0
  51. package/lib-esm/sync/merger.js.map +1 -1
  52. package/lib-esm/sync/outbox.js +66 -62
  53. package/lib-esm/sync/outbox.js.map +1 -1
  54. package/lib-esm/sync/processors/mutation.d.ts +2 -0
  55. package/lib-esm/sync/processors/mutation.js +208 -166
  56. package/lib-esm/sync/processors/mutation.js.map +1 -1
  57. package/lib-esm/sync/processors/subscription.d.ts +2 -0
  58. package/lib-esm/sync/processors/subscription.js +211 -176
  59. package/lib-esm/sync/processors/subscription.js.map +1 -1
  60. package/lib-esm/sync/processors/sync.d.ts +2 -0
  61. package/lib-esm/sync/processors/sync.js +96 -73
  62. package/lib-esm/sync/processors/sync.js.map +1 -1
  63. package/lib-esm/sync/utils.js +1 -3
  64. package/lib-esm/sync/utils.js.map +1 -1
  65. package/lib-esm/util.d.ts +11 -0
  66. package/lib-esm/util.js +89 -0
  67. package/lib-esm/util.js.map +1 -1
  68. package/package.json +7 -7
  69. package/src/authModeStrategies/multiAuthStrategy.ts +11 -0
  70. package/src/datastore/datastore.ts +572 -366
  71. package/src/storage/adapter/IndexedDBAdapter.ts +50 -9
  72. package/src/storage/storage.ts +2 -2
  73. package/src/sync/datastoreConnectivity.ts +6 -0
  74. package/src/sync/index.ts +492 -400
  75. package/src/sync/merger.ts +6 -0
  76. package/src/sync/outbox.ts +1 -1
  77. package/src/sync/processors/mutation.ts +139 -104
  78. package/src/sync/processors/subscription.ts +287 -250
  79. package/src/sync/processors/sync.ts +88 -60
  80. package/src/sync/utils.ts +1 -3
  81. package/src/util.ts +92 -2
  82. package/lib/authModeStrategies/defaultAuthStrategy.d.ts +0 -2
  83. package/lib/authModeStrategies/index.d.ts +0 -2
  84. package/lib/authModeStrategies/multiAuthStrategy.d.ts +0 -2
  85. package/lib/datastore/datastore.d.ts +0 -63
  86. package/lib/index.d.ts +0 -15
  87. package/lib/predicates/index.d.ts +0 -16
  88. package/lib/predicates/sort.d.ts +0 -8
  89. package/lib/ssr/index.d.ts +0 -3
  90. package/lib/storage/adapter/AsyncStorageAdapter.d.ts +0 -41
  91. package/lib/storage/adapter/AsyncStorageDatabase.d.ts +0 -39
  92. package/lib/storage/adapter/InMemoryStore.d.ts +0 -11
  93. package/lib/storage/adapter/InMemoryStore.native.d.ts +0 -1
  94. package/lib/storage/adapter/IndexedDBAdapter.d.ts +0 -38
  95. package/lib/storage/adapter/getDefaultAdapter/index.d.ts +0 -3
  96. package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +0 -3
  97. package/lib/storage/adapter/index.d.ts +0 -9
  98. package/lib/storage/storage.d.ts +0 -49
  99. package/lib/sync/datastoreConnectivity.d.ts +0 -15
  100. package/lib/sync/datastoreReachability/index.d.ts +0 -3
  101. package/lib/sync/datastoreReachability/index.native.d.ts +0 -3
  102. package/lib/sync/index.d.ts +0 -81
  103. package/lib/sync/merger.d.ts +0 -11
  104. package/lib/sync/outbox.d.ts +0 -27
  105. package/lib/sync/processors/errorMaps.d.ts +0 -17
  106. package/lib/sync/processors/mutation.d.ts +0 -56
  107. package/lib/sync/processors/subscription.d.ts +0 -31
  108. package/lib/sync/processors/sync.d.ts +0 -26
  109. package/lib/sync/utils.d.ts +0 -42
  110. package/lib/types.d.ts +0 -501
  111. package/lib/util.d.ts +0 -145
package/lib/sync/index.js CHANGED
@@ -100,7 +100,7 @@ var ControlMessage;
100
100
  ControlMessage["SYNC_ENGINE_READY"] = "ready";
101
101
  })(ControlMessage = exports.ControlMessage || (exports.ControlMessage = {}));
102
102
  var SyncEngine = /** @class */ (function () {
103
- function SyncEngine(schema, namespaceResolver, modelClasses, userModelClasses, storage, modelInstanceCreator, conflictHandler, errorHandler, syncPredicates, amplifyConfig, authModeStrategy, amplifyContext) {
103
+ function SyncEngine(schema, namespaceResolver, modelClasses, userModelClasses, storage, modelInstanceCreator, conflictHandler, errorHandler, syncPredicates, amplifyConfig, authModeStrategy, amplifyContext, connectivityMonitor) {
104
104
  if (amplifyConfig === void 0) { amplifyConfig = {}; }
105
105
  this.schema = schema;
106
106
  this.namespaceResolver = namespaceResolver;
@@ -112,15 +112,18 @@ var SyncEngine = /** @class */ (function () {
112
112
  this.amplifyConfig = amplifyConfig;
113
113
  this.authModeStrategy = authModeStrategy;
114
114
  this.amplifyContext = amplifyContext;
115
+ this.connectivityMonitor = connectivityMonitor;
115
116
  this.online = false;
116
117
  this.modelSyncedStatus = new WeakMap();
118
+ this.runningProcesses = new core_1.BackgroundProcessManager();
117
119
  var MutationEvent = this.modelClasses['MutationEvent'];
118
120
  this.outbox = new outbox_1.MutationEventOutbox(this.schema, MutationEvent, modelInstanceCreator, ownSymbol);
119
121
  this.modelMerger = new merger_1.ModelMerger(this.outbox, ownSymbol);
120
122
  this.syncQueriesProcessor = new sync_1.SyncProcessor(this.schema, this.syncPredicates, this.amplifyConfig, this.authModeStrategy, errorHandler, this.amplifyContext);
121
123
  this.subscriptionsProcessor = new subscription_1.SubscriptionProcessor(this.schema, this.syncPredicates, this.amplifyConfig, this.authModeStrategy, errorHandler, this.amplifyContext);
122
124
  this.mutationsProcessor = new mutation_1.MutationProcessor(this.schema, this.storage, this.userModelClasses, this.outbox, this.modelInstanceCreator, MutationEvent, this.amplifyConfig, this.authModeStrategy, errorHandler, conflictHandler, this.amplifyContext);
123
- this.datastoreConnectivity = new datastoreConnectivity_1.default();
125
+ this.datastoreConnectivity =
126
+ this.connectivityMonitor || new datastoreConnectivity_1.default();
124
127
  }
125
128
  SyncEngine.prototype.getModelSyncedStatus = function (modelConstructor) {
126
129
  return this.modelSyncedStatus.get(modelConstructor);
@@ -130,7 +133,7 @@ var SyncEngine = /** @class */ (function () {
130
133
  return new zen_observable_ts_1.default(function (observer) {
131
134
  logger.log('starting sync engine...');
132
135
  var subscriptions = [];
133
- (function () { return __awaiter(_this, void 0, void 0, function () {
136
+ _this.runningProcesses.add(function () { return __awaiter(_this, void 0, void 0, function () {
134
137
  var err_1, startPromise, hasMutationsInOutbox;
135
138
  var _this = this;
136
139
  return __generator(this, function (_a) {
@@ -146,154 +149,188 @@ var SyncEngine = /** @class */ (function () {
146
149
  observer.error(err_1);
147
150
  return [2 /*return*/];
148
151
  case 3:
149
- startPromise = new Promise(function (resolve) {
152
+ startPromise = new Promise(function (doneStarting, failedStarting) {
150
153
  _this.datastoreConnectivity.status().subscribe(function (_a) {
151
154
  var online = _a.online;
152
155
  return __awaiter(_this, void 0, void 0, function () {
153
- var ctlSubsObservable_1, dataSubsObservable, err_2, error_1;
154
- var _b;
155
156
  var _this = this;
156
- return __generator(this, function (_c) {
157
- switch (_c.label) {
158
- case 0:
159
- if (!(online && !this.online)) return [3 /*break*/, 10];
160
- this.online = online;
161
- observer.next({
162
- type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
163
- data: {
164
- active: this.online,
165
- },
166
- });
167
- dataSubsObservable = void 0;
168
- if (!isNode) return [3 /*break*/, 1];
169
- logger.warn('Realtime disabled when in a server-side environment');
170
- return [3 /*break*/, 6];
171
- case 1:
172
- //#region GraphQL Subscriptions
173
- _b = __read(this.subscriptionsProcessor.start(), 2),
174
- // const ctlObservable: Observable<CONTROL_MSG>
175
- ctlSubsObservable_1 = _b[0],
176
- // const dataObservable: Observable<[TransformerMutationType, SchemaModel, Readonly<{
177
- // id: string;
178
- // } & Record<string, any>>]>
179
- dataSubsObservable = _b[1];
180
- _c.label = 2;
181
- case 2:
182
- _c.trys.push([2, 4, , 5]);
183
- return [4 /*yield*/, new Promise(function (resolve, reject) {
184
- var ctlSubsSubscription = ctlSubsObservable_1.subscribe({
185
- next: function (msg) {
186
- if (msg === subscription_1.CONTROL_MSG.CONNECTED) {
187
- resolve();
157
+ return __generator(this, function (_b) {
158
+ return [2 /*return*/, this.runningProcesses.isOpen &&
159
+ this.runningProcesses.add(function (onTerminate) { return __awaiter(_this, void 0, void 0, function () {
160
+ var ctlSubsObservable_1, dataSubsObservable, err_2, error_1;
161
+ var _a;
162
+ var _this = this;
163
+ return __generator(this, function (_b) {
164
+ switch (_b.label) {
165
+ case 0:
166
+ if (!(online && !this.online)) return [3 /*break*/, 10];
167
+ this.online = online;
168
+ observer.next({
169
+ type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
170
+ data: {
171
+ active: this.online,
172
+ },
173
+ });
174
+ dataSubsObservable = void 0;
175
+ if (!isNode) return [3 /*break*/, 1];
176
+ logger.warn('Realtime disabled when in a server-side environment');
177
+ return [3 /*break*/, 6];
178
+ case 1:
179
+ //#region GraphQL Subscriptions
180
+ _a = __read(this.subscriptionsProcessor.start(), 2),
181
+ // const ctlObservable: Observable<CONTROL_MSG>
182
+ ctlSubsObservable_1 = _a[0],
183
+ // const dataObservable: Observable<[TransformerMutationType, SchemaModel, Readonly<{
184
+ // id: string;
185
+ // } & Record<string, any>>]>
186
+ dataSubsObservable = _a[1];
187
+ _b.label = 2;
188
+ case 2:
189
+ _b.trys.push([2, 4, , 5]);
190
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
191
+ onTerminate.then(reject);
192
+ var ctlSubsSubscription = ctlSubsObservable_1.subscribe({
193
+ next: function (msg) {
194
+ if (msg === subscription_1.CONTROL_MSG.CONNECTED) {
195
+ resolve();
196
+ }
197
+ },
198
+ error: function (err) {
199
+ reject(err);
200
+ var handleDisconnect = _this.disconnectionHandler();
201
+ handleDisconnect(err);
202
+ },
203
+ });
204
+ subscriptions.push(ctlSubsSubscription);
205
+ })];
206
+ case 3:
207
+ _b.sent();
208
+ return [3 /*break*/, 5];
209
+ case 4:
210
+ err_2 = _b.sent();
211
+ observer.error(err_2);
212
+ failedStarting();
213
+ return [2 /*return*/];
214
+ case 5:
215
+ logger.log('Realtime ready');
216
+ observer.next({
217
+ type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
218
+ });
219
+ _b.label = 6;
220
+ case 6:
221
+ _b.trys.push([6, 8, , 9]);
222
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
223
+ var syncQuerySubscription = _this.syncQueriesObservable().subscribe({
224
+ next: function (message) {
225
+ var type = message.type;
226
+ if (type ===
227
+ ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY) {
228
+ resolve();
229
+ }
230
+ observer.next(message);
231
+ },
232
+ complete: function () {
233
+ resolve();
234
+ },
235
+ error: function (error) {
236
+ reject(error);
237
+ },
238
+ });
239
+ if (syncQuerySubscription) {
240
+ subscriptions.push(syncQuerySubscription);
241
+ }
242
+ })];
243
+ case 7:
244
+ _b.sent();
245
+ return [3 /*break*/, 9];
246
+ case 8:
247
+ error_1 = _b.sent();
248
+ observer.error(error_1);
249
+ failedStarting();
250
+ return [2 /*return*/];
251
+ case 9:
252
+ //#endregion
253
+ //#region process mutations (outbox)
254
+ subscriptions.push(this.mutationsProcessor
255
+ .start()
256
+ .subscribe(function (_a) {
257
+ var modelDefinition = _a.modelDefinition, item = _a.model, hasMore = _a.hasMore;
258
+ return _this.runningProcesses.add(function () { return __awaiter(_this, void 0, void 0, function () {
259
+ var modelConstructor, model;
260
+ var _this = this;
261
+ return __generator(this, function (_a) {
262
+ switch (_a.label) {
263
+ case 0:
264
+ modelConstructor = this.userModelClasses[modelDefinition.name];
265
+ model = this.modelInstanceCreator(modelConstructor, item);
266
+ return [4 /*yield*/, this.storage.runExclusive(function (storage) {
267
+ return _this.modelMerger.merge(storage, model, modelDefinition);
268
+ })];
269
+ case 1:
270
+ _a.sent();
271
+ observer.next({
272
+ type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
273
+ data: {
274
+ model: modelConstructor,
275
+ element: model,
276
+ },
277
+ });
278
+ observer.next({
279
+ type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
280
+ data: {
281
+ isEmpty: !hasMore,
282
+ },
283
+ });
284
+ return [2 /*return*/];
285
+ }
286
+ });
287
+ }); }, 'mutation processor event');
288
+ }));
289
+ //#endregion
290
+ //#region Merge subscriptions buffer
291
+ // TODO: extract to function
292
+ if (!isNode) {
293
+ subscriptions.push(dataSubsObservable.subscribe(function (_a) {
294
+ var _b = __read(_a, 3), _transformerMutationType = _b[0], modelDefinition = _b[1], item = _b[2];
295
+ return _this.runningProcesses.add(function () { return __awaiter(_this, void 0, void 0, function () {
296
+ var modelConstructor, model;
297
+ var _this = this;
298
+ return __generator(this, function (_a) {
299
+ switch (_a.label) {
300
+ case 0:
301
+ modelConstructor = this.userModelClasses[modelDefinition.name];
302
+ model = this.modelInstanceCreator(modelConstructor, item);
303
+ return [4 /*yield*/, this.storage.runExclusive(function (storage) {
304
+ return _this.modelMerger.merge(storage, model, modelDefinition);
305
+ })];
306
+ case 1:
307
+ _a.sent();
308
+ return [2 /*return*/];
309
+ }
310
+ });
311
+ }); }, 'subscription dataSubsObservable event');
312
+ }));
188
313
  }
189
- },
190
- error: function (err) {
191
- reject(err);
192
- var handleDisconnect = _this.disconnectionHandler();
193
- handleDisconnect(err);
194
- },
195
- });
196
- subscriptions.push(ctlSubsSubscription);
197
- })];
198
- case 3:
199
- _c.sent();
200
- return [3 /*break*/, 5];
201
- case 4:
202
- err_2 = _c.sent();
203
- observer.error(err_2);
204
- return [2 /*return*/];
205
- case 5:
206
- logger.log('Realtime ready');
207
- observer.next({
208
- type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
209
- });
210
- _c.label = 6;
211
- case 6:
212
- _c.trys.push([6, 8, , 9]);
213
- return [4 /*yield*/, new Promise(function (resolve, reject) {
214
- var syncQuerySubscription = _this.syncQueriesObservable().subscribe({
215
- next: function (message) {
216
- var type = message.type;
217
- if (type === ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY) {
218
- resolve();
314
+ return [3 /*break*/, 11];
315
+ case 10:
316
+ if (!online) {
317
+ this.online = online;
318
+ observer.next({
319
+ type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
320
+ data: {
321
+ active: this.online,
322
+ },
323
+ });
324
+ subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
325
+ subscriptions = [];
219
326
  }
220
- observer.next(message);
221
- },
222
- complete: function () {
223
- resolve();
224
- },
225
- error: function (error) {
226
- reject(error);
227
- },
228
- });
229
- if (syncQuerySubscription) {
230
- subscriptions.push(syncQuerySubscription);
327
+ _b.label = 11;
328
+ case 11:
329
+ doneStarting();
330
+ return [2 /*return*/];
231
331
  }
232
- })];
233
- case 7:
234
- _c.sent();
235
- return [3 /*break*/, 9];
236
- case 8:
237
- error_1 = _c.sent();
238
- observer.error(error_1);
239
- return [2 /*return*/];
240
- case 9:
241
- //#endregion
242
- //#region process mutations
243
- subscriptions.push(this.mutationsProcessor
244
- .start()
245
- .subscribe(function (_a) {
246
- var modelDefinition = _a.modelDefinition, item = _a.model, hasMore = _a.hasMore;
247
- var modelConstructor = _this.userModelClasses[modelDefinition.name];
248
- var model = _this.modelInstanceCreator(modelConstructor, item);
249
- _this.storage.runExclusive(function (storage) {
250
- return _this.modelMerger.merge(storage, model, modelDefinition);
251
- });
252
- observer.next({
253
- type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
254
- data: {
255
- model: modelConstructor,
256
- element: model,
257
- },
258
- });
259
- observer.next({
260
- type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
261
- data: {
262
- isEmpty: !hasMore,
263
- },
264
332
  });
265
- }));
266
- //#endregion
267
- //#region Merge subscriptions buffer
268
- // TODO: extract to function
269
- if (!isNode) {
270
- subscriptions.push(dataSubsObservable.subscribe(function (_a) {
271
- var _b = __read(_a, 3), _transformerMutationType = _b[0], modelDefinition = _b[1], item = _b[2];
272
- var modelConstructor = _this.userModelClasses[modelDefinition.name];
273
- var model = _this.modelInstanceCreator(modelConstructor, item);
274
- _this.storage.runExclusive(function (storage) {
275
- return _this.modelMerger.merge(storage, model, modelDefinition);
276
- });
277
- }));
278
- }
279
- return [3 /*break*/, 11];
280
- case 10:
281
- if (!online) {
282
- this.online = online;
283
- observer.next({
284
- type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
285
- data: {
286
- active: this.online,
287
- },
288
- });
289
- subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
290
- subscriptions = [];
291
- }
292
- _c.label = 11;
293
- case 11:
294
- resolve();
295
- return [2 /*return*/];
296
- }
333
+ }); }, 'datastore connectivity event')];
297
334
  });
298
335
  });
299
336
  });
@@ -309,39 +346,44 @@ var SyncEngine = /** @class */ (function () {
309
346
  next: function (_a) {
310
347
  var opType = _a.opType, model = _a.model, element = _a.element, condition = _a.condition;
311
348
  return __awaiter(_this, void 0, void 0, function () {
312
- var namespace, MutationEventConstructor, modelDefinition, graphQLCondition, mutationEvent;
349
+ var _this = this;
313
350
  return __generator(this, function (_b) {
314
- switch (_b.label) {
315
- case 0:
316
- namespace = this.schema.namespaces[this.namespaceResolver(model)];
317
- MutationEventConstructor = this.modelClasses['MutationEvent'];
318
- modelDefinition = this.getModelDefinition(model);
319
- graphQLCondition = utils_1.predicateToGraphQLCondition(condition, modelDefinition);
320
- mutationEvent = utils_1.createMutationInstanceFromModelOperation(namespace.relationships, this.getModelDefinition(model), opType, model, element, graphQLCondition, MutationEventConstructor, this.modelInstanceCreator);
321
- return [4 /*yield*/, this.outbox.enqueue(this.storage, mutationEvent)];
322
- case 1:
323
- _b.sent();
324
- observer.next({
325
- type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_ENQUEUED,
326
- data: {
327
- model: model,
328
- element: element,
329
- },
330
- });
331
- observer.next({
332
- type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
333
- data: {
334
- isEmpty: false,
335
- },
351
+ return [2 /*return*/, this.runningProcesses.add(function () { return __awaiter(_this, void 0, void 0, function () {
352
+ var namespace, MutationEventConstructor, modelDefinition, graphQLCondition, mutationEvent;
353
+ return __generator(this, function (_a) {
354
+ switch (_a.label) {
355
+ case 0:
356
+ namespace = this.schema.namespaces[this.namespaceResolver(model)];
357
+ MutationEventConstructor = this.modelClasses['MutationEvent'];
358
+ modelDefinition = this.getModelDefinition(model);
359
+ graphQLCondition = utils_1.predicateToGraphQLCondition(condition, modelDefinition);
360
+ mutationEvent = utils_1.createMutationInstanceFromModelOperation(namespace.relationships, this.getModelDefinition(model), opType, model, element, graphQLCondition, MutationEventConstructor, this.modelInstanceCreator);
361
+ return [4 /*yield*/, this.outbox.enqueue(this.storage, mutationEvent)];
362
+ case 1:
363
+ _a.sent();
364
+ observer.next({
365
+ type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_ENQUEUED,
366
+ data: {
367
+ model: model,
368
+ element: element,
369
+ },
370
+ });
371
+ observer.next({
372
+ type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
373
+ data: {
374
+ isEmpty: false,
375
+ },
376
+ });
377
+ return [4 /*yield*/, startPromise];
378
+ case 2:
379
+ _a.sent();
380
+ if (this.online) {
381
+ this.mutationsProcessor.resume();
382
+ }
383
+ return [2 /*return*/];
384
+ }
336
385
  });
337
- return [4 /*yield*/, startPromise];
338
- case 2:
339
- _b.sent();
340
- if (this.online) {
341
- this.mutationsProcessor.resume();
342
- }
343
- return [2 /*return*/];
344
- }
386
+ }); }, 'storage event')];
345
387
  });
346
388
  });
347
389
  },
@@ -367,10 +409,7 @@ var SyncEngine = /** @class */ (function () {
367
409
  return [2 /*return*/];
368
410
  }
369
411
  });
370
- }); })();
371
- return function () {
372
- subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
373
- };
412
+ }); }, 'sync start');
374
413
  });
375
414
  };
376
415
  SyncEngine.prototype.getModelsMetadataWithNextFullSync = function (currentTimeStamp) {
@@ -381,7 +420,7 @@ var SyncEngine = /** @class */ (function () {
381
420
  switch (_b.label) {
382
421
  case 0:
383
422
  _a = Map.bind;
384
- return [4 /*yield*/, this.getModelsMetadata()];
423
+ return [4 /*yield*/, this.runningProcesses.add(function () { return _this.getModelsMetadata(); }, 'sync/index getModelsMetadataWithNextFullSync')];
385
424
  case 1:
386
425
  modelLastSync = new (_a.apply(Map, [void 0, (_b.sent()).map(function (_a) {
387
426
  var namespace = _a.namespace, model = _a.model, lastSync = _a.lastSync, lastFullSync = _a.lastFullSync, fullSyncInterval = _a.fullSyncInterval, lastSyncPredicate = _a.lastSyncPredicate;
@@ -406,228 +445,261 @@ var SyncEngine = /** @class */ (function () {
406
445
  }
407
446
  return new zen_observable_ts_1.default(function (observer) {
408
447
  var syncQueriesSubscription;
409
- var waitTimeoutId;
410
- (function () { return __awaiter(_this, void 0, void 0, function () {
411
- var _loop_1, this_1;
412
- var _this = this;
413
- return __generator(this, function (_a) {
414
- switch (_a.label) {
415
- case 0:
416
- _loop_1 = function () {
417
- var count, modelLastSync, paginatingModels, newestFullSyncStartedAt, theInterval, start, duration, newestStartedAt, msNextFullSync;
418
- return __generator(this, function (_a) {
419
- switch (_a.label) {
420
- case 0:
421
- count = new WeakMap();
422
- return [4 /*yield*/, this_1.getModelsMetadataWithNextFullSync(Date.now())];
423
- case 1:
424
- modelLastSync = _a.sent();
425
- paginatingModels = new Set(modelLastSync.keys());
426
- return [4 /*yield*/, new Promise(function (resolve) {
427
- syncQueriesSubscription = _this.syncQueriesProcessor
428
- .start(modelLastSync)
429
- .subscribe({
430
- next: function (_a) {
431
- var namespace = _a.namespace, modelDefinition = _a.modelDefinition, items = _a.items, done = _a.done, startedAt = _a.startedAt, isFullSync = _a.isFullSync;
432
- return __awaiter(_this, void 0, void 0, function () {
433
- var modelConstructor, modelName, modelMetadata_1, lastFullSync, fullSyncInterval, counts;
434
- var _this = this;
435
- return __generator(this, function (_b) {
436
- switch (_b.label) {
437
- case 0:
438
- modelConstructor = this.userModelClasses[modelDefinition.name];
439
- if (!count.has(modelConstructor)) {
440
- count.set(modelConstructor, {
441
- new: 0,
442
- updated: 0,
443
- deleted: 0,
444
- });
445
- start = util_1.getNow();
446
- newestStartedAt =
447
- newestStartedAt === undefined
448
- ? startedAt
449
- : Math.max(newestStartedAt, startedAt);
450
- }
451
- /**
452
- * If there are mutations in the outbox for a given id, those need to be
453
- * merged individually. Otherwise, we can merge them in batches.
454
- */
455
- return [4 /*yield*/, this.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
456
- var idsInOutbox, oneByOne, page, opTypeCount, oneByOne_1, oneByOne_1_1, item, opType, e_1_1, _a, _b, _c, counts;
457
- var e_1, _d;
458
- return __generator(this, function (_e) {
459
- switch (_e.label) {
460
- case 0: return [4 /*yield*/, this.outbox.getModelIds(storage)];
461
- case 1:
462
- idsInOutbox = _e.sent();
463
- oneByOne = [];
464
- page = items.filter(function (item) {
465
- var itemId = utils_1.getIdentifierValue(modelDefinition, item);
466
- if (!idsInOutbox.has(itemId)) {
467
- return true;
448
+ _this.runningProcesses.isOpen &&
449
+ _this.runningProcesses.add(function (onTerminate) { return __awaiter(_this, void 0, void 0, function () {
450
+ var terminated, _loop_1, this_1;
451
+ var _this = this;
452
+ return __generator(this, function (_a) {
453
+ switch (_a.label) {
454
+ case 0:
455
+ terminated = false;
456
+ _loop_1 = function () {
457
+ var count, modelLastSync, paginatingModels, newestFullSyncStartedAt, theInterval, start, duration, newestStartedAt, msNextFullSync;
458
+ return __generator(this, function (_a) {
459
+ switch (_a.label) {
460
+ case 0:
461
+ count = new WeakMap();
462
+ return [4 /*yield*/, this_1.getModelsMetadataWithNextFullSync(Date.now())];
463
+ case 1:
464
+ modelLastSync = _a.sent();
465
+ paginatingModels = new Set(modelLastSync.keys());
466
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
467
+ if (!_this.runningProcesses.isOpen)
468
+ resolve();
469
+ onTerminate.then(function () { return resolve(); });
470
+ syncQueriesSubscription = _this.syncQueriesProcessor
471
+ .start(modelLastSync)
472
+ .subscribe({
473
+ next: function (_a) {
474
+ var namespace = _a.namespace, modelDefinition = _a.modelDefinition, items = _a.items, done = _a.done, startedAt = _a.startedAt, isFullSync = _a.isFullSync;
475
+ return __awaiter(_this, void 0, void 0, function () {
476
+ var modelConstructor, modelName, modelMetadata_1, lastFullSync, fullSyncInterval, counts;
477
+ var _this = this;
478
+ return __generator(this, function (_b) {
479
+ switch (_b.label) {
480
+ case 0:
481
+ modelConstructor = this.userModelClasses[modelDefinition.name];
482
+ if (!count.has(modelConstructor)) {
483
+ count.set(modelConstructor, {
484
+ new: 0,
485
+ updated: 0,
486
+ deleted: 0,
487
+ });
488
+ start = util_1.getNow();
489
+ newestStartedAt =
490
+ newestStartedAt === undefined
491
+ ? startedAt
492
+ : Math.max(newestStartedAt, startedAt);
493
+ }
494
+ /**
495
+ * If there are mutations in the outbox for a given id, those need to be
496
+ * merged individually. Otherwise, we can merge them in batches.
497
+ */
498
+ return [4 /*yield*/, this.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
499
+ var idsInOutbox, oneByOne, page, opTypeCount, oneByOne_1, oneByOne_1_1, item, opType, e_1_1, _a, _b, _c, counts;
500
+ var e_1, _d;
501
+ return __generator(this, function (_e) {
502
+ switch (_e.label) {
503
+ case 0: return [4 /*yield*/, this.outbox.getModelIds(storage)];
504
+ case 1:
505
+ idsInOutbox = _e.sent();
506
+ oneByOne = [];
507
+ page = items.filter(function (item) {
508
+ var itemId = utils_1.getIdentifierValue(modelDefinition, item);
509
+ if (!idsInOutbox.has(itemId)) {
510
+ return true;
511
+ }
512
+ oneByOne.push(item);
513
+ return false;
514
+ });
515
+ opTypeCount = [];
516
+ _e.label = 2;
517
+ case 2:
518
+ _e.trys.push([2, 7, 8, 9]);
519
+ oneByOne_1 = __values(oneByOne), oneByOne_1_1 = oneByOne_1.next();
520
+ _e.label = 3;
521
+ case 3:
522
+ if (!!oneByOne_1_1.done) return [3 /*break*/, 6];
523
+ item = oneByOne_1_1.value;
524
+ return [4 /*yield*/, this.modelMerger.merge(storage, item, modelDefinition)];
525
+ case 4:
526
+ opType = _e.sent();
527
+ if (opType !== undefined) {
528
+ opTypeCount.push([item, opType]);
468
529
  }
469
- oneByOne.push(item);
470
- return false;
471
- });
472
- opTypeCount = [];
473
- _e.label = 2;
474
- case 2:
475
- _e.trys.push([2, 7, 8, 9]);
476
- oneByOne_1 = __values(oneByOne), oneByOne_1_1 = oneByOne_1.next();
477
- _e.label = 3;
478
- case 3:
479
- if (!!oneByOne_1_1.done) return [3 /*break*/, 6];
480
- item = oneByOne_1_1.value;
481
- return [4 /*yield*/, this.modelMerger.merge(storage, item, modelDefinition)];
482
- case 4:
483
- opType = _e.sent();
484
- if (opType !== undefined) {
485
- opTypeCount.push([item, opType]);
486
- }
487
- _e.label = 5;
488
- case 5:
489
- oneByOne_1_1 = oneByOne_1.next();
490
- return [3 /*break*/, 3];
491
- case 6: return [3 /*break*/, 9];
492
- case 7:
493
- e_1_1 = _e.sent();
494
- e_1 = { error: e_1_1 };
495
- return [3 /*break*/, 9];
496
- case 8:
497
- try {
498
- if (oneByOne_1_1 && !oneByOne_1_1.done && (_d = oneByOne_1.return)) _d.call(oneByOne_1);
499
- }
500
- finally { if (e_1) throw e_1.error; }
501
- return [7 /*endfinally*/];
502
- case 9:
503
- _b = (_a = opTypeCount.push).apply;
504
- _c = [opTypeCount];
505
- return [4 /*yield*/, this.modelMerger.mergePage(storage, modelConstructor, page, modelDefinition)];
506
- case 10:
507
- _b.apply(_a, _c.concat([__spread.apply(void 0, [(_e.sent())])]));
508
- counts = count.get(modelConstructor);
509
- opTypeCount.forEach(function (_a) {
510
- var _b = __read(_a, 2), opType = _b[1];
511
- switch (opType) {
512
- case types_1.OpType.INSERT:
513
- counts.new++;
514
- break;
515
- case types_1.OpType.UPDATE:
516
- counts.updated++;
517
- break;
518
- case types_1.OpType.DELETE:
519
- counts.deleted++;
520
- break;
521
- default:
522
- util_1.exhaustiveCheck(opType);
530
+ _e.label = 5;
531
+ case 5:
532
+ oneByOne_1_1 = oneByOne_1.next();
533
+ return [3 /*break*/, 3];
534
+ case 6: return [3 /*break*/, 9];
535
+ case 7:
536
+ e_1_1 = _e.sent();
537
+ e_1 = { error: e_1_1 };
538
+ return [3 /*break*/, 9];
539
+ case 8:
540
+ try {
541
+ if (oneByOne_1_1 && !oneByOne_1_1.done && (_d = oneByOne_1.return)) _d.call(oneByOne_1);
523
542
  }
524
- });
525
- return [2 /*return*/];
526
- }
527
- });
528
- }); })];
529
- case 1:
530
- /**
531
- * If there are mutations in the outbox for a given id, those need to be
532
- * merged individually. Otherwise, we can merge them in batches.
533
- */
534
- _b.sent();
535
- if (!done) return [3 /*break*/, 4];
536
- modelName = modelDefinition.name;
537
- return [4 /*yield*/, this.getModelMetadata(namespace, modelName)];
538
- case 2:
539
- modelMetadata_1 = _b.sent();
540
- lastFullSync = modelMetadata_1.lastFullSync, fullSyncInterval = modelMetadata_1.fullSyncInterval;
541
- theInterval = fullSyncInterval;
542
- newestFullSyncStartedAt =
543
- newestFullSyncStartedAt === undefined
544
- ? lastFullSync
545
- : Math.max(newestFullSyncStartedAt, isFullSync ? startedAt : lastFullSync);
546
- modelMetadata_1 = this.modelClasses
547
- .ModelMetadata.copyOf(modelMetadata_1, function (draft) {
548
- draft.lastSync = startedAt;
549
- draft.lastFullSync = isFullSync
550
- ? startedAt
551
- : modelMetadata_1.lastFullSync;
552
- });
553
- return [4 /*yield*/, this.storage.save(modelMetadata_1, undefined, ownSymbol)];
554
- case 3:
555
- _b.sent();
556
- counts = count.get(modelConstructor);
557
- this.modelSyncedStatus.set(modelConstructor, true);
558
- observer.next({
559
- type: ControlMessage.SYNC_ENGINE_MODEL_SYNCED,
560
- data: {
561
- model: modelConstructor,
562
- isFullSync: isFullSync,
563
- isDeltaSync: !isFullSync,
564
- counts: counts,
565
- },
566
- });
567
- paginatingModels.delete(modelDefinition);
568
- if (paginatingModels.size === 0) {
569
- duration = util_1.getNow() - start;
570
- resolve();
543
+ finally { if (e_1) throw e_1.error; }
544
+ return [7 /*endfinally*/];
545
+ case 9:
546
+ _b = (_a = opTypeCount.push).apply;
547
+ _c = [opTypeCount];
548
+ return [4 /*yield*/, this.modelMerger.mergePage(storage, modelConstructor, page, modelDefinition)];
549
+ case 10:
550
+ _b.apply(_a, _c.concat([__spread.apply(void 0, [(_e.sent())])]));
551
+ counts = count.get(modelConstructor);
552
+ opTypeCount.forEach(function (_a) {
553
+ var _b = __read(_a, 2), opType = _b[1];
554
+ switch (opType) {
555
+ case types_1.OpType.INSERT:
556
+ counts.new++;
557
+ break;
558
+ case types_1.OpType.UPDATE:
559
+ counts.updated++;
560
+ break;
561
+ case types_1.OpType.DELETE:
562
+ counts.deleted++;
563
+ break;
564
+ default:
565
+ util_1.exhaustiveCheck(opType);
566
+ }
567
+ });
568
+ return [2 /*return*/];
569
+ }
570
+ });
571
+ }); })];
572
+ case 1:
573
+ /**
574
+ * If there are mutations in the outbox for a given id, those need to be
575
+ * merged individually. Otherwise, we can merge them in batches.
576
+ */
577
+ _b.sent();
578
+ if (!done) return [3 /*break*/, 4];
579
+ modelName = modelDefinition.name;
580
+ return [4 /*yield*/, this.getModelMetadata(namespace, modelName)];
581
+ case 2:
582
+ modelMetadata_1 = _b.sent();
583
+ lastFullSync = modelMetadata_1.lastFullSync, fullSyncInterval = modelMetadata_1.fullSyncInterval;
584
+ theInterval = fullSyncInterval;
585
+ newestFullSyncStartedAt =
586
+ newestFullSyncStartedAt === undefined
587
+ ? lastFullSync
588
+ : Math.max(newestFullSyncStartedAt, isFullSync ? startedAt : lastFullSync);
589
+ modelMetadata_1 = this.modelClasses
590
+ .ModelMetadata.copyOf(modelMetadata_1, function (draft) {
591
+ draft.lastSync = startedAt;
592
+ draft.lastFullSync = isFullSync
593
+ ? startedAt
594
+ : modelMetadata_1.lastFullSync;
595
+ });
596
+ return [4 /*yield*/, this.storage.save(modelMetadata_1, undefined, ownSymbol)];
597
+ case 3:
598
+ _b.sent();
599
+ counts = count.get(modelConstructor);
600
+ this.modelSyncedStatus.set(modelConstructor, true);
571
601
  observer.next({
572
- type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY,
602
+ type: ControlMessage.SYNC_ENGINE_MODEL_SYNCED,
603
+ data: {
604
+ model: modelConstructor,
605
+ isFullSync: isFullSync,
606
+ isDeltaSync: !isFullSync,
607
+ counts: counts,
608
+ },
573
609
  });
574
- syncQueriesSubscription.unsubscribe();
575
- }
576
- _b.label = 4;
577
- case 4: return [2 /*return*/];
578
- }
610
+ paginatingModels.delete(modelDefinition);
611
+ if (paginatingModels.size === 0) {
612
+ duration = util_1.getNow() - start;
613
+ resolve();
614
+ observer.next({
615
+ type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY,
616
+ });
617
+ syncQueriesSubscription.unsubscribe();
618
+ }
619
+ _b.label = 4;
620
+ case 4: return [2 /*return*/];
621
+ }
622
+ });
579
623
  });
624
+ },
625
+ error: function (error) {
626
+ observer.error(error);
627
+ },
628
+ });
629
+ observer.next({
630
+ type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_STARTED,
631
+ data: {
632
+ models: Array.from(paginatingModels).map(function (_a) {
633
+ var name = _a.name;
634
+ return name;
635
+ }),
636
+ },
637
+ });
638
+ })];
639
+ case 2:
640
+ _a.sent();
641
+ msNextFullSync = newestFullSyncStartedAt +
642
+ theInterval -
643
+ (newestStartedAt + duration);
644
+ logger.debug("Next fullSync in " + msNextFullSync / 1000 + " seconds. (" + new Date(Date.now() + msNextFullSync) + ")");
645
+ // TODO: create `BackgroundProcessManager.sleep()` ... but, need to put
646
+ // a lot of thought into what that contract looks like to
647
+ // support possible use-cases:
648
+ //
649
+ // 1. non-cancelable
650
+ // 2. cancelable, unsleep on exit()
651
+ // 3. cancelable, throw Error on exit()
652
+ // 4. cancelable, callback first on exit()?
653
+ // 5. ... etc. ? ...
654
+ //
655
+ // TLDR; this is a lot of complexity here for a sleep(),
656
+ // but, it's not clear to me yet how to support an
657
+ // extensible, centralized cancelable `sleep()` elegantly.
658
+ return [4 /*yield*/, this_1.runningProcesses.add(function (onTerminate) { return __awaiter(_this, void 0, void 0, function () {
659
+ var sleepTimer, unsleep, sleep;
660
+ return __generator(this, function (_a) {
661
+ sleep = new Promise(function (_unsleep) {
662
+ unsleep = _unsleep;
663
+ sleepTimer = setTimeout(unsleep, msNextFullSync);
580
664
  });
581
- },
582
- error: function (error) {
583
- observer.error(error);
584
- },
585
- });
586
- observer.next({
587
- type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_STARTED,
588
- data: {
589
- models: Array.from(paginatingModels).map(function (_a) {
590
- var name = _a.name;
591
- return name;
592
- }),
593
- },
594
- });
595
- })];
596
- case 2:
597
- _a.sent();
598
- msNextFullSync = newestFullSyncStartedAt +
599
- theInterval -
600
- (newestStartedAt + duration);
601
- logger.debug("Next fullSync in " + msNextFullSync / 1000 + " seconds. (" + new Date(Date.now() + msNextFullSync) + ")");
602
- return [4 /*yield*/, new Promise(function (res) {
603
- waitTimeoutId = setTimeout(res, msNextFullSync);
604
- })];
605
- case 3:
606
- _a.sent();
607
- return [2 /*return*/];
608
- }
609
- });
610
- };
611
- this_1 = this;
612
- _a.label = 1;
613
- case 1:
614
- if (!!observer.closed) return [3 /*break*/, 3];
615
- return [5 /*yield**/, _loop_1()];
616
- case 2:
617
- _a.sent();
618
- return [3 /*break*/, 1];
619
- case 3: return [2 /*return*/];
620
- }
621
- });
622
- }); })();
623
- return function () {
624
- if (syncQueriesSubscription) {
625
- syncQueriesSubscription.unsubscribe();
626
- }
627
- if (waitTimeoutId) {
628
- clearTimeout(waitTimeoutId);
629
- }
630
- };
665
+ onTerminate.then(function () {
666
+ terminated = true;
667
+ unsleep();
668
+ });
669
+ return [2 /*return*/, sleep];
670
+ });
671
+ }); }, 'syncQueriesObservable sleep')];
672
+ case 3:
673
+ // TODO: create `BackgroundProcessManager.sleep()` ... but, need to put
674
+ // a lot of thought into what that contract looks like to
675
+ // support possible use-cases:
676
+ //
677
+ // 1. non-cancelable
678
+ // 2. cancelable, unsleep on exit()
679
+ // 3. cancelable, throw Error on exit()
680
+ // 4. cancelable, callback first on exit()?
681
+ // 5. ... etc. ? ...
682
+ //
683
+ // TLDR; this is a lot of complexity here for a sleep(),
684
+ // but, it's not clear to me yet how to support an
685
+ // extensible, centralized cancelable `sleep()` elegantly.
686
+ _a.sent();
687
+ return [2 /*return*/];
688
+ }
689
+ });
690
+ };
691
+ this_1 = this;
692
+ _a.label = 1;
693
+ case 1:
694
+ if (!(!observer.closed && !terminated)) return [3 /*break*/, 3];
695
+ return [5 /*yield**/, _loop_1()];
696
+ case 2:
697
+ _a.sent();
698
+ return [3 /*break*/, 1];
699
+ case 3: return [2 /*return*/];
700
+ }
701
+ });
702
+ }); }, 'syncQueriesObservable main');
631
703
  });
632
704
  };
633
705
  SyncEngine.prototype.disconnectionHandler = function () {
@@ -643,6 +715,57 @@ var SyncEngine = /** @class */ (function () {
643
715
  SyncEngine.prototype.unsubscribeConnectivity = function () {
644
716
  this.datastoreConnectivity.unsubscribe();
645
717
  };
718
+ /**
719
+ * Stops all subscription activities and resolves when all activies report
720
+ * that they're disconnected, done retrying, etc..
721
+ */
722
+ SyncEngine.prototype.stop = function () {
723
+ return __awaiter(this, void 0, void 0, function () {
724
+ return __generator(this, function (_a) {
725
+ switch (_a.label) {
726
+ case 0:
727
+ logger.debug('stopping sync engine');
728
+ /**
729
+ * Gracefully disconnecting subscribers first just prevents *more* work
730
+ * from entering the pipelines.
731
+ */
732
+ this.unsubscribeConnectivity();
733
+ /**
734
+ * aggressively shut down any lingering background processes.
735
+ * some of this might be semi-redundant with unsubscribing. however,
736
+ * unsubscribing doesn't allow us to wait for settling.
737
+ * (Whereas `stop()` does.)
738
+ */
739
+ return [4 /*yield*/, this.mutationsProcessor.stop()];
740
+ case 1:
741
+ /**
742
+ * aggressively shut down any lingering background processes.
743
+ * some of this might be semi-redundant with unsubscribing. however,
744
+ * unsubscribing doesn't allow us to wait for settling.
745
+ * (Whereas `stop()` does.)
746
+ */
747
+ _a.sent();
748
+ return [4 /*yield*/, this.subscriptionsProcessor.stop()];
749
+ case 2:
750
+ _a.sent();
751
+ return [4 /*yield*/, this.datastoreConnectivity.stop()];
752
+ case 3:
753
+ _a.sent();
754
+ return [4 /*yield*/, this.syncQueriesProcessor.stop()];
755
+ case 4:
756
+ _a.sent();
757
+ return [4 /*yield*/, this.runningProcesses.close()];
758
+ case 5:
759
+ _a.sent();
760
+ return [4 /*yield*/, this.runningProcesses.open()];
761
+ case 6:
762
+ _a.sent();
763
+ logger.debug('sync engine stopped and ready to restart');
764
+ return [2 /*return*/];
765
+ }
766
+ });
767
+ });
768
+ };
646
769
  SyncEngine.prototype.setupModels = function (params) {
647
770
  return __awaiter(this, void 0, void 0, function () {
648
771
  var fullSyncInterval, ModelMetadataConstructor, models, savedModel, promises, result, _a, _b, modelMetadata, modelName, e_2_1;