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