cozy-pouch-link 50.1.0 → 50.3.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.
@@ -43,6 +43,8 @@ var _zipWith = _interopRequireDefault(require("lodash/zipWith"));
43
43
 
44
44
  var _get = _interopRequireDefault(require("lodash/get"));
45
45
 
46
+ var _debounce = _interopRequireDefault(require("lodash/debounce"));
47
+
46
48
  var _helpers = _interopRequireDefault(require("./helpers"));
47
49
 
48
50
  var _mango = require("./mango");
@@ -92,6 +94,8 @@ var parseMutationResult = function parseMutationResult(original, res) {
92
94
  var DEFAULT_OPTIONS = {
93
95
  replicationInterval: 30 * 1000
94
96
  };
97
+ var DEFAULT_DEBOUNCE_DELAY = 10 * 1000;
98
+ var MAX_DEBOUNCE_DELAY = 600 * 1000;
95
99
 
96
100
  var addBasicAuth = function addBasicAuth(url, basicAuth) {
97
101
  return url.replace('//', "//".concat(basicAuth));
@@ -138,6 +142,8 @@ var normalizeAll = function normalizeAll(client) {
138
142
  * @typedef {object} PouchLinkOptions
139
143
  * @property {boolean} initialSync Whether or not a replication process should be started. Default is false
140
144
  * @property {boolean} periodicSync Whether or not the replication should be periodic. Default is true
145
+ * @property {number} [syncDebounceDelayInMs] Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds
146
+ * @property {number} [syncDebounceMaxDelayInMs] The maximum duration (in ms) the `startReplicationWithDebounce()` method can be delayed. Should be used only when periodicSync is false. Default is 10 minutes
141
147
  * @property {number} [replicationInterval] Milliseconds between periodic replications
142
148
  * @property {string[]} doctypes Doctypes to replicate
143
149
  * @property {Record<string, object>} doctypesReplicationOptions A mapping from doctypes to replication options. All pouch replication options can be used, as well as the "strategy" option that determines which way the replication is done (can be "sync", "fromRemote" or "toRemote")
@@ -172,7 +178,9 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
172
178
  var doctypes = options.doctypes,
173
179
  doctypesReplicationOptions = options.doctypesReplicationOptions,
174
180
  periodicSync = options.periodicSync,
175
- initialSync = options.initialSync;
181
+ initialSync = options.initialSync,
182
+ syncDebounceDelayInMs = options.syncDebounceDelayInMs,
183
+ syncDebounceMaxDelayInMs = options.syncDebounceMaxDelayInMs;
176
184
  _this.options = options;
177
185
 
178
186
  if (!doctypes) {
@@ -188,6 +196,11 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
188
196
  /** @type {Record<string, ReplicationStatus>} - Stores replication states per doctype */
189
197
 
190
198
  _this.replicationStatus = _this.replicationStatus || {};
199
+ /** @private */
200
+
201
+ _this.startReplicationDebounced = (0, _debounce.default)(_this._startReplication, syncDebounceDelayInMs || DEFAULT_DEBOUNCE_DELAY, {
202
+ maxWait: syncDebounceMaxDelayInMs || MAX_DEBOUNCE_DELAY
203
+ });
191
204
  return _this;
192
205
  }
193
206
  /**
@@ -519,6 +532,25 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
519
532
  this.replicationStatus[doctype] = 'idle';
520
533
  this.client.emit('pouchlink:doctypesync:end', doctype);
521
534
  }
535
+ /**
536
+ * @private
537
+ */
538
+
539
+ }, {
540
+ key: "_startReplication",
541
+ value: function _startReplication() {
542
+ this.client.emit('pouchlink:sync:start');
543
+
544
+ if (this.periodicSync) {
545
+ this.pouches.startReplicationLoop();
546
+ } else {
547
+ this.pouches.replicateOnce();
548
+ }
549
+
550
+ if (this.options.onStartReplication) {
551
+ this.options.onStartReplication.apply(this);
552
+ }
553
+ }
522
554
  /**
523
555
  * User of the link can call this to start ongoing replications.
524
556
  * Typically, it can be used when the application regains focus.
@@ -532,17 +564,26 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
532
564
  }, {
533
565
  key: "startReplication",
534
566
  value: function startReplication() {
535
- this.client.emit('pouchlink:sync:start');
567
+ this.startReplicationDebounced.cancel();
568
+ return this._startReplication();
569
+ }
570
+ /**
571
+ * Debounced version of startReplication() method
572
+ *
573
+ * Debounce delay can be configured through constructor's `syncDebounceDelayInMs` option
574
+ *
575
+ * @public
576
+ * @returns {void}
577
+ */
536
578
 
579
+ }, {
580
+ key: "startReplicationWithDebounce",
581
+ value: function startReplicationWithDebounce() {
537
582
  if (this.periodicSync) {
538
- this.pouches.startReplicationLoop();
539
- } else {
540
- this.pouches.replicateOnce();
583
+ throw new Error('createDebounceableReplication cannot be called when periodic sync is configured');
541
584
  }
542
585
 
543
- if (this.options.onStartReplication) {
544
- this.options.onStartReplication.apply(this);
545
- }
586
+ return this.startReplicationDebounced();
546
587
  }
547
588
  /**
548
589
  * User of the link can call this to stop ongoing replications.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-pouch-link",
3
- "version": "50.1.0",
3
+ "version": "50.3.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  "url": "git+https://github.com/cozy/cozy-client.git"
14
14
  },
15
15
  "dependencies": {
16
- "cozy-client": "^50.1.0",
16
+ "cozy-client": "^50.2.0",
17
17
  "pouchdb-browser": "^7.2.2",
18
18
  "pouchdb-find": "^7.2.2"
19
19
  },
@@ -39,5 +39,5 @@
39
39
  "typecheck": "tsc -p tsconfig.json"
40
40
  },
41
41
  "sideEffects": false,
42
- "gitHead": "eabd164250195c878b7f40fb22c41e8b89503ea2"
42
+ "gitHead": "6681e9a57350fd3480c1b9167f3dad5988c12456"
43
43
  }
@@ -12,6 +12,14 @@ export type PouchLinkOptions = {
12
12
  * Whether or not the replication should be periodic. Default is true
13
13
  */
14
14
  periodicSync: boolean;
15
+ /**
16
+ * Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds
17
+ */
18
+ syncDebounceDelayInMs?: number;
19
+ /**
20
+ * The maximum duration (in ms) the `startReplicationWithDebounce()` method can be delayed. Should be used only when periodicSync is false. Default is 10 minutes
21
+ */
22
+ syncDebounceMaxDelayInMs?: number;
15
23
  /**
16
24
  * Milliseconds between periodic replications
17
25
  */
@@ -38,6 +46,8 @@ export type PouchLinkOptions = {
38
46
  * @typedef {object} PouchLinkOptions
39
47
  * @property {boolean} initialSync Whether or not a replication process should be started. Default is false
40
48
  * @property {boolean} periodicSync Whether or not the replication should be periodic. Default is true
49
+ * @property {number} [syncDebounceDelayInMs] Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds
50
+ * @property {number} [syncDebounceMaxDelayInMs] The maximum duration (in ms) the `startReplicationWithDebounce()` method can be delayed. Should be used only when periodicSync is false. Default is 10 minutes
41
51
  * @property {number} [replicationInterval] Milliseconds between periodic replications
42
52
  * @property {string[]} doctypes Doctypes to replicate
43
53
  * @property {Record<string, object>} doctypesReplicationOptions A mapping from doctypes to replication options. All pouch replication options can be used, as well as the "strategy" option that determines which way the replication is done (can be "sync", "fromRemote" or "toRemote")
@@ -74,6 +84,8 @@ declare class PouchLink extends CozyLink {
74
84
  periodicSync: boolean;
75
85
  /** @type {Record<string, ReplicationStatus>} - Stores replication states per doctype */
76
86
  replicationStatus: Record<string, ReplicationStatus>;
87
+ /** @private */
88
+ private startReplicationDebounced;
77
89
  getReplicationURL(doctype: any): string;
78
90
  registerClient(client: any): Promise<void>;
79
91
  client: any;
@@ -118,6 +130,10 @@ declare class PouchLink extends CozyLink {
118
130
  handleOnSync(doctypeUpdates: any): void;
119
131
  handleDoctypeSyncStart(doctype: any): void;
120
132
  handleDoctypeSyncEnd(doctype: any): void;
133
+ /**
134
+ * @private
135
+ */
136
+ private _startReplication;
121
137
  /**
122
138
  * User of the link can call this to start ongoing replications.
123
139
  * Typically, it can be used when the application regains focus.
@@ -128,6 +144,15 @@ declare class PouchLink extends CozyLink {
128
144
  * @returns {void}
129
145
  */
130
146
  public startReplication(): void;
147
+ /**
148
+ * Debounced version of startReplication() method
149
+ *
150
+ * Debounce delay can be configured through constructor's `syncDebounceDelayInMs` option
151
+ *
152
+ * @public
153
+ * @returns {void}
154
+ */
155
+ public startReplicationWithDebounce(): void;
131
156
  /**
132
157
  * User of the link can call this to stop ongoing replications.
133
158
  * Typically, it can be used when the applications loses focus.