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