@aws-amplify/datastore 5.0.1-api-v6-models.c1977f8.0 → 5.0.1-api-v6-models.891fe0d.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/lib/authModeStrategies/defaultAuthStrategy.js +1 -1
- package/lib/authModeStrategies/multiAuthStrategy.js +29 -105
- package/lib/datastore/datastore.js +887 -1370
- package/lib/index.js +1 -1
- package/lib/predicates/index.js +53 -102
- package/lib/predicates/next.js +310 -557
- package/lib/predicates/sort.js +24 -27
- package/lib/ssr/index.js +1 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js +115 -449
- package/lib/storage/adapter/AsyncStorageDatabase.js +215 -480
- package/lib/storage/adapter/InMemoryStore.js +27 -101
- package/lib/storage/adapter/InMemoryStore.native.js +1 -1
- package/lib/storage/adapter/IndexedDBAdapter.js +441 -1002
- package/lib/storage/adapter/StorageAdapterBase.js +177 -396
- package/lib/storage/adapter/getDefaultAdapter/index.js +5 -6
- package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -2
- package/lib/storage/relationship.js +174 -260
- package/lib/storage/storage.js +244 -507
- package/lib/sync/datastoreConnectivity.js +29 -84
- package/lib/sync/datastoreReachability/index.js +1 -1
- package/lib/sync/datastoreReachability/index.native.js +2 -2
- package/lib/sync/index.js +512 -885
- package/lib/sync/merger.js +30 -133
- package/lib/sync/outbox.js +147 -302
- package/lib/sync/processors/errorMaps.js +30 -80
- package/lib/sync/processors/mutation.js +331 -579
- package/lib/sync/processors/subscription.js +268 -428
- package/lib/sync/processors/sync.js +276 -464
- package/lib/sync/utils.js +248 -393
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.js +16 -58
- package/lib/util.js +335 -575
- package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -1
- package/lib-esm/authModeStrategies/multiAuthStrategy.js +27 -103
- package/lib-esm/datastore/datastore.js +874 -1359
- package/lib-esm/index.js +6 -6
- package/lib-esm/predicates/index.js +54 -104
- package/lib-esm/predicates/next.js +306 -555
- package/lib-esm/predicates/sort.js +24 -27
- package/lib-esm/ssr/index.js +1 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -446
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -477
- package/lib-esm/storage/adapter/InMemoryStore.js +27 -102
- package/lib-esm/storage/adapter/IndexedDBAdapter.js +436 -997
- package/lib-esm/storage/adapter/StorageAdapterBase.js +172 -392
- package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -3
- package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -1
- package/lib-esm/storage/relationship.js +173 -260
- package/lib-esm/storage/storage.js +236 -499
- package/lib-esm/sync/datastoreConnectivity.js +26 -82
- package/lib-esm/sync/datastoreReachability/index.js +1 -1
- package/lib-esm/sync/datastoreReachability/index.native.js +1 -1
- package/lib-esm/sync/index.js +498 -872
- package/lib-esm/sync/merger.js +28 -131
- package/lib-esm/sync/outbox.js +143 -298
- package/lib-esm/sync/processors/errorMaps.js +32 -82
- package/lib-esm/sync/processors/mutation.js +324 -572
- package/lib-esm/sync/processors/subscription.js +258 -418
- package/lib-esm/sync/processors/sync.js +269 -457
- package/lib-esm/sync/utils.js +245 -390
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types.js +16 -59
- package/lib-esm/util.js +335 -577
- package/package.json +12 -12
- package/src/datastore/datastore.ts +4 -3
- package/src/storage/adapter/getDefaultAdapter/index.ts +1 -3
- package/src/sync/processors/mutation.ts +1 -1
- package/src/sync/processors/sync.ts +1 -1
- package/src/sync/utils.ts +1 -1
- package/src/util.ts +44 -6
package/lib-esm/sync/index.js
CHANGED
|
@@ -1,75 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
38
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
39
|
-
if (!m) return o;
|
|
40
|
-
var i = m.call(o), r, ar = [], e;
|
|
41
|
-
try {
|
|
42
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
43
|
-
}
|
|
44
|
-
catch (error) { e = { error: error }; }
|
|
45
|
-
finally {
|
|
46
|
-
try {
|
|
47
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
48
|
-
}
|
|
49
|
-
finally { if (e) throw e.error; }
|
|
50
|
-
}
|
|
51
|
-
return ar;
|
|
52
|
-
};
|
|
53
|
-
var __values = (this && this.__values) || function(o) {
|
|
54
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
55
|
-
if (m) return m.call(o);
|
|
56
|
-
if (o && typeof o.length === "number") return {
|
|
57
|
-
next: function () {
|
|
58
|
-
if (o && i >= o.length) o = void 0;
|
|
59
|
-
return { value: o && o[i++], done: !o };
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
63
|
-
};
|
|
64
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
65
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
66
|
-
if (ar || !(i in from)) {
|
|
67
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
68
|
-
ar[i] = from[i];
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
72
|
-
};
|
|
73
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
74
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
75
3
|
import { BackgroundProcessManager } from '@aws-amplify/core/internals/utils';
|
|
@@ -86,8 +14,8 @@ import { CONTROL_MSG, SubscriptionProcessor } from './processors/subscription';
|
|
|
86
14
|
import { SyncProcessor } from './processors/sync';
|
|
87
15
|
import { createMutationInstanceFromModelOperation, getIdentifierValue, predicateToGraphQLCondition, } from './utils';
|
|
88
16
|
import { CONTROL_MSG as PUBSUB_CONTROL_MSG, ConnectionState, CONNECTION_STATE_CHANGE as PUBSUB_CONNECTION_STATE_CHANGE, } from '@aws-amplify/api-graphql';
|
|
89
|
-
|
|
90
|
-
|
|
17
|
+
const logger = new ConsoleLogger('DataStore');
|
|
18
|
+
const ownSymbol = Symbol('sync');
|
|
91
19
|
export var ControlMessage;
|
|
92
20
|
(function (ControlMessage) {
|
|
93
21
|
ControlMessage["SYNC_ENGINE_STORAGE_SUBSCRIBED"] = "storageSubscribed";
|
|
@@ -101,10 +29,11 @@ export var ControlMessage;
|
|
|
101
29
|
ControlMessage["SYNC_ENGINE_NETWORK_STATUS"] = "networkStatus";
|
|
102
30
|
ControlMessage["SYNC_ENGINE_READY"] = "ready";
|
|
103
31
|
})(ControlMessage || (ControlMessage = {}));
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
32
|
+
export class SyncEngine {
|
|
33
|
+
getModelSyncedStatus(modelConstructor) {
|
|
34
|
+
return this.modelSyncedStatus.get(modelConstructor);
|
|
35
|
+
}
|
|
36
|
+
constructor(schema, namespaceResolver, modelClasses, userModelClasses, storage, modelInstanceCreator, conflictHandler, errorHandler, syncPredicates, amplifyConfig = {}, authModeStrategy, amplifyContext, connectivityMonitor) {
|
|
108
37
|
this.schema = schema;
|
|
109
38
|
this.namespaceResolver = namespaceResolver;
|
|
110
39
|
this.modelClasses = modelClasses;
|
|
@@ -120,10 +49,10 @@ var SyncEngine = /** @class */ (function () {
|
|
|
120
49
|
this.modelSyncedStatus = new WeakMap();
|
|
121
50
|
this.connectionDisrupted = false;
|
|
122
51
|
this.runningProcesses = new BackgroundProcessManager();
|
|
123
|
-
this.waitForSleepState = new Promise(
|
|
124
|
-
|
|
52
|
+
this.waitForSleepState = new Promise(resolve => {
|
|
53
|
+
this.syncQueriesObservableStartSleeping = resolve;
|
|
125
54
|
});
|
|
126
|
-
|
|
55
|
+
const MutationEvent = this.modelClasses['MutationEvent'];
|
|
127
56
|
this.outbox = new MutationEventOutbox(this.schema, MutationEvent, modelInstanceCreator, ownSymbol);
|
|
128
57
|
this.modelMerger = new ModelMerger(this.outbox, ownSymbol);
|
|
129
58
|
this.syncQueriesProcessor = new SyncProcessor(this.schema, this.syncPredicates, this.amplifyConfig, this.authModeStrategy, errorHandler, this.amplifyContext);
|
|
@@ -132,807 +61,510 @@ var SyncEngine = /** @class */ (function () {
|
|
|
132
61
|
this.datastoreConnectivity =
|
|
133
62
|
this.connectivityMonitor || new DataStoreConnectivity();
|
|
134
63
|
}
|
|
135
|
-
|
|
136
|
-
return
|
|
137
|
-
};
|
|
138
|
-
SyncEngine.prototype.start = function (params) {
|
|
139
|
-
var _this = this;
|
|
140
|
-
return new Observable(function (observer) {
|
|
64
|
+
start(params) {
|
|
65
|
+
return new Observable(observer => {
|
|
141
66
|
logger.log('starting sync engine...');
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
_b.label = 1;
|
|
187
|
-
case 1:
|
|
188
|
-
_b.trys.push([1, 3, , 4]);
|
|
189
|
-
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
190
|
-
onTerminate.then(reject);
|
|
191
|
-
var ctlSubsSubscription = ctlSubsObservable_1.subscribe({
|
|
192
|
-
next: function (msg) {
|
|
193
|
-
if (msg === CONTROL_MSG.CONNECTED) {
|
|
194
|
-
resolve();
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
error: function (err) {
|
|
198
|
-
reject(err);
|
|
199
|
-
var handleDisconnect = _this.disconnectionHandler();
|
|
200
|
-
handleDisconnect(err);
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
subscriptions.push(ctlSubsSubscription);
|
|
204
|
-
})];
|
|
205
|
-
case 2:
|
|
206
|
-
_b.sent();
|
|
207
|
-
return [3 /*break*/, 4];
|
|
208
|
-
case 3:
|
|
209
|
-
err_2 = _b.sent();
|
|
210
|
-
observer.error(err_2);
|
|
211
|
-
failedStarting();
|
|
212
|
-
return [2 /*return*/];
|
|
213
|
-
case 4:
|
|
214
|
-
logger.log('Realtime ready');
|
|
215
|
-
observer.next({
|
|
216
|
-
type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
|
|
217
|
-
});
|
|
218
|
-
_b.label = 5;
|
|
219
|
-
case 5:
|
|
220
|
-
_b.trys.push([5, 7, , 8]);
|
|
221
|
-
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
222
|
-
var syncQuerySubscription = _this.syncQueriesObservable().subscribe({
|
|
223
|
-
next: function (message) {
|
|
224
|
-
var type = message.type;
|
|
225
|
-
if (type ===
|
|
226
|
-
ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY) {
|
|
227
|
-
resolve();
|
|
228
|
-
}
|
|
229
|
-
observer.next(message);
|
|
230
|
-
},
|
|
231
|
-
complete: function () {
|
|
232
|
-
resolve();
|
|
233
|
-
},
|
|
234
|
-
error: function (error) {
|
|
235
|
-
reject(error);
|
|
236
|
-
},
|
|
237
|
-
});
|
|
238
|
-
if (syncQuerySubscription) {
|
|
239
|
-
subscriptions.push(syncQuerySubscription);
|
|
240
|
-
}
|
|
241
|
-
})];
|
|
242
|
-
case 6:
|
|
243
|
-
_b.sent();
|
|
244
|
-
return [3 /*break*/, 8];
|
|
245
|
-
case 7:
|
|
246
|
-
error_1 = _b.sent();
|
|
247
|
-
observer.error(error_1);
|
|
248
|
-
failedStarting();
|
|
249
|
-
return [2 /*return*/];
|
|
250
|
-
case 8:
|
|
251
|
-
//#endregion
|
|
252
|
-
//#region process mutations (outbox)
|
|
253
|
-
subscriptions.push(this.mutationsProcessor
|
|
254
|
-
.start()
|
|
255
|
-
.subscribe(function (_a) {
|
|
256
|
-
var modelDefinition = _a.modelDefinition, item = _a.model, hasMore = _a.hasMore;
|
|
257
|
-
return _this.runningProcesses.add(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
258
|
-
var modelConstructor, model;
|
|
259
|
-
var _this = this;
|
|
260
|
-
return __generator(this, function (_a) {
|
|
261
|
-
switch (_a.label) {
|
|
262
|
-
case 0:
|
|
263
|
-
modelConstructor = this.userModelClasses[modelDefinition.name];
|
|
264
|
-
model = this.modelInstanceCreator(modelConstructor, item);
|
|
265
|
-
return [4 /*yield*/, this.storage.runExclusive(function (storage) {
|
|
266
|
-
return _this.modelMerger.merge(storage, model, modelDefinition);
|
|
267
|
-
})];
|
|
268
|
-
case 1:
|
|
269
|
-
_a.sent();
|
|
270
|
-
observer.next({
|
|
271
|
-
type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
|
|
272
|
-
data: {
|
|
273
|
-
model: modelConstructor,
|
|
274
|
-
element: model,
|
|
275
|
-
},
|
|
276
|
-
});
|
|
277
|
-
observer.next({
|
|
278
|
-
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
279
|
-
data: {
|
|
280
|
-
isEmpty: !hasMore,
|
|
281
|
-
},
|
|
282
|
-
});
|
|
283
|
-
return [2 /*return*/];
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
}); }, 'mutation processor event');
|
|
287
|
-
}));
|
|
288
|
-
//#endregion
|
|
289
|
-
//#region Merge subscriptions buffer
|
|
290
|
-
subscriptions.push(dataSubsObservable.subscribe(function (_a) {
|
|
291
|
-
var _b = __read(_a, 3), _transformerMutationType = _b[0], modelDefinition = _b[1], item = _b[2];
|
|
292
|
-
return _this.runningProcesses.add(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
293
|
-
var modelConstructor, model;
|
|
294
|
-
var _this = this;
|
|
295
|
-
return __generator(this, function (_a) {
|
|
296
|
-
switch (_a.label) {
|
|
297
|
-
case 0:
|
|
298
|
-
modelConstructor = this.userModelClasses[modelDefinition.name];
|
|
299
|
-
model = this.modelInstanceCreator(modelConstructor, item);
|
|
300
|
-
return [4 /*yield*/, this.storage.runExclusive(function (storage) {
|
|
301
|
-
return _this.modelMerger.merge(storage, model, modelDefinition);
|
|
302
|
-
})];
|
|
303
|
-
case 1:
|
|
304
|
-
_a.sent();
|
|
305
|
-
return [2 /*return*/];
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
}); }, 'subscription dataSubsObservable event');
|
|
309
|
-
}));
|
|
310
|
-
return [3 /*break*/, 10];
|
|
311
|
-
case 9:
|
|
312
|
-
if (!online) {
|
|
313
|
-
this.online = online;
|
|
314
|
-
observer.next({
|
|
315
|
-
type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
|
|
316
|
-
data: {
|
|
317
|
-
active: this.online,
|
|
318
|
-
},
|
|
319
|
-
});
|
|
320
|
-
subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
|
|
321
|
-
subscriptions = [];
|
|
322
|
-
}
|
|
323
|
-
_b.label = 10;
|
|
324
|
-
case 10:
|
|
325
|
-
doneStarting();
|
|
326
|
-
return [2 /*return*/];
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
}); }, 'datastore connectivity event')];
|
|
67
|
+
let subscriptions = [];
|
|
68
|
+
this.runningProcesses.add(async () => {
|
|
69
|
+
try {
|
|
70
|
+
await this.setupModels(params);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
observer.error(err);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
// this is awaited at the bottom. so, we don't need to register
|
|
77
|
+
// this explicitly with the context. it's already contained.
|
|
78
|
+
const startPromise = new Promise((doneStarting, failedStarting) => {
|
|
79
|
+
this.datastoreConnectivity.status().subscribe(async ({ online }) => this.runningProcesses.isOpen &&
|
|
80
|
+
this.runningProcesses.add(async (onTerminate) => {
|
|
81
|
+
// From offline to online
|
|
82
|
+
if (online && !this.online) {
|
|
83
|
+
this.online = online;
|
|
84
|
+
observer.next({
|
|
85
|
+
type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
|
|
86
|
+
data: {
|
|
87
|
+
active: this.online,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
let ctlSubsObservable;
|
|
91
|
+
let dataSubsObservable;
|
|
92
|
+
this.stopDisruptionListener =
|
|
93
|
+
this.startDisruptionListener();
|
|
94
|
+
//#region GraphQL Subscriptions
|
|
95
|
+
[ctlSubsObservable, dataSubsObservable] =
|
|
96
|
+
this.subscriptionsProcessor.start();
|
|
97
|
+
try {
|
|
98
|
+
await new Promise((resolve, reject) => {
|
|
99
|
+
onTerminate.then(reject);
|
|
100
|
+
const ctlSubsSubscription = ctlSubsObservable.subscribe({
|
|
101
|
+
next: msg => {
|
|
102
|
+
if (msg === CONTROL_MSG.CONNECTED) {
|
|
103
|
+
resolve();
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
error: err => {
|
|
107
|
+
reject(err);
|
|
108
|
+
const handleDisconnect = this.disconnectionHandler();
|
|
109
|
+
handleDisconnect(err);
|
|
110
|
+
},
|
|
330
111
|
});
|
|
112
|
+
subscriptions.push(ctlSubsSubscription);
|
|
331
113
|
});
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
observer.error(err);
|
|
117
|
+
failedStarting();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
logger.log('Realtime ready');
|
|
121
|
+
observer.next({
|
|
122
|
+
type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
|
|
332
123
|
});
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region Base & Sync queries
|
|
126
|
+
try {
|
|
127
|
+
await new Promise((resolve, reject) => {
|
|
128
|
+
const syncQuerySubscription = this.syncQueriesObservable().subscribe({
|
|
129
|
+
next: message => {
|
|
130
|
+
const { type } = message;
|
|
131
|
+
if (type ===
|
|
132
|
+
ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY) {
|
|
133
|
+
resolve();
|
|
134
|
+
}
|
|
135
|
+
observer.next(message);
|
|
136
|
+
},
|
|
137
|
+
complete: () => {
|
|
138
|
+
resolve();
|
|
139
|
+
},
|
|
140
|
+
error: error => {
|
|
141
|
+
reject(error);
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
if (syncQuerySubscription) {
|
|
145
|
+
subscriptions.push(syncQuerySubscription);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
observer.error(error);
|
|
151
|
+
failedStarting();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region process mutations (outbox)
|
|
156
|
+
subscriptions.push(this.mutationsProcessor
|
|
157
|
+
.start()
|
|
158
|
+
.subscribe(({ modelDefinition, model: item, hasMore }) => this.runningProcesses.add(async () => {
|
|
159
|
+
const modelConstructor = this.userModelClasses[modelDefinition.name];
|
|
160
|
+
const model = this.modelInstanceCreator(modelConstructor, item);
|
|
161
|
+
await this.storage.runExclusive(storage => this.modelMerger.merge(storage, model, modelDefinition));
|
|
162
|
+
observer.next({
|
|
163
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
|
|
164
|
+
data: {
|
|
165
|
+
model: modelConstructor,
|
|
166
|
+
element: model,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
observer.next({
|
|
170
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
171
|
+
data: {
|
|
172
|
+
isEmpty: !hasMore,
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}, 'mutation processor event')));
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region Merge subscriptions buffer
|
|
178
|
+
subscriptions.push(dataSubsObservable.subscribe(([_transformerMutationType, modelDefinition, item]) => this.runningProcesses.add(async () => {
|
|
179
|
+
const modelConstructor = this.userModelClasses[modelDefinition.name];
|
|
180
|
+
const model = this.modelInstanceCreator(modelConstructor, item);
|
|
181
|
+
await this.storage.runExclusive(storage => this.modelMerger.merge(storage, model, modelDefinition));
|
|
182
|
+
}, 'subscription dataSubsObservable event')));
|
|
183
|
+
//#endregion
|
|
184
|
+
}
|
|
185
|
+
else if (!online) {
|
|
186
|
+
this.online = online;
|
|
187
|
+
observer.next({
|
|
188
|
+
type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
|
|
189
|
+
data: {
|
|
190
|
+
active: this.online,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
subscriptions.forEach(sub => sub.unsubscribe());
|
|
194
|
+
subscriptions = [];
|
|
195
|
+
}
|
|
196
|
+
doneStarting();
|
|
197
|
+
}, 'datastore connectivity event'));
|
|
198
|
+
});
|
|
199
|
+
this.storage
|
|
200
|
+
.observe(null, null, ownSymbol)
|
|
201
|
+
.pipe(filter(({ model }) => {
|
|
202
|
+
const modelDefinition = this.getModelDefinition(model);
|
|
203
|
+
return modelDefinition.syncable === true;
|
|
204
|
+
}))
|
|
205
|
+
.subscribe({
|
|
206
|
+
next: async ({ opType, model, element, condition }) => this.runningProcesses.add(async () => {
|
|
207
|
+
const namespace = this.schema.namespaces[this.namespaceResolver(model)];
|
|
208
|
+
const MutationEventConstructor = this.modelClasses['MutationEvent'];
|
|
209
|
+
const modelDefinition = this.getModelDefinition(model);
|
|
210
|
+
const graphQLCondition = predicateToGraphQLCondition(condition, modelDefinition);
|
|
211
|
+
const mutationEvent = createMutationInstanceFromModelOperation(namespace.relationships, this.getModelDefinition(model), opType, model, element, graphQLCondition, MutationEventConstructor, this.modelInstanceCreator);
|
|
212
|
+
await this.outbox.enqueue(this.storage, mutationEvent);
|
|
213
|
+
observer.next({
|
|
214
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_ENQUEUED,
|
|
215
|
+
data: {
|
|
216
|
+
model,
|
|
217
|
+
element,
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
observer.next({
|
|
221
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
222
|
+
data: {
|
|
223
|
+
isEmpty: false,
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
await startPromise;
|
|
227
|
+
// Set by the this.datastoreConnectivity.status().subscribe() loop
|
|
228
|
+
if (this.online) {
|
|
229
|
+
this.mutationsProcessor.resume();
|
|
230
|
+
}
|
|
231
|
+
}, 'storage event'),
|
|
232
|
+
});
|
|
233
|
+
observer.next({
|
|
234
|
+
type: ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED,
|
|
235
|
+
});
|
|
236
|
+
const hasMutationsInOutbox = (await this.outbox.peek(this.storage)) === undefined;
|
|
237
|
+
observer.next({
|
|
238
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
239
|
+
data: {
|
|
240
|
+
isEmpty: hasMutationsInOutbox,
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
await startPromise;
|
|
244
|
+
observer.next({
|
|
245
|
+
type: ControlMessage.SYNC_ENGINE_READY,
|
|
246
|
+
});
|
|
247
|
+
}, 'sync start');
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
async getModelsMetadataWithNextFullSync(currentTimeStamp) {
|
|
251
|
+
const modelLastSync = new Map((await this.runningProcesses.add(() => this.getModelsMetadata(), 'sync/index getModelsMetadataWithNextFullSync')).map(({ namespace, model, lastSync, lastFullSync, fullSyncInterval, lastSyncPredicate, }) => {
|
|
252
|
+
const nextFullSync = lastFullSync + fullSyncInterval;
|
|
253
|
+
const syncFrom = !lastFullSync || nextFullSync < currentTimeStamp
|
|
254
|
+
? 0 // perform full sync if expired
|
|
255
|
+
: lastSync; // perform delta sync
|
|
256
|
+
return [
|
|
257
|
+
this.schema.namespaces[namespace].models[model],
|
|
258
|
+
[namespace, syncFrom],
|
|
259
|
+
];
|
|
260
|
+
}));
|
|
261
|
+
return modelLastSync;
|
|
262
|
+
}
|
|
263
|
+
syncQueriesObservable() {
|
|
264
|
+
if (!this.online) {
|
|
265
|
+
return of({}); // TODO(v6): fix this
|
|
266
|
+
}
|
|
267
|
+
return new Observable(observer => {
|
|
268
|
+
let syncQueriesSubscription;
|
|
269
|
+
this.runningProcesses.isOpen &&
|
|
270
|
+
this.runningProcesses.add(async (onTerminate) => {
|
|
271
|
+
let terminated = false;
|
|
272
|
+
while (!observer.closed && !terminated) {
|
|
273
|
+
const count = new WeakMap();
|
|
274
|
+
const modelLastSync = await this.getModelsMetadataWithNextFullSync(Date.now());
|
|
275
|
+
const paginatingModels = new Set(modelLastSync.keys());
|
|
276
|
+
let lastFullSyncStartedAt;
|
|
277
|
+
let syncInterval;
|
|
278
|
+
let start;
|
|
279
|
+
let syncDuration;
|
|
280
|
+
let lastStartedAt;
|
|
281
|
+
await new Promise((resolve, reject) => {
|
|
282
|
+
if (!this.runningProcesses.isOpen)
|
|
283
|
+
resolve();
|
|
284
|
+
onTerminate.then(() => resolve());
|
|
285
|
+
syncQueriesSubscription = this.syncQueriesProcessor
|
|
286
|
+
.start(modelLastSync)
|
|
341
287
|
.subscribe({
|
|
342
|
-
next:
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
288
|
+
next: async ({ namespace, modelDefinition, items, done, startedAt, isFullSync, }) => {
|
|
289
|
+
const modelConstructor = this.userModelClasses[modelDefinition.name];
|
|
290
|
+
if (!count.has(modelConstructor)) {
|
|
291
|
+
count.set(modelConstructor, {
|
|
292
|
+
new: 0,
|
|
293
|
+
updated: 0,
|
|
294
|
+
deleted: 0,
|
|
295
|
+
});
|
|
296
|
+
start = getNow();
|
|
297
|
+
lastStartedAt =
|
|
298
|
+
lastStartedAt === undefined
|
|
299
|
+
? startedAt
|
|
300
|
+
: Math.max(lastStartedAt, startedAt);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* If there are mutations in the outbox for a given id, those need to be
|
|
304
|
+
* merged individually. Otherwise, we can merge them in batches.
|
|
305
|
+
*/
|
|
306
|
+
await this.storage.runExclusive(async (storage) => {
|
|
307
|
+
const idsInOutbox = await this.outbox.getModelIds(storage);
|
|
308
|
+
const oneByOne = [];
|
|
309
|
+
const page = items.filter(item => {
|
|
310
|
+
const itemId = getIdentifierValue(modelDefinition, item);
|
|
311
|
+
if (!idsInOutbox.has(itemId)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
oneByOne.push(item);
|
|
315
|
+
return false;
|
|
316
|
+
});
|
|
317
|
+
const opTypeCount = [];
|
|
318
|
+
for (const item of oneByOne) {
|
|
319
|
+
const opType = await this.modelMerger.merge(storage, item, modelDefinition);
|
|
320
|
+
if (opType !== undefined) {
|
|
321
|
+
opTypeCount.push([item, opType]);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
opTypeCount.push(...(await this.modelMerger.mergePage(storage, modelConstructor, page, modelDefinition)));
|
|
325
|
+
const counts = count.get(modelConstructor);
|
|
326
|
+
opTypeCount.forEach(([, opType]) => {
|
|
327
|
+
switch (opType) {
|
|
328
|
+
case OpType.INSERT:
|
|
329
|
+
counts.new++;
|
|
330
|
+
break;
|
|
331
|
+
case OpType.UPDATE:
|
|
332
|
+
counts.updated++;
|
|
333
|
+
break;
|
|
334
|
+
case OpType.DELETE:
|
|
335
|
+
counts.deleted++;
|
|
336
|
+
break;
|
|
337
|
+
default:
|
|
338
|
+
throw new Error(`Invalid opType ${opType}`);
|
|
339
|
+
}
|
|
384
340
|
});
|
|
385
341
|
});
|
|
342
|
+
if (done) {
|
|
343
|
+
const { name: modelName } = modelDefinition;
|
|
344
|
+
//#region update last sync for type
|
|
345
|
+
let modelMetadata = await this.getModelMetadata(namespace, modelName);
|
|
346
|
+
const { lastFullSync, fullSyncInterval } = modelMetadata;
|
|
347
|
+
syncInterval = fullSyncInterval;
|
|
348
|
+
lastFullSyncStartedAt =
|
|
349
|
+
lastFullSyncStartedAt === undefined
|
|
350
|
+
? lastFullSync
|
|
351
|
+
: Math.max(lastFullSyncStartedAt, isFullSync ? startedAt : lastFullSync);
|
|
352
|
+
modelMetadata = this.modelClasses
|
|
353
|
+
.ModelMetadata.copyOf(modelMetadata, draft => {
|
|
354
|
+
draft.lastSync = startedAt;
|
|
355
|
+
draft.lastFullSync = isFullSync
|
|
356
|
+
? startedAt
|
|
357
|
+
: modelMetadata.lastFullSync;
|
|
358
|
+
});
|
|
359
|
+
await this.storage.save(modelMetadata, undefined, ownSymbol);
|
|
360
|
+
//#endregion
|
|
361
|
+
const counts = count.get(modelConstructor);
|
|
362
|
+
this.modelSyncedStatus.set(modelConstructor, true);
|
|
363
|
+
observer.next({
|
|
364
|
+
type: ControlMessage.SYNC_ENGINE_MODEL_SYNCED,
|
|
365
|
+
data: {
|
|
366
|
+
model: modelConstructor,
|
|
367
|
+
isFullSync,
|
|
368
|
+
isDeltaSync: !isFullSync,
|
|
369
|
+
counts,
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
paginatingModels.delete(modelDefinition);
|
|
373
|
+
if (paginatingModels.size === 0) {
|
|
374
|
+
syncDuration = getNow() - start;
|
|
375
|
+
resolve();
|
|
376
|
+
observer.next({
|
|
377
|
+
type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY,
|
|
378
|
+
});
|
|
379
|
+
syncQueriesSubscription.unsubscribe();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
error: error => {
|
|
384
|
+
observer.error(error);
|
|
386
385
|
},
|
|
387
386
|
});
|
|
388
387
|
observer.next({
|
|
389
|
-
type: ControlMessage.
|
|
390
|
-
});
|
|
391
|
-
return [4 /*yield*/, this.outbox.peek(this.storage)];
|
|
392
|
-
case 4:
|
|
393
|
-
hasMutationsInOutbox = (_a.sent()) === undefined;
|
|
394
|
-
observer.next({
|
|
395
|
-
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
388
|
+
type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_STARTED,
|
|
396
389
|
data: {
|
|
397
|
-
|
|
390
|
+
models: Array.from(paginatingModels).map(({ name }) => name),
|
|
398
391
|
},
|
|
399
392
|
});
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
393
|
+
});
|
|
394
|
+
// null is cast to 0 resulting in unexpected behavior.
|
|
395
|
+
// undefined in arithmetic operations results in NaN also resulting in unexpected behavior.
|
|
396
|
+
// If lastFullSyncStartedAt is null this is the first sync.
|
|
397
|
+
// Assume lastStartedAt is is also newest full sync.
|
|
398
|
+
let msNextFullSync;
|
|
399
|
+
if (!lastFullSyncStartedAt) {
|
|
400
|
+
msNextFullSync = syncInterval - syncDuration;
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
msNextFullSync =
|
|
404
|
+
lastFullSyncStartedAt +
|
|
405
|
+
syncInterval -
|
|
406
|
+
(lastStartedAt + syncDuration);
|
|
407
|
+
}
|
|
408
|
+
logger.debug(`Next fullSync in ${msNextFullSync / 1000} seconds. (${new Date(Date.now() + msNextFullSync)})`);
|
|
409
|
+
// TODO: create `BackgroundProcessManager.sleep()` ... but, need to put
|
|
410
|
+
// a lot of thought into what that contract looks like to
|
|
411
|
+
// support possible use-cases:
|
|
412
|
+
//
|
|
413
|
+
// 1. non-cancelable
|
|
414
|
+
// 2. cancelable, unsleep on exit()
|
|
415
|
+
// 3. cancelable, throw Error on exit()
|
|
416
|
+
// 4. cancelable, callback first on exit()?
|
|
417
|
+
// 5. ... etc. ? ...
|
|
418
|
+
//
|
|
419
|
+
// TLDR; this is a lot of complexity here for a sleep(),
|
|
420
|
+
// but, it's not clear to me yet how to support an
|
|
421
|
+
// extensible, centralized cancelable `sleep()` elegantly.
|
|
422
|
+
await this.runningProcesses.add(async (onTerminate) => {
|
|
423
|
+
let sleepTimer;
|
|
424
|
+
let unsleep;
|
|
425
|
+
const sleep = new Promise(_unsleep => {
|
|
426
|
+
unsleep = _unsleep;
|
|
427
|
+
sleepTimer = setTimeout(unsleep, msNextFullSync);
|
|
428
|
+
});
|
|
429
|
+
onTerminate.then(() => {
|
|
430
|
+
terminated = true;
|
|
431
|
+
this.syncQueriesObservableStartSleeping();
|
|
432
|
+
unsleep();
|
|
405
433
|
});
|
|
406
|
-
|
|
434
|
+
this.unsleepSyncQueriesObservable = unsleep;
|
|
435
|
+
this.syncQueriesObservableStartSleeping();
|
|
436
|
+
return sleep;
|
|
437
|
+
}, 'syncQueriesObservable sleep');
|
|
438
|
+
this.unsleepSyncQueriesObservable = null;
|
|
439
|
+
this.waitForSleepState = new Promise(resolve => {
|
|
440
|
+
this.syncQueriesObservableStartSleeping = resolve;
|
|
441
|
+
});
|
|
407
442
|
}
|
|
408
|
-
});
|
|
409
|
-
}); }, 'sync start');
|
|
410
|
-
});
|
|
411
|
-
};
|
|
412
|
-
SyncEngine.prototype.getModelsMetadataWithNextFullSync = function (currentTimeStamp) {
|
|
413
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
414
|
-
var modelLastSync, _a;
|
|
415
|
-
var _this = this;
|
|
416
|
-
return __generator(this, function (_b) {
|
|
417
|
-
switch (_b.label) {
|
|
418
|
-
case 0:
|
|
419
|
-
_a = Map.bind;
|
|
420
|
-
return [4 /*yield*/, this.runningProcesses.add(function () { return _this.getModelsMetadata(); }, 'sync/index getModelsMetadataWithNextFullSync')];
|
|
421
|
-
case 1:
|
|
422
|
-
modelLastSync = new (_a.apply(Map, [void 0, (_b.sent()).map(function (_a) {
|
|
423
|
-
var namespace = _a.namespace, model = _a.model, lastSync = _a.lastSync, lastFullSync = _a.lastFullSync, fullSyncInterval = _a.fullSyncInterval, lastSyncPredicate = _a.lastSyncPredicate;
|
|
424
|
-
var nextFullSync = lastFullSync + fullSyncInterval;
|
|
425
|
-
var syncFrom = !lastFullSync || nextFullSync < currentTimeStamp
|
|
426
|
-
? 0 // perform full sync if expired
|
|
427
|
-
: lastSync; // perform delta sync
|
|
428
|
-
return [
|
|
429
|
-
_this.schema.namespaces[namespace].models[model],
|
|
430
|
-
[namespace, syncFrom],
|
|
431
|
-
];
|
|
432
|
-
})]))();
|
|
433
|
-
return [2 /*return*/, modelLastSync];
|
|
434
|
-
}
|
|
435
|
-
});
|
|
436
|
-
});
|
|
437
|
-
};
|
|
438
|
-
SyncEngine.prototype.syncQueriesObservable = function () {
|
|
439
|
-
var _this = this;
|
|
440
|
-
if (!this.online) {
|
|
441
|
-
return of({}); // TODO(v6): fix this
|
|
442
|
-
}
|
|
443
|
-
return new Observable(function (observer) {
|
|
444
|
-
var syncQueriesSubscription;
|
|
445
|
-
_this.runningProcesses.isOpen &&
|
|
446
|
-
_this.runningProcesses.add(function (onTerminate) { return __awaiter(_this, void 0, void 0, function () {
|
|
447
|
-
var terminated, _loop_1, this_1;
|
|
448
|
-
var _this = this;
|
|
449
|
-
return __generator(this, function (_a) {
|
|
450
|
-
switch (_a.label) {
|
|
451
|
-
case 0:
|
|
452
|
-
terminated = false;
|
|
453
|
-
_loop_1 = function () {
|
|
454
|
-
var count, modelLastSync, paginatingModels, lastFullSyncStartedAt, syncInterval, start, syncDuration, lastStartedAt, msNextFullSync;
|
|
455
|
-
return __generator(this, function (_b) {
|
|
456
|
-
switch (_b.label) {
|
|
457
|
-
case 0:
|
|
458
|
-
count = new WeakMap();
|
|
459
|
-
return [4 /*yield*/, this_1.getModelsMetadataWithNextFullSync(Date.now())];
|
|
460
|
-
case 1:
|
|
461
|
-
modelLastSync = _b.sent();
|
|
462
|
-
paginatingModels = new Set(modelLastSync.keys());
|
|
463
|
-
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
464
|
-
if (!_this.runningProcesses.isOpen)
|
|
465
|
-
resolve();
|
|
466
|
-
onTerminate.then(function () { return resolve(); });
|
|
467
|
-
syncQueriesSubscription = _this.syncQueriesProcessor
|
|
468
|
-
.start(modelLastSync)
|
|
469
|
-
.subscribe({
|
|
470
|
-
next: function (_a) {
|
|
471
|
-
var namespace = _a.namespace, modelDefinition = _a.modelDefinition, items = _a.items, done = _a.done, startedAt = _a.startedAt, isFullSync = _a.isFullSync;
|
|
472
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
473
|
-
var modelConstructor, modelName, modelMetadata_1, lastFullSync, fullSyncInterval, counts;
|
|
474
|
-
var _this = this;
|
|
475
|
-
return __generator(this, function (_b) {
|
|
476
|
-
switch (_b.label) {
|
|
477
|
-
case 0:
|
|
478
|
-
modelConstructor = this.userModelClasses[modelDefinition.name];
|
|
479
|
-
if (!count.has(modelConstructor)) {
|
|
480
|
-
count.set(modelConstructor, {
|
|
481
|
-
new: 0,
|
|
482
|
-
updated: 0,
|
|
483
|
-
deleted: 0,
|
|
484
|
-
});
|
|
485
|
-
start = getNow();
|
|
486
|
-
lastStartedAt =
|
|
487
|
-
lastStartedAt === undefined
|
|
488
|
-
? startedAt
|
|
489
|
-
: Math.max(lastStartedAt, startedAt);
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* If there are mutations in the outbox for a given id, those need to be
|
|
493
|
-
* merged individually. Otherwise, we can merge them in batches.
|
|
494
|
-
*/
|
|
495
|
-
return [4 /*yield*/, this.storage.runExclusive(function (storage) { return __awaiter(_this, void 0, void 0, function () {
|
|
496
|
-
var idsInOutbox, oneByOne, page, opTypeCount, oneByOne_1, oneByOne_1_1, item, opType, e_1_1, _a, _b, _c, _d, counts;
|
|
497
|
-
var e_1, _e;
|
|
498
|
-
return __generator(this, function (_f) {
|
|
499
|
-
switch (_f.label) {
|
|
500
|
-
case 0: return [4 /*yield*/, this.outbox.getModelIds(storage)];
|
|
501
|
-
case 1:
|
|
502
|
-
idsInOutbox = _f.sent();
|
|
503
|
-
oneByOne = [];
|
|
504
|
-
page = items.filter(function (item) {
|
|
505
|
-
var itemId = getIdentifierValue(modelDefinition, item);
|
|
506
|
-
if (!idsInOutbox.has(itemId)) {
|
|
507
|
-
return true;
|
|
508
|
-
}
|
|
509
|
-
oneByOne.push(item);
|
|
510
|
-
return false;
|
|
511
|
-
});
|
|
512
|
-
opTypeCount = [];
|
|
513
|
-
_f.label = 2;
|
|
514
|
-
case 2:
|
|
515
|
-
_f.trys.push([2, 7, 8, 9]);
|
|
516
|
-
oneByOne_1 = __values(oneByOne), oneByOne_1_1 = oneByOne_1.next();
|
|
517
|
-
_f.label = 3;
|
|
518
|
-
case 3:
|
|
519
|
-
if (!!oneByOne_1_1.done) return [3 /*break*/, 6];
|
|
520
|
-
item = oneByOne_1_1.value;
|
|
521
|
-
return [4 /*yield*/, this.modelMerger.merge(storage, item, modelDefinition)];
|
|
522
|
-
case 4:
|
|
523
|
-
opType = _f.sent();
|
|
524
|
-
if (opType !== undefined) {
|
|
525
|
-
opTypeCount.push([item, opType]);
|
|
526
|
-
}
|
|
527
|
-
_f.label = 5;
|
|
528
|
-
case 5:
|
|
529
|
-
oneByOne_1_1 = oneByOne_1.next();
|
|
530
|
-
return [3 /*break*/, 3];
|
|
531
|
-
case 6: return [3 /*break*/, 9];
|
|
532
|
-
case 7:
|
|
533
|
-
e_1_1 = _f.sent();
|
|
534
|
-
e_1 = { error: e_1_1 };
|
|
535
|
-
return [3 /*break*/, 9];
|
|
536
|
-
case 8:
|
|
537
|
-
try {
|
|
538
|
-
if (oneByOne_1_1 && !oneByOne_1_1.done && (_e = oneByOne_1.return)) _e.call(oneByOne_1);
|
|
539
|
-
}
|
|
540
|
-
finally { if (e_1) throw e_1.error; }
|
|
541
|
-
return [7 /*endfinally*/];
|
|
542
|
-
case 9:
|
|
543
|
-
_b = (_a = opTypeCount.push).apply;
|
|
544
|
-
_c = [opTypeCount];
|
|
545
|
-
_d = [[]];
|
|
546
|
-
return [4 /*yield*/, this.modelMerger.mergePage(storage, modelConstructor, page, modelDefinition)];
|
|
547
|
-
case 10:
|
|
548
|
-
_b.apply(_a, _c.concat([__spreadArray.apply(void 0, _d.concat([__read.apply(void 0, [(_f.sent())]), false]))]));
|
|
549
|
-
counts = count.get(modelConstructor);
|
|
550
|
-
opTypeCount.forEach(function (_a) {
|
|
551
|
-
var _b = __read(_a, 2), opType = _b[1];
|
|
552
|
-
switch (opType) {
|
|
553
|
-
case OpType.INSERT:
|
|
554
|
-
counts.new++;
|
|
555
|
-
break;
|
|
556
|
-
case OpType.UPDATE:
|
|
557
|
-
counts.updated++;
|
|
558
|
-
break;
|
|
559
|
-
case OpType.DELETE:
|
|
560
|
-
counts.deleted++;
|
|
561
|
-
break;
|
|
562
|
-
default:
|
|
563
|
-
throw new Error("Invalid opType ".concat(opType));
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
return [2 /*return*/];
|
|
567
|
-
}
|
|
568
|
-
});
|
|
569
|
-
}); })];
|
|
570
|
-
case 1:
|
|
571
|
-
/**
|
|
572
|
-
* If there are mutations in the outbox for a given id, those need to be
|
|
573
|
-
* merged individually. Otherwise, we can merge them in batches.
|
|
574
|
-
*/
|
|
575
|
-
_b.sent();
|
|
576
|
-
if (!done) return [3 /*break*/, 4];
|
|
577
|
-
modelName = modelDefinition.name;
|
|
578
|
-
return [4 /*yield*/, this.getModelMetadata(namespace, modelName)];
|
|
579
|
-
case 2:
|
|
580
|
-
modelMetadata_1 = _b.sent();
|
|
581
|
-
lastFullSync = modelMetadata_1.lastFullSync, fullSyncInterval = modelMetadata_1.fullSyncInterval;
|
|
582
|
-
syncInterval = fullSyncInterval;
|
|
583
|
-
lastFullSyncStartedAt =
|
|
584
|
-
lastFullSyncStartedAt === undefined
|
|
585
|
-
? lastFullSync
|
|
586
|
-
: Math.max(lastFullSyncStartedAt, isFullSync ? startedAt : lastFullSync);
|
|
587
|
-
modelMetadata_1 = this.modelClasses
|
|
588
|
-
.ModelMetadata.copyOf(modelMetadata_1, function (draft) {
|
|
589
|
-
draft.lastSync = startedAt;
|
|
590
|
-
draft.lastFullSync = isFullSync
|
|
591
|
-
? startedAt
|
|
592
|
-
: modelMetadata_1.lastFullSync;
|
|
593
|
-
});
|
|
594
|
-
return [4 /*yield*/, this.storage.save(modelMetadata_1, undefined, ownSymbol)];
|
|
595
|
-
case 3:
|
|
596
|
-
_b.sent();
|
|
597
|
-
counts = count.get(modelConstructor);
|
|
598
|
-
this.modelSyncedStatus.set(modelConstructor, true);
|
|
599
|
-
observer.next({
|
|
600
|
-
type: ControlMessage.SYNC_ENGINE_MODEL_SYNCED,
|
|
601
|
-
data: {
|
|
602
|
-
model: modelConstructor,
|
|
603
|
-
isFullSync: isFullSync,
|
|
604
|
-
isDeltaSync: !isFullSync,
|
|
605
|
-
counts: counts,
|
|
606
|
-
},
|
|
607
|
-
});
|
|
608
|
-
paginatingModels.delete(modelDefinition);
|
|
609
|
-
if (paginatingModels.size === 0) {
|
|
610
|
-
syncDuration = getNow() - start;
|
|
611
|
-
resolve();
|
|
612
|
-
observer.next({
|
|
613
|
-
type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY,
|
|
614
|
-
});
|
|
615
|
-
syncQueriesSubscription.unsubscribe();
|
|
616
|
-
}
|
|
617
|
-
_b.label = 4;
|
|
618
|
-
case 4: return [2 /*return*/];
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
});
|
|
622
|
-
},
|
|
623
|
-
error: function (error) {
|
|
624
|
-
observer.error(error);
|
|
625
|
-
},
|
|
626
|
-
});
|
|
627
|
-
observer.next({
|
|
628
|
-
type: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_STARTED,
|
|
629
|
-
data: {
|
|
630
|
-
models: Array.from(paginatingModels).map(function (_a) {
|
|
631
|
-
var name = _a.name;
|
|
632
|
-
return name;
|
|
633
|
-
}),
|
|
634
|
-
},
|
|
635
|
-
});
|
|
636
|
-
})];
|
|
637
|
-
case 2:
|
|
638
|
-
_b.sent();
|
|
639
|
-
if (!lastFullSyncStartedAt) {
|
|
640
|
-
msNextFullSync = syncInterval - syncDuration;
|
|
641
|
-
}
|
|
642
|
-
else {
|
|
643
|
-
msNextFullSync =
|
|
644
|
-
lastFullSyncStartedAt +
|
|
645
|
-
syncInterval -
|
|
646
|
-
(lastStartedAt + syncDuration);
|
|
647
|
-
}
|
|
648
|
-
logger.debug("Next fullSync in ".concat(msNextFullSync / 1000, " seconds. (").concat(new Date(Date.now() + msNextFullSync), ")"));
|
|
649
|
-
// TODO: create `BackgroundProcessManager.sleep()` ... but, need to put
|
|
650
|
-
// a lot of thought into what that contract looks like to
|
|
651
|
-
// support possible use-cases:
|
|
652
|
-
//
|
|
653
|
-
// 1. non-cancelable
|
|
654
|
-
// 2. cancelable, unsleep on exit()
|
|
655
|
-
// 3. cancelable, throw Error on exit()
|
|
656
|
-
// 4. cancelable, callback first on exit()?
|
|
657
|
-
// 5. ... etc. ? ...
|
|
658
|
-
//
|
|
659
|
-
// TLDR; this is a lot of complexity here for a sleep(),
|
|
660
|
-
// but, it's not clear to me yet how to support an
|
|
661
|
-
// extensible, centralized cancelable `sleep()` elegantly.
|
|
662
|
-
return [4 /*yield*/, this_1.runningProcesses.add(function (onTerminate) { return __awaiter(_this, void 0, void 0, function () {
|
|
663
|
-
var sleepTimer, unsleep, sleep;
|
|
664
|
-
var _this = this;
|
|
665
|
-
return __generator(this, function (_a) {
|
|
666
|
-
sleep = new Promise(function (_unsleep) {
|
|
667
|
-
unsleep = _unsleep;
|
|
668
|
-
sleepTimer = setTimeout(unsleep, msNextFullSync);
|
|
669
|
-
});
|
|
670
|
-
onTerminate.then(function () {
|
|
671
|
-
terminated = true;
|
|
672
|
-
_this.syncQueriesObservableStartSleeping();
|
|
673
|
-
unsleep();
|
|
674
|
-
});
|
|
675
|
-
this.unsleepSyncQueriesObservable = unsleep;
|
|
676
|
-
this.syncQueriesObservableStartSleeping();
|
|
677
|
-
return [2 /*return*/, sleep];
|
|
678
|
-
});
|
|
679
|
-
}); }, 'syncQueriesObservable sleep')];
|
|
680
|
-
case 3:
|
|
681
|
-
// TODO: create `BackgroundProcessManager.sleep()` ... but, need to put
|
|
682
|
-
// a lot of thought into what that contract looks like to
|
|
683
|
-
// support possible use-cases:
|
|
684
|
-
//
|
|
685
|
-
// 1. non-cancelable
|
|
686
|
-
// 2. cancelable, unsleep on exit()
|
|
687
|
-
// 3. cancelable, throw Error on exit()
|
|
688
|
-
// 4. cancelable, callback first on exit()?
|
|
689
|
-
// 5. ... etc. ? ...
|
|
690
|
-
//
|
|
691
|
-
// TLDR; this is a lot of complexity here for a sleep(),
|
|
692
|
-
// but, it's not clear to me yet how to support an
|
|
693
|
-
// extensible, centralized cancelable `sleep()` elegantly.
|
|
694
|
-
_b.sent();
|
|
695
|
-
this_1.unsleepSyncQueriesObservable = null;
|
|
696
|
-
this_1.waitForSleepState = new Promise(function (resolve) {
|
|
697
|
-
_this.syncQueriesObservableStartSleeping = resolve;
|
|
698
|
-
});
|
|
699
|
-
return [2 /*return*/];
|
|
700
|
-
}
|
|
701
|
-
});
|
|
702
|
-
};
|
|
703
|
-
this_1 = this;
|
|
704
|
-
_a.label = 1;
|
|
705
|
-
case 1:
|
|
706
|
-
if (!(!observer.closed && !terminated)) return [3 /*break*/, 3];
|
|
707
|
-
return [5 /*yield**/, _loop_1()];
|
|
708
|
-
case 2:
|
|
709
|
-
_a.sent();
|
|
710
|
-
return [3 /*break*/, 1];
|
|
711
|
-
case 3: return [2 /*return*/];
|
|
712
|
-
}
|
|
713
|
-
});
|
|
714
|
-
}); }, 'syncQueriesObservable main');
|
|
443
|
+
}, 'syncQueriesObservable main');
|
|
715
444
|
});
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
return function (msg) {
|
|
445
|
+
}
|
|
446
|
+
disconnectionHandler() {
|
|
447
|
+
return (msg) => {
|
|
720
448
|
// This implementation is tied to AWSAppSyncRealTimeProvider 'Connection closed', 'Timeout disconnect' msg
|
|
721
449
|
if (PUBSUB_CONTROL_MSG.CONNECTION_CLOSED === msg ||
|
|
722
450
|
PUBSUB_CONTROL_MSG.TIMEOUT_DISCONNECT === msg) {
|
|
723
|
-
|
|
451
|
+
this.datastoreConnectivity.socketDisconnected();
|
|
724
452
|
}
|
|
725
453
|
};
|
|
726
|
-
}
|
|
727
|
-
|
|
454
|
+
}
|
|
455
|
+
unsubscribeConnectivity() {
|
|
728
456
|
this.datastoreConnectivity.unsubscribe();
|
|
729
|
-
}
|
|
457
|
+
}
|
|
730
458
|
/**
|
|
731
459
|
* Stops all subscription activities and resolves when all activies report
|
|
732
460
|
* that they're disconnected, done retrying, etc..
|
|
733
461
|
*/
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
return [4 /*yield*/, this.runningProcesses.close()];
|
|
774
|
-
case 5:
|
|
775
|
-
_a.sent();
|
|
776
|
-
return [4 /*yield*/, this.runningProcesses.open()];
|
|
777
|
-
case 6:
|
|
778
|
-
_a.sent();
|
|
779
|
-
logger.debug('sync engine stopped and ready to restart');
|
|
780
|
-
return [2 /*return*/];
|
|
781
|
-
}
|
|
782
|
-
});
|
|
783
|
-
});
|
|
784
|
-
};
|
|
785
|
-
SyncEngine.prototype.setupModels = function (params) {
|
|
786
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
787
|
-
var fullSyncInterval, ModelMetadataConstructor, models, savedModel, promises, result, _a, _b, modelMetadata, modelName, e_2_1;
|
|
788
|
-
var e_2, _c;
|
|
789
|
-
var _this = this;
|
|
790
|
-
return __generator(this, function (_d) {
|
|
791
|
-
switch (_d.label) {
|
|
792
|
-
case 0:
|
|
793
|
-
fullSyncInterval = params.fullSyncInterval;
|
|
794
|
-
ModelMetadataConstructor = this.modelClasses
|
|
795
|
-
.ModelMetadata;
|
|
796
|
-
models = [];
|
|
797
|
-
Object.values(this.schema.namespaces).forEach(function (namespace) {
|
|
798
|
-
Object.values(namespace.models)
|
|
799
|
-
.filter(function (_a) {
|
|
800
|
-
var syncable = _a.syncable;
|
|
801
|
-
return syncable;
|
|
802
|
-
})
|
|
803
|
-
.forEach(function (model) {
|
|
804
|
-
models.push([namespace.name, model]);
|
|
805
|
-
if (namespace.name === USER) {
|
|
806
|
-
var modelConstructor = _this.userModelClasses[model.name];
|
|
807
|
-
_this.modelSyncedStatus.set(modelConstructor, false);
|
|
808
|
-
}
|
|
809
|
-
});
|
|
810
|
-
});
|
|
811
|
-
promises = models.map(function (_a) {
|
|
812
|
-
var _b = __read(_a, 2), namespace = _b[0], model = _b[1];
|
|
813
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
814
|
-
var modelMetadata, syncPredicate, lastSyncPredicate, prevSyncPredicate, syncPredicateUpdated_1;
|
|
815
|
-
var _c, _d, _e, _f;
|
|
816
|
-
return __generator(this, function (_g) {
|
|
817
|
-
switch (_g.label) {
|
|
818
|
-
case 0: return [4 /*yield*/, this.getModelMetadata(namespace, model.name)];
|
|
819
|
-
case 1:
|
|
820
|
-
modelMetadata = _g.sent();
|
|
821
|
-
syncPredicate = ModelPredicateCreator.getPredicates(this.syncPredicates.get(model), false);
|
|
822
|
-
lastSyncPredicate = syncPredicate
|
|
823
|
-
? JSON.stringify(syncPredicate)
|
|
824
|
-
: null;
|
|
825
|
-
if (!(modelMetadata === undefined)) return [3 /*break*/, 3];
|
|
826
|
-
return [4 /*yield*/, this.storage.save(this.modelInstanceCreator(ModelMetadataConstructor, {
|
|
827
|
-
model: model.name,
|
|
828
|
-
namespace: namespace,
|
|
829
|
-
lastSync: null,
|
|
830
|
-
fullSyncInterval: fullSyncInterval,
|
|
831
|
-
lastFullSync: null,
|
|
832
|
-
lastSyncPredicate: lastSyncPredicate,
|
|
833
|
-
}), undefined, ownSymbol)];
|
|
834
|
-
case 2:
|
|
835
|
-
_c = __read.apply(void 0, [_g.sent(), 1]), _d = __read(_c[0], 1), savedModel = _d[0];
|
|
836
|
-
return [3 /*break*/, 5];
|
|
837
|
-
case 3:
|
|
838
|
-
prevSyncPredicate = modelMetadata.lastSyncPredicate
|
|
839
|
-
? modelMetadata.lastSyncPredicate
|
|
840
|
-
: null;
|
|
841
|
-
syncPredicateUpdated_1 = prevSyncPredicate !== lastSyncPredicate;
|
|
842
|
-
return [4 /*yield*/, this.storage.save(ModelMetadataConstructor.copyOf(modelMetadata, function (draft) {
|
|
843
|
-
draft.fullSyncInterval = fullSyncInterval;
|
|
844
|
-
// perform a base sync if the syncPredicate changed in between calls to DataStore.start
|
|
845
|
-
// ensures that the local store contains all the data specified by the syncExpression
|
|
846
|
-
if (syncPredicateUpdated_1) {
|
|
847
|
-
draft.lastSync = null;
|
|
848
|
-
draft.lastFullSync = null;
|
|
849
|
-
draft.lastSyncPredicate = lastSyncPredicate;
|
|
850
|
-
}
|
|
851
|
-
}))];
|
|
852
|
-
case 4:
|
|
853
|
-
_e = __read.apply(void 0, [_g.sent(), 1]), _f = __read(_e[0], 1), savedModel = _f[0];
|
|
854
|
-
_g.label = 5;
|
|
855
|
-
case 5: return [2 /*return*/, savedModel];
|
|
856
|
-
}
|
|
857
|
-
});
|
|
858
|
-
});
|
|
859
|
-
});
|
|
860
|
-
result = {};
|
|
861
|
-
_d.label = 1;
|
|
862
|
-
case 1:
|
|
863
|
-
_d.trys.push([1, 6, 7, 8]);
|
|
864
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
865
|
-
case 2:
|
|
866
|
-
_a = __values.apply(void 0, [_d.sent()]), _b = _a.next();
|
|
867
|
-
_d.label = 3;
|
|
868
|
-
case 3:
|
|
869
|
-
if (!!_b.done) return [3 /*break*/, 5];
|
|
870
|
-
modelMetadata = _b.value;
|
|
871
|
-
modelName = modelMetadata.model;
|
|
872
|
-
result[modelName] = modelMetadata;
|
|
873
|
-
_d.label = 4;
|
|
874
|
-
case 4:
|
|
875
|
-
_b = _a.next();
|
|
876
|
-
return [3 /*break*/, 3];
|
|
877
|
-
case 5: return [3 /*break*/, 8];
|
|
878
|
-
case 6:
|
|
879
|
-
e_2_1 = _d.sent();
|
|
880
|
-
e_2 = { error: e_2_1 };
|
|
881
|
-
return [3 /*break*/, 8];
|
|
882
|
-
case 7:
|
|
883
|
-
try {
|
|
884
|
-
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
885
|
-
}
|
|
886
|
-
finally { if (e_2) throw e_2.error; }
|
|
887
|
-
return [7 /*endfinally*/];
|
|
888
|
-
case 8: return [2 /*return*/, result];
|
|
462
|
+
async stop() {
|
|
463
|
+
logger.debug('stopping sync engine');
|
|
464
|
+
/**
|
|
465
|
+
* Gracefully disconnecting subscribers first just prevents *more* work
|
|
466
|
+
* from entering the pipelines.
|
|
467
|
+
*/
|
|
468
|
+
this.unsubscribeConnectivity();
|
|
469
|
+
/**
|
|
470
|
+
* Stop listening for websocket connection disruption
|
|
471
|
+
*/
|
|
472
|
+
this.stopDisruptionListener && this.stopDisruptionListener();
|
|
473
|
+
/**
|
|
474
|
+
* aggressively shut down any lingering background processes.
|
|
475
|
+
* some of this might be semi-redundant with unsubscribing. however,
|
|
476
|
+
* unsubscribing doesn't allow us to wait for settling.
|
|
477
|
+
* (Whereas `stop()` does.)
|
|
478
|
+
*/
|
|
479
|
+
await this.mutationsProcessor.stop();
|
|
480
|
+
await this.subscriptionsProcessor.stop();
|
|
481
|
+
await this.datastoreConnectivity.stop();
|
|
482
|
+
await this.syncQueriesProcessor.stop();
|
|
483
|
+
await this.runningProcesses.close();
|
|
484
|
+
await this.runningProcesses.open();
|
|
485
|
+
logger.debug('sync engine stopped and ready to restart');
|
|
486
|
+
}
|
|
487
|
+
async setupModels(params) {
|
|
488
|
+
const { fullSyncInterval } = params;
|
|
489
|
+
const ModelMetadataConstructor = this.modelClasses
|
|
490
|
+
.ModelMetadata;
|
|
491
|
+
const models = [];
|
|
492
|
+
let savedModel;
|
|
493
|
+
Object.values(this.schema.namespaces).forEach(namespace => {
|
|
494
|
+
Object.values(namespace.models)
|
|
495
|
+
.filter(({ syncable }) => syncable)
|
|
496
|
+
.forEach(model => {
|
|
497
|
+
models.push([namespace.name, model]);
|
|
498
|
+
if (namespace.name === USER) {
|
|
499
|
+
const modelConstructor = this.userModelClasses[model.name];
|
|
500
|
+
this.modelSyncedStatus.set(modelConstructor, false);
|
|
889
501
|
}
|
|
890
502
|
});
|
|
891
503
|
});
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
504
|
+
const promises = models.map(async ([namespace, model]) => {
|
|
505
|
+
const modelMetadata = await this.getModelMetadata(namespace, model.name);
|
|
506
|
+
const syncPredicate = ModelPredicateCreator.getPredicates(this.syncPredicates.get(model), false);
|
|
507
|
+
const lastSyncPredicate = syncPredicate
|
|
508
|
+
? JSON.stringify(syncPredicate)
|
|
509
|
+
: null;
|
|
510
|
+
if (modelMetadata === undefined) {
|
|
511
|
+
[[savedModel]] = await this.storage.save(this.modelInstanceCreator(ModelMetadataConstructor, {
|
|
512
|
+
model: model.name,
|
|
513
|
+
namespace,
|
|
514
|
+
lastSync: null,
|
|
515
|
+
fullSyncInterval,
|
|
516
|
+
lastFullSync: null,
|
|
517
|
+
lastSyncPredicate,
|
|
518
|
+
}), undefined, ownSymbol);
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
const prevSyncPredicate = modelMetadata.lastSyncPredicate
|
|
522
|
+
? modelMetadata.lastSyncPredicate
|
|
523
|
+
: null;
|
|
524
|
+
const syncPredicateUpdated = prevSyncPredicate !== lastSyncPredicate;
|
|
525
|
+
[[savedModel]] = await this.storage.save(ModelMetadataConstructor.copyOf(modelMetadata, draft => {
|
|
526
|
+
draft.fullSyncInterval = fullSyncInterval;
|
|
527
|
+
// perform a base sync if the syncPredicate changed in between calls to DataStore.start
|
|
528
|
+
// ensures that the local store contains all the data specified by the syncExpression
|
|
529
|
+
if (syncPredicateUpdated) {
|
|
530
|
+
draft.lastSync = null;
|
|
531
|
+
draft.lastFullSync = null;
|
|
532
|
+
draft.lastSyncPredicate = lastSyncPredicate;
|
|
533
|
+
}
|
|
534
|
+
}));
|
|
535
|
+
}
|
|
536
|
+
return savedModel;
|
|
907
537
|
});
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
538
|
+
const result = {};
|
|
539
|
+
for (const modelMetadata of await Promise.all(promises)) {
|
|
540
|
+
const { model: modelName } = modelMetadata;
|
|
541
|
+
result[modelName] = modelMetadata;
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
545
|
+
async getModelsMetadata() {
|
|
546
|
+
const ModelMetadata = this.modelClasses
|
|
547
|
+
.ModelMetadata;
|
|
548
|
+
const modelsMetadata = await this.storage.query(ModelMetadata);
|
|
549
|
+
return modelsMetadata;
|
|
550
|
+
}
|
|
551
|
+
async getModelMetadata(namespace, model) {
|
|
552
|
+
const ModelMetadata = this.modelClasses
|
|
553
|
+
.ModelMetadata;
|
|
554
|
+
const predicate = ModelPredicateCreator.createFromAST(this.schema.namespaces[SYNC].models[ModelMetadata.name], { and: [{ namespace: { eq: namespace } }, { model: { eq: model } }] });
|
|
555
|
+
const [modelMetadata] = await this.storage.query(ModelMetadata, predicate, {
|
|
556
|
+
page: 0,
|
|
557
|
+
limit: 1,
|
|
927
558
|
});
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
559
|
+
return modelMetadata;
|
|
560
|
+
}
|
|
561
|
+
getModelDefinition(modelConstructor) {
|
|
562
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
563
|
+
const modelDefinition = this.schema.namespaces[namespaceName].models[modelConstructor.name];
|
|
932
564
|
return modelDefinition;
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
|
|
565
|
+
}
|
|
566
|
+
static getNamespace() {
|
|
567
|
+
const namespace = {
|
|
936
568
|
name: SYNC,
|
|
937
569
|
relationships: {},
|
|
938
570
|
enums: {
|
|
@@ -1040,7 +672,7 @@ var SyncEngine = /** @class */ (function () {
|
|
|
1040
672
|
},
|
|
1041
673
|
};
|
|
1042
674
|
return namespace;
|
|
1043
|
-
}
|
|
675
|
+
}
|
|
1044
676
|
/**
|
|
1045
677
|
* listen for websocket connection disruption
|
|
1046
678
|
*
|
|
@@ -1048,43 +680,37 @@ var SyncEngine = /** @class */ (function () {
|
|
|
1048
680
|
* from AppSync were missed. A sync needs to be triggered to
|
|
1049
681
|
* retrieve the missed data.
|
|
1050
682
|
*/
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
return Hub.listen('api', function (data) {
|
|
683
|
+
startDisruptionListener() {
|
|
684
|
+
return Hub.listen('api', (data) => {
|
|
1054
685
|
if (data.source === 'PubSub' &&
|
|
1055
686
|
data.payload.event === PUBSUB_CONNECTION_STATE_CHANGE) {
|
|
1056
|
-
|
|
687
|
+
const connectionState = data.payload.data
|
|
1057
688
|
.connectionState;
|
|
1058
689
|
switch (connectionState) {
|
|
1059
690
|
// Do not need to listen for ConnectionDisruptedPendingNetwork
|
|
1060
691
|
// Normal network reconnection logic will handle the sync
|
|
1061
692
|
case ConnectionState.ConnectionDisrupted:
|
|
1062
|
-
|
|
693
|
+
this.connectionDisrupted = true;
|
|
1063
694
|
break;
|
|
1064
695
|
case ConnectionState.Connected:
|
|
1065
|
-
if (
|
|
1066
|
-
|
|
696
|
+
if (this.connectionDisrupted) {
|
|
697
|
+
this.scheduleSync();
|
|
1067
698
|
}
|
|
1068
|
-
|
|
699
|
+
this.connectionDisrupted = false;
|
|
1069
700
|
break;
|
|
1070
701
|
}
|
|
1071
702
|
}
|
|
1072
703
|
});
|
|
1073
|
-
}
|
|
704
|
+
}
|
|
1074
705
|
/*
|
|
1075
706
|
* Schedule a sync to start when syncQueriesObservable enters sleep state
|
|
1076
707
|
* Start sync immediately if syncQueriesObservable is already in sleep state
|
|
1077
708
|
*/
|
|
1078
|
-
|
|
1079
|
-
var _this = this;
|
|
709
|
+
scheduleSync() {
|
|
1080
710
|
return (this.runningProcesses.isOpen &&
|
|
1081
|
-
this.runningProcesses.add(
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
};
|
|
1088
|
-
return SyncEngine;
|
|
1089
|
-
}());
|
|
1090
|
-
export { SyncEngine };
|
|
711
|
+
this.runningProcesses.add(() => this.waitForSleepState.then(() => {
|
|
712
|
+
// unsleepSyncQueriesObservable will be set if waitForSleepState has resolved
|
|
713
|
+
this.unsleepSyncQueriesObservable();
|
|
714
|
+
})));
|
|
715
|
+
}
|
|
716
|
+
}
|