cozy-pouch-link 48.25.0 → 49.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 (41) hide show
  1. package/dist/CozyPouchLink.js +593 -237
  2. package/dist/CozyPouchLink.spec.js +67 -42
  3. package/dist/PouchManager.js +317 -254
  4. package/dist/PouchManager.spec.js +91 -58
  5. package/dist/helpers.js +79 -0
  6. package/dist/helpers.spec.js +85 -1
  7. package/dist/jsonapi.js +54 -7
  8. package/dist/jsonapi.spec.js +57 -14
  9. package/dist/localStorage.js +646 -207
  10. package/dist/localStorage.spec.js +48 -0
  11. package/dist/mango.js +72 -20
  12. package/dist/mango.spec.js +1 -1
  13. package/dist/migrations/adapter.js +1 -1
  14. package/dist/platformWeb.js +120 -0
  15. package/dist/remote.js +39 -5
  16. package/dist/remote.spec.js +214 -0
  17. package/dist/replicateOnce.js +337 -0
  18. package/dist/startReplication.js +70 -45
  19. package/dist/startReplication.spec.js +374 -39
  20. package/dist/types.js +80 -0
  21. package/dist/utils.js +11 -2
  22. package/package.json +9 -5
  23. package/types/AccessToken.d.ts +16 -0
  24. package/types/CozyPouchLink.d.ts +228 -0
  25. package/types/PouchManager.d.ts +86 -0
  26. package/types/__tests__/fixtures.d.ts +48 -0
  27. package/types/__tests__/mocks.d.ts +4 -0
  28. package/types/helpers.d.ts +17 -0
  29. package/types/index.d.ts +1 -0
  30. package/types/jsonapi.d.ts +19 -0
  31. package/types/localStorage.d.ts +124 -0
  32. package/types/logger.d.ts +2 -0
  33. package/types/loop.d.ts +60 -0
  34. package/types/mango.d.ts +3 -0
  35. package/types/migrations/adapter.d.ts +18 -0
  36. package/types/platformWeb.d.ts +17 -0
  37. package/types/remote.d.ts +6 -0
  38. package/types/replicateOnce.d.ts +29 -0
  39. package/types/startReplication.d.ts +12 -0
  40. package/types/types.d.ts +104 -0
  41. package/types/utils.d.ts +3 -0
@@ -1,7 +1,5 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
4
 
7
5
  Object.defineProperty(exports, "__esModule", {
@@ -17,39 +15,29 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl
17
15
 
18
16
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
19
17
 
20
- var _pouchdbBrowser = _interopRequireDefault(require("pouchdb-browser"));
21
-
22
18
  var _fromPairs = _interopRequireDefault(require("lodash/fromPairs"));
23
19
 
24
20
  var _forEach = _interopRequireDefault(require("lodash/forEach"));
25
21
 
26
22
  var _get = _interopRequireDefault(require("lodash/get"));
27
23
 
28
- var _map = _interopRequireDefault(require("lodash/map"));
29
-
30
- var _zip = _interopRequireDefault(require("lodash/zip"));
31
-
32
- var _startsWith = _interopRequireDefault(require("lodash/startsWith"));
33
-
34
24
  var _cozyDeviceHelper = require("cozy-device-helper");
35
25
 
36
- var _cozyClient = require("cozy-client");
26
+ var _localStorage = require("./localStorage");
37
27
 
38
28
  var _loop = _interopRequireDefault(require("./loop"));
39
29
 
40
30
  var _logger = _interopRequireDefault(require("./logger"));
41
31
 
42
- var _remote = require("./remote");
43
-
44
- var _startReplication = require("./startReplication");
32
+ var _platformWeb = require("./platformWeb");
45
33
 
46
- var localStorage = _interopRequireWildcard(require("./localStorage"));
34
+ var _replicateOnce3 = require("./replicateOnce");
47
35
 
48
36
  var _utils = require("./utils");
49
37
 
50
38
  var DEFAULT_DELAY = 30 * 1000;
51
39
  /**
52
- * @param {QueryDefinition} query The query definition whose name we're getting
40
+ * @param {import('cozy-client/types/types').Query} query The query definition whose name we're getting
53
41
  *
54
42
  * @returns {string} alias
55
43
  */
@@ -67,41 +55,87 @@ var getQueryAlias = function getQueryAlias(query) {
67
55
 
68
56
  var PouchManager = /*#__PURE__*/function () {
69
57
  function PouchManager(doctypes, options) {
58
+ var _options$platform, _options$platform2, _options$platform3, _options$platform4;
59
+
70
60
  (0, _classCallCheck2.default)(this, PouchManager);
71
61
  this.options = options;
72
- var pouchPlugins = (0, _get.default)(options, 'pouch.plugins', []);
73
- var pouchOptions = (0, _get.default)(options, 'pouch.options', {});
74
- (0, _forEach.default)(pouchPlugins, function (plugin) {
75
- return _pouchdbBrowser.default.plugin(plugin);
76
- });
77
- this.pouches = (0, _fromPairs.default)(doctypes.map(function (doctype) {
78
- return [doctype, new _pouchdbBrowser.default((0, _utils.getDatabaseName)(options.prefix, doctype), pouchOptions)];
79
- }));
80
- this.syncedDoctypes = localStorage.getPersistedSyncedDoctypes();
81
- this.warmedUpQueries = localStorage.getPersistedWarmedUpQueries();
82
- this.getReplicationURL = options.getReplicationURL;
83
- this.doctypesReplicationOptions = options.doctypesReplicationOptions || {};
84
- this.listenerLaunched = false; // We must ensure databases exist on the remote before
85
- // starting replications
86
-
87
- this.ensureDatabasesExistDone = false;
88
- this.startReplicationLoop = this.startReplicationLoop.bind(this);
89
- this.stopReplicationLoop = this.stopReplicationLoop.bind(this);
90
- this.replicateOnce = this.replicateOnce.bind(this);
91
- this.executeQuery = this.options.executeQuery;
62
+ this.doctypes = doctypes;
63
+ this.storage = new _localStorage.PouchLocalStorage(((_options$platform = options.platform) === null || _options$platform === void 0 ? void 0 : _options$platform.storage) || _platformWeb.platformWeb.storage);
64
+ this.PouchDB = ((_options$platform2 = options.platform) === null || _options$platform2 === void 0 ? void 0 : _options$platform2.pouchAdapter) || _platformWeb.platformWeb.pouchAdapter;
65
+ this.isOnline = ((_options$platform3 = options.platform) === null || _options$platform3 === void 0 ? void 0 : _options$platform3.isOnline) || _platformWeb.platformWeb.isOnline;
66
+ this.events = ((_options$platform4 = options.platform) === null || _options$platform4 === void 0 ? void 0 : _options$platform4.events) || _platformWeb.platformWeb.events;
92
67
  }
93
68
 
94
69
  (0, _createClass2.default)(PouchManager, [{
70
+ key: "init",
71
+ value: function () {
72
+ var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
73
+ var _this = this;
74
+
75
+ var pouchPlugins, pouchOptions;
76
+ return _regenerator.default.wrap(function _callee$(_context) {
77
+ while (1) {
78
+ switch (_context.prev = _context.next) {
79
+ case 0:
80
+ pouchPlugins = (0, _get.default)(this.options, 'pouch.plugins', []);
81
+ pouchOptions = (0, _get.default)(this.options, 'pouch.options', {});
82
+ (0, _forEach.default)(pouchPlugins, function (plugin) {
83
+ return _this.PouchDB.plugin(plugin);
84
+ });
85
+ this.pouches = (0, _fromPairs.default)(this.doctypes.map(function (doctype) {
86
+ return [doctype, new _this.PouchDB((0, _utils.getDatabaseName)(_this.options.prefix, doctype), pouchOptions)];
87
+ }));
88
+ /** @type {Record<string, import('./types').SyncInfo>} - Stores synchronization info per doctype */
89
+
90
+ _context.next = 6;
91
+ return this.storage.getPersistedSyncedDoctypes();
92
+
93
+ case 6:
94
+ this.syncedDoctypes = _context.sent;
95
+ _context.next = 9;
96
+ return this.storage.getPersistedWarmedUpQueries();
97
+
98
+ case 9:
99
+ this.warmedUpQueries = _context.sent;
100
+ this.getReplicationURL = this.options.getReplicationURL;
101
+ this.doctypesReplicationOptions = this.options.doctypesReplicationOptions || {};
102
+ this.listenerLaunched = false; // We must ensure databases exist on the remote before
103
+ // starting replications
104
+
105
+ this.ensureDatabasesExistDone = false;
106
+ this.startReplicationLoop = this.startReplicationLoop.bind(this);
107
+ this.stopReplicationLoop = this.stopReplicationLoop.bind(this);
108
+ this.replicateOnce = this.replicateOnce.bind(this);
109
+ this.executeQuery = this.options.executeQuery;
110
+ /** @type {import('./types').CancelablePromise[]} - Stores replication promises */
111
+
112
+ this.replications = undefined;
113
+
114
+ case 19:
115
+ case "end":
116
+ return _context.stop();
117
+ }
118
+ }
119
+ }, _callee, this);
120
+ }));
121
+
122
+ function init() {
123
+ return _init.apply(this, arguments);
124
+ }
125
+
126
+ return init;
127
+ }()
128
+ }, {
95
129
  key: "addListeners",
96
130
  value: function addListeners() {
97
131
  if (!this.listenerLaunched) {
98
132
  if ((0, _cozyDeviceHelper.isMobileApp)()) {
99
- document.addEventListener('pause', this.stopReplicationLoop);
100
- document.addEventListener('resume', this.startReplicationLoop);
133
+ this.events.addEventListener('pause', this.stopReplicationLoop);
134
+ this.events.addEventListener('resume', this.startReplicationLoop);
101
135
  }
102
136
 
103
- document.addEventListener('online', this.startReplicationLoop);
104
- document.addEventListener('offline', this.stopReplicationLoop);
137
+ this.events.addEventListener('online', this.startReplicationLoop);
138
+ this.events.addEventListener('offline', this.stopReplicationLoop);
105
139
  this.listenerLaunched = true;
106
140
  }
107
141
  }
@@ -110,28 +144,59 @@ var PouchManager = /*#__PURE__*/function () {
110
144
  value: function removeListeners() {
111
145
  if (this.listenerLaunched) {
112
146
  if ((0, _cozyDeviceHelper.isMobileApp)()) {
113
- document.removeEventListener('pause', this.stopReplicationLoop);
114
- document.removeEventListener('resume', this.startReplicationLoop);
147
+ this.events.removeEventListener('pause', this.stopReplicationLoop);
148
+ this.events.removeEventListener('resume', this.startReplicationLoop);
115
149
  }
116
150
 
117
- document.removeEventListener('online', this.startReplicationLoop);
118
- document.removeEventListener('offline', this.stopReplicationLoop);
151
+ this.events.removeEventListener('online', this.startReplicationLoop);
152
+ this.events.removeEventListener('offline', this.stopReplicationLoop);
119
153
  this.listenerLaunched = false;
120
154
  }
121
155
  }
122
156
  }, {
123
157
  key: "destroy",
124
- value: function destroy() {
125
- this.stopReplicationLoop();
126
- this.removeListeners();
127
- this.clearSyncedDoctypes();
128
- this.clearWarmedUpQueries();
129
- localStorage.destroyAllDoctypeLastSequence();
130
- localStorage.destroyAllLastReplicatedDocID();
131
- return Promise.all(Object.values(this.pouches).map(function (pouch) {
132
- return pouch.destroy();
158
+ value: function () {
159
+ var _destroy = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
160
+ return _regenerator.default.wrap(function _callee2$(_context2) {
161
+ while (1) {
162
+ switch (_context2.prev = _context2.next) {
163
+ case 0:
164
+ this.stopReplicationLoop();
165
+ this.removeListeners();
166
+ _context2.next = 4;
167
+ return this.clearSyncedDoctypes();
168
+
169
+ case 4:
170
+ _context2.next = 6;
171
+ return this.clearWarmedUpQueries();
172
+
173
+ case 6:
174
+ _context2.next = 8;
175
+ return this.storage.destroyAllDoctypeLastSequence();
176
+
177
+ case 8:
178
+ _context2.next = 10;
179
+ return this.storage.destroyAllLastReplicatedDocID();
180
+
181
+ case 10:
182
+ return _context2.abrupt("return", Promise.all(Object.values(this.pouches).map(function (pouch) {
183
+ return pouch.destroy();
184
+ })));
185
+
186
+ case 11:
187
+ case "end":
188
+ return _context2.stop();
189
+ }
190
+ }
191
+ }, _callee2, this);
133
192
  }));
134
- }
193
+
194
+ function destroy() {
195
+ return _destroy.apply(this, arguments);
196
+ }
197
+
198
+ return destroy;
199
+ }()
135
200
  /**
136
201
  * Via a call to info() we ensure the database exist on the
137
202
  * remote side. This is done only once since after the first
@@ -141,35 +206,35 @@ var PouchManager = /*#__PURE__*/function () {
141
206
  }, {
142
207
  key: "ensureDatabasesExist",
143
208
  value: function () {
144
- var _ensureDatabasesExist = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
145
- var _this = this;
209
+ var _ensureDatabasesExist = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
210
+ var _this2 = this;
146
211
 
147
- return _regenerator.default.wrap(function _callee$(_context) {
212
+ return _regenerator.default.wrap(function _callee3$(_context3) {
148
213
  while (1) {
149
- switch (_context.prev = _context.next) {
214
+ switch (_context3.prev = _context3.next) {
150
215
  case 0:
151
216
  if (!this.ensureDatabasesExistDone) {
152
- _context.next = 2;
217
+ _context3.next = 2;
153
218
  break;
154
219
  }
155
220
 
156
- return _context.abrupt("return", Promise.resolve());
221
+ return _context3.abrupt("return", Promise.resolve());
157
222
 
158
223
  case 2:
159
- return _context.abrupt("return", Promise.all(Object.values(this.pouches).map(function (pouch) {
224
+ return _context3.abrupt("return", Promise.all(Object.values(this.pouches).map(function (pouch) {
160
225
  return pouch.info();
161
226
  })).then(function () {
162
227
  _logger.default.info('PouchManager: ensure databases exist done');
163
228
 
164
- _this.ensureDatabasesExistDone = true;
229
+ _this2.ensureDatabasesExistDone = true;
165
230
  }));
166
231
 
167
232
  case 3:
168
233
  case "end":
169
- return _context.stop();
234
+ return _context3.stop();
170
235
  }
171
236
  }
172
- }, _callee, this);
237
+ }, _callee3, this);
173
238
  }));
174
239
 
175
240
  function ensureDatabasesExist() {
@@ -178,29 +243,33 @@ var PouchManager = /*#__PURE__*/function () {
178
243
 
179
244
  return ensureDatabasesExist;
180
245
  }()
181
- /** Starts periodic syncing of the pouches */
246
+ /**
247
+ * Starts periodic syncing of the pouches
248
+ *
249
+ * @returns {Promise<Loop | void>}
250
+ */
182
251
 
183
252
  }, {
184
253
  key: "startReplicationLoop",
185
254
  value: function () {
186
- var _startReplicationLoop = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
255
+ var _startReplicationLoop = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
187
256
  var delay;
188
- return _regenerator.default.wrap(function _callee2$(_context2) {
257
+ return _regenerator.default.wrap(function _callee4$(_context4) {
189
258
  while (1) {
190
- switch (_context2.prev = _context2.next) {
259
+ switch (_context4.prev = _context4.next) {
191
260
  case 0:
192
- _context2.next = 2;
261
+ _context4.next = 2;
193
262
  return this.ensureDatabasesExist();
194
263
 
195
264
  case 2:
196
265
  if (!this.replicationLoop) {
197
- _context2.next = 5;
266
+ _context4.next = 5;
198
267
  break;
199
268
  }
200
269
 
201
270
  _logger.default.warn('Replication loop already started');
202
271
 
203
- return _context2.abrupt("return");
272
+ return _context4.abrupt("return");
204
273
 
205
274
  case 5:
206
275
  if (process.env.NODE_ENV !== 'production') {
@@ -211,14 +280,14 @@ var PouchManager = /*#__PURE__*/function () {
211
280
  this.replicationLoop = new _loop.default(this.replicateOnce, delay);
212
281
  this.replicationLoop.start();
213
282
  this.addListeners();
214
- return _context2.abrupt("return", this.replicationLoop);
283
+ return _context4.abrupt("return", this.replicationLoop);
215
284
 
216
285
  case 11:
217
286
  case "end":
218
- return _context2.stop();
287
+ return _context4.stop();
219
288
  }
220
289
  }
221
- }, _callee2, this);
290
+ }, _callee4, this);
222
291
  }));
223
292
 
224
293
  function startReplicationLoop() {
@@ -261,147 +330,23 @@ var PouchManager = /*#__PURE__*/function () {
261
330
  }, {
262
331
  key: "replicateOnce",
263
332
  value: function () {
264
- var _replicateOnce = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
265
- var _this2 = this;
266
-
267
- var doctypes, promises, res, doctypeUpdates;
268
- return _regenerator.default.wrap(function _callee4$(_context4) {
333
+ var _replicateOnce2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5() {
334
+ return _regenerator.default.wrap(function _callee5$(_context5) {
269
335
  while (1) {
270
- switch (_context4.prev = _context4.next) {
336
+ switch (_context5.prev = _context5.next) {
271
337
  case 0:
272
- if (window.navigator.onLine) {
273
- _context4.next = 3;
274
- break;
275
- }
276
-
277
- _logger.default.info('PouchManager: The device is offline so the replication has been skipped');
278
-
279
- return _context4.abrupt("return", Promise.resolve());
280
-
281
- case 3:
282
- _logger.default.info('PouchManager: Starting replication iteration'); // Creating each replication
283
-
284
-
285
- this.replications = (0, _map.default)(this.pouches, /*#__PURE__*/function () {
286
- var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(pouch, doctype) {
287
- var getReplicationURL, initialReplication, replicationFilter, seq, lastSeq, replicationOptions, res;
288
- return _regenerator.default.wrap(function _callee3$(_context3) {
289
- while (1) {
290
- switch (_context3.prev = _context3.next) {
291
- case 0:
292
- _logger.default.info('PouchManager: Starting replication for ' + doctype);
293
-
294
- getReplicationURL = function getReplicationURL() {
295
- return _this2.getReplicationURL(doctype);
296
- };
297
-
298
- initialReplication = !_this2.isSynced(doctype);
299
-
300
- replicationFilter = function replicationFilter(doc) {
301
- return !(0, _startsWith.default)(doc._id, '_design');
302
- };
303
-
304
- seq = '';
305
-
306
- if (!initialReplication) {
307
- _context3.next = 12;
308
- break;
309
- }
310
-
311
- _context3.next = 8;
312
- return (0, _remote.fetchRemoteLastSequence)(getReplicationURL());
313
-
314
- case 8:
315
- lastSeq = _context3.sent;
316
- localStorage.persistDoctypeLastSequence(doctype, lastSeq);
317
- _context3.next = 13;
318
- break;
319
-
320
- case 12:
321
- seq = localStorage.getDoctypeLastSequence(doctype);
322
-
323
- case 13:
324
- replicationOptions = (0, _get.default)(_this2.doctypesReplicationOptions, doctype, {});
325
- replicationOptions.initialReplication = initialReplication;
326
- replicationOptions.filter = replicationFilter;
327
- replicationOptions.since = seq;
328
- replicationOptions.doctype = doctype;
329
-
330
- if (_this2.options.onDoctypeSyncStart) {
331
- _this2.options.onDoctypeSyncStart(doctype);
332
- }
333
-
334
- _context3.next = 21;
335
- return (0, _startReplication.startReplication)(pouch, replicationOptions, getReplicationURL);
336
-
337
- case 21:
338
- res = _context3.sent;
339
-
340
- if (seq) {
341
- // We only need the sequence for the second replication, as PouchDB
342
- // will use a local checkpoint for the next runs.
343
- localStorage.destroyDoctypeLastSequence(doctype);
344
- }
345
-
346
- _this2.updateSyncInfo(doctype);
347
-
348
- _this2.checkToWarmupDoctype(doctype, replicationOptions);
338
+ return _context5.abrupt("return", (0, _replicateOnce3.replicateOnce)(this));
349
339
 
350
- if (_this2.options.onDoctypeSyncEnd) {
351
- _this2.options.onDoctypeSyncEnd(doctype);
352
- }
353
-
354
- return _context3.abrupt("return", res);
355
-
356
- case 27:
357
- case "end":
358
- return _context3.stop();
359
- }
360
- }
361
- }, _callee3);
362
- }));
363
-
364
- return function (_x, _x2) {
365
- return _ref.apply(this, arguments);
366
- };
367
- }()); // Waiting on each replication
368
-
369
- doctypes = Object.keys(this.pouches);
370
- promises = Object.values(this.replications);
371
- _context4.prev = 7;
372
- _context4.next = 10;
373
- return Promise.all(promises);
374
-
375
- case 10:
376
- res = _context4.sent;
377
-
378
- if (process.env.NODE_ENV !== 'production') {
379
- _logger.default.info('PouchManager: Replication ended');
380
- }
381
-
382
- if (this.options.onSync) {
383
- doctypeUpdates = (0, _fromPairs.default)((0, _zip.default)(doctypes, res));
384
- this.options.onSync(doctypeUpdates);
385
- }
386
-
387
- res.cancel = this.cancelCurrentReplications;
388
- return _context4.abrupt("return", res);
389
-
390
- case 17:
391
- _context4.prev = 17;
392
- _context4.t0 = _context4["catch"](7);
393
- this.handleReplicationError(_context4.t0);
394
-
395
- case 20:
340
+ case 1:
396
341
  case "end":
397
- return _context4.stop();
342
+ return _context5.stop();
398
343
  }
399
344
  }
400
- }, _callee4, this, [[7, 17]]);
345
+ }, _callee5, this);
401
346
  }));
402
347
 
403
348
  function replicateOnce() {
404
- return _replicateOnce.apply(this, arguments);
349
+ return _replicateOnce2.apply(this, arguments);
405
350
  }
406
351
 
407
352
  return replicateOnce;
@@ -409,7 +354,14 @@ var PouchManager = /*#__PURE__*/function () {
409
354
  }, {
410
355
  key: "handleReplicationError",
411
356
  value: function handleReplicationError(err) {
412
- _logger.default.warn('PouchManager: Error during replication', err); // On error, replication stops, it needs to be started
357
+ var aggregatedMessage = ''; // @ts-ignore
358
+ // eslint-disable-next-line no-undef
359
+
360
+ if (err instanceof AggregateError) {
361
+ aggregatedMessage = (0, _utils.formatAggregatedError)(err);
362
+ }
363
+
364
+ _logger.default.warn("PouchManager: Error during replication - ".concat(err.message).concat(aggregatedMessage)); // On error, replication stops, it needs to be started
413
365
  // again manually by the owner of PouchManager
414
366
 
415
367
 
@@ -446,59 +398,124 @@ var PouchManager = /*#__PURE__*/function () {
446
398
  value: function getPouch(doctype) {
447
399
  return this.pouches[doctype];
448
400
  }
401
+ /**
402
+ * Update the Sync info for the specifed doctype
403
+ *
404
+ * @param {string} doctype - The doctype to update
405
+ * @param {import('./types').SyncStatus} status - The new Sync status for the doctype
406
+ */
407
+
449
408
  }, {
450
409
  key: "updateSyncInfo",
451
- value: function updateSyncInfo(doctype) {
452
- this.syncedDoctypes[doctype] = {
453
- date: new Date().toISOString()
454
- };
455
- localStorage.persistSyncedDoctypes(this.syncedDoctypes);
456
- }
410
+ value: function () {
411
+ var _updateSyncInfo = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(doctype) {
412
+ var status,
413
+ _args6 = arguments;
414
+ return _regenerator.default.wrap(function _callee6$(_context6) {
415
+ while (1) {
416
+ switch (_context6.prev = _context6.next) {
417
+ case 0:
418
+ status = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : 'synced';
419
+ this.syncedDoctypes[doctype] = {
420
+ date: new Date().toISOString(),
421
+ status: status
422
+ };
423
+ _context6.next = 4;
424
+ return this.storage.persistSyncedDoctypes(this.syncedDoctypes);
425
+
426
+ case 4:
427
+ case "end":
428
+ return _context6.stop();
429
+ }
430
+ }
431
+ }, _callee6, this);
432
+ }));
433
+
434
+ function updateSyncInfo(_x) {
435
+ return _updateSyncInfo.apply(this, arguments);
436
+ }
437
+
438
+ return updateSyncInfo;
439
+ }()
440
+ /**
441
+ * Get the Sync info for the specified doctype
442
+ *
443
+ * @param {string} doctype - The doctype to check
444
+ * @returns {import('./types').SyncInfo}
445
+ */
446
+
457
447
  }, {
458
448
  key: "getSyncInfo",
459
449
  value: function getSyncInfo(doctype) {
460
450
  return this.syncedDoctypes && this.syncedDoctypes[doctype];
461
451
  }
452
+ /**
453
+ * Get the Sync status for the specified doctype
454
+ *
455
+ * @param {string} doctype - The doctype to check
456
+ * @returns {import('./types').SyncStatus}
457
+ */
458
+
462
459
  }, {
463
- key: "isSynced",
464
- value: function isSynced(doctype) {
460
+ key: "getSyncStatus",
461
+ value: function getSyncStatus(doctype) {
465
462
  var info = this.getSyncInfo(doctype);
466
- return info ? !!info.date : false;
463
+ return (info === null || info === void 0 ? void 0 : info.status) || 'not_synced';
467
464
  }
468
465
  }, {
469
466
  key: "clearSyncedDoctypes",
470
- value: function clearSyncedDoctypes() {
471
- this.syncedDoctypes = {};
472
- localStorage.destroySyncedDoctypes();
473
- }
467
+ value: function () {
468
+ var _clearSyncedDoctypes = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7() {
469
+ return _regenerator.default.wrap(function _callee7$(_context7) {
470
+ while (1) {
471
+ switch (_context7.prev = _context7.next) {
472
+ case 0:
473
+ this.syncedDoctypes = {};
474
+ _context7.next = 3;
475
+ return this.storage.destroySyncedDoctypes();
476
+
477
+ case 3:
478
+ case "end":
479
+ return _context7.stop();
480
+ }
481
+ }
482
+ }, _callee7, this);
483
+ }));
484
+
485
+ function clearSyncedDoctypes() {
486
+ return _clearSyncedDoctypes.apply(this, arguments);
487
+ }
488
+
489
+ return clearSyncedDoctypes;
490
+ }()
474
491
  }, {
475
492
  key: "warmupQueries",
476
493
  value: function () {
477
- var _warmupQueries = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(doctype, queries) {
494
+ var _warmupQueries = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(doctype, queries) {
478
495
  var _this3 = this;
479
496
 
480
- return _regenerator.default.wrap(function _callee6$(_context6) {
497
+ return _regenerator.default.wrap(function _callee9$(_context9) {
481
498
  while (1) {
482
- switch (_context6.prev = _context6.next) {
499
+ switch (_context9.prev = _context9.next) {
483
500
  case 0:
484
501
  if (!this.warmedUpQueries[doctype]) this.warmedUpQueries[doctype] = [];
485
- _context6.prev = 1;
486
- _context6.next = 4;
502
+ _context9.prev = 1;
503
+ _context9.next = 4;
487
504
  return Promise.all(queries.map( /*#__PURE__*/function () {
488
- var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(query) {
505
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8(query) {
489
506
  var def;
490
- return _regenerator.default.wrap(function _callee5$(_context5) {
507
+ return _regenerator.default.wrap(function _callee8$(_context8) {
491
508
  while (1) {
492
- switch (_context5.prev = _context5.next) {
509
+ switch (_context8.prev = _context8.next) {
493
510
  case 0:
494
511
  def = getQueryAlias(query);
495
512
 
496
513
  if (_this3.warmedUpQueries[doctype].includes(def)) {
497
- _context5.next = 5;
514
+ _context8.next = 5;
498
515
  break;
499
516
  }
500
517
 
501
- _context5.next = 4;
518
+ _context8.next = 4;
502
519
  return _this3.executeQuery(query.definition().toDefinition());
503
520
 
504
521
  case 4:
@@ -506,42 +523,44 @@ var PouchManager = /*#__PURE__*/function () {
506
523
 
507
524
  case 5:
508
525
  case "end":
509
- return _context5.stop();
526
+ return _context8.stop();
510
527
  }
511
528
  }
512
- }, _callee5);
529
+ }, _callee8);
513
530
  }));
514
531
 
515
- return function (_x5) {
516
- return _ref2.apply(this, arguments);
532
+ return function (_x4) {
533
+ return _ref.apply(this, arguments);
517
534
  };
518
535
  }()));
519
536
 
520
537
  case 4:
521
- localStorage.persistWarmedUpQueries(this.warmedUpQueries);
538
+ _context9.next = 6;
539
+ return this.storage.persistWarmedUpQueries(this.warmedUpQueries);
522
540
 
541
+ case 6:
523
542
  _logger.default.log('PouchManager: warmupQueries for ' + doctype + ' are done');
524
543
 
525
- _context6.next = 12;
544
+ _context9.next = 13;
526
545
  break;
527
546
 
528
- case 8:
529
- _context6.prev = 8;
530
- _context6.t0 = _context6["catch"](1);
547
+ case 9:
548
+ _context9.prev = 9;
549
+ _context9.t0 = _context9["catch"](1);
531
550
 
532
- _logger.default.error('PouchManager: Could not warm up queries for ' + doctype, _context6.t0);
551
+ _logger.default.error('PouchManager: Could not warm up queries for ' + doctype, _context9.t0);
533
552
 
534
553
  delete this.warmedUpQueries[doctype];
535
554
 
536
- case 12:
555
+ case 13:
537
556
  case "end":
538
- return _context6.stop();
557
+ return _context9.stop();
539
558
  }
540
559
  }
541
- }, _callee6, this, [[1, 8]]);
560
+ }, _callee9, this, [[1, 9]]);
542
561
  }));
543
562
 
544
- function warmupQueries(_x3, _x4) {
563
+ function warmupQueries(_x2, _x3) {
545
564
  return _warmupQueries.apply(this, arguments);
546
565
  }
547
566
 
@@ -559,18 +578,62 @@ var PouchManager = /*#__PURE__*/function () {
559
578
  }
560
579
  }, {
561
580
  key: "areQueriesWarmedUp",
562
- value: function areQueriesWarmedUp(doctype, queries) {
563
- var persistWarmedUpQueries = localStorage.getPersistedWarmedUpQueries();
564
- return queries.every(function (query) {
565
- return persistWarmedUpQueries[doctype] && persistWarmedUpQueries[doctype].includes(getQueryAlias(query));
566
- });
567
- }
581
+ value: function () {
582
+ var _areQueriesWarmedUp = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10(doctype, queries) {
583
+ var persistWarmedUpQueries;
584
+ return _regenerator.default.wrap(function _callee10$(_context10) {
585
+ while (1) {
586
+ switch (_context10.prev = _context10.next) {
587
+ case 0:
588
+ _context10.next = 2;
589
+ return this.storage.getPersistedWarmedUpQueries();
590
+
591
+ case 2:
592
+ persistWarmedUpQueries = _context10.sent;
593
+ return _context10.abrupt("return", queries.every(function (query) {
594
+ return persistWarmedUpQueries[doctype] && persistWarmedUpQueries[doctype].includes(getQueryAlias(query));
595
+ }));
596
+
597
+ case 4:
598
+ case "end":
599
+ return _context10.stop();
600
+ }
601
+ }
602
+ }, _callee10, this);
603
+ }));
604
+
605
+ function areQueriesWarmedUp(_x5, _x6) {
606
+ return _areQueriesWarmedUp.apply(this, arguments);
607
+ }
608
+
609
+ return areQueriesWarmedUp;
610
+ }()
568
611
  }, {
569
612
  key: "clearWarmedUpQueries",
570
- value: function clearWarmedUpQueries() {
571
- this.warmedUpQueries = {};
572
- localStorage.destroyWarmedUpQueries();
573
- }
613
+ value: function () {
614
+ var _clearWarmedUpQueries = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11() {
615
+ return _regenerator.default.wrap(function _callee11$(_context11) {
616
+ while (1) {
617
+ switch (_context11.prev = _context11.next) {
618
+ case 0:
619
+ this.warmedUpQueries = {};
620
+ _context11.next = 3;
621
+ return this.storage.destroyWarmedUpQueries();
622
+
623
+ case 3:
624
+ case "end":
625
+ return _context11.stop();
626
+ }
627
+ }
628
+ }, _callee11, this);
629
+ }));
630
+
631
+ function clearWarmedUpQueries() {
632
+ return _clearWarmedUpQueries.apply(this, arguments);
633
+ }
634
+
635
+ return clearWarmedUpQueries;
636
+ }()
574
637
  }]);
575
638
  return PouchManager;
576
639
  }();