From 5e26fc6be8e7ec34743a6104b49c5bb1dda8aa19 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Mon, 4 Nov 2024 18:18:22 +0100 Subject: [PATCH 1/2] feat(cozy-pouch-link): Add `startReplicationWithDebounce()` method We want to be able to debounce calls to `startReplication()` This commit adds the `startReplicationWithDebounce()` method that is responsible to debounce the call of `startReplication()` by a specific delay The debounce delay can be configured using the CozyPouchLink constructor's `syncDebounceDelayInMs` option Calling the `startReplication()` will cancel debounced calls and immediately execute the replication --- docs/api/cozy-pouch-link/classes/PouchLink.md | 106 ++++++++++-------- packages/cozy-pouch-link/src/CozyPouchLink.js | 53 +++++++-- .../cozy-pouch-link/types/CozyPouchLink.d.ts | 20 ++++ 3 files changed, 127 insertions(+), 52 deletions(-) diff --git a/docs/api/cozy-pouch-link/classes/PouchLink.md b/docs/api/cozy-pouch-link/classes/PouchLink.md index 21a9f8c09..6922075d6 100644 --- a/docs/api/cozy-pouch-link/classes/PouchLink.md +++ b/docs/api/cozy-pouch-link/classes/PouchLink.md @@ -32,7 +32,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:89](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L89) +[CozyPouchLink.js:93](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L93) ## Properties @@ -42,7 +42,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:149](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L149) +[CozyPouchLink.js:160](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L160) *** @@ -52,7 +52,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:104](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L104) +[CozyPouchLink.js:109](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L109) *** @@ -62,7 +62,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:105](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L105) +[CozyPouchLink.js:110](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L110) *** @@ -72,7 +72,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:106](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L106) +[CozyPouchLink.js:111](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L111) *** @@ -82,7 +82,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:110](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L110) +[CozyPouchLink.js:115](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L115) *** @@ -92,7 +92,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:98](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L98) +[CozyPouchLink.js:103](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L103) *** @@ -102,7 +102,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:111](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L111) +[CozyPouchLink.js:116](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L116) *** @@ -112,7 +112,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:219](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L219) +[CozyPouchLink.js:230](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L230) *** @@ -122,7 +122,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:114](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L114) +[CozyPouchLink.js:119](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L119) *** @@ -132,7 +132,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:107](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L107) +[CozyPouchLink.js:112](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L112) ## Methods @@ -152,7 +152,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:746](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L746) +[CozyPouchLink.js:783](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L783) *** @@ -172,7 +172,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:707](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L707) +[CozyPouchLink.js:744](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L744) *** @@ -198,7 +198,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:538](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L538) +[CozyPouchLink.js:575](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L575) *** @@ -219,7 +219,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:750](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L750) +[CozyPouchLink.js:787](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L787) *** @@ -239,7 +239,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:735](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L735) +[CozyPouchLink.js:772](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L772) *** @@ -261,7 +261,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:677](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L677) +[CozyPouchLink.js:714](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L714) *** @@ -281,7 +281,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:615](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L615) +[CozyPouchLink.js:652](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L652) *** @@ -305,7 +305,7 @@ Retrieve the PouchDB index if exist, undefined otherwise *Defined in* -[CozyPouchLink.js:562](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L562) +[CozyPouchLink.js:599](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L599) *** @@ -331,7 +331,7 @@ The changes *Defined in* -[CozyPouchLink.js:415](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L415) +[CozyPouchLink.js:452](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L452) *** @@ -356,7 +356,7 @@ The db info *Defined in* -[CozyPouchLink.js:430](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L430) +[CozyPouchLink.js:467](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L467) *** @@ -376,7 +376,7 @@ The db info *Defined in* -[CozyPouchLink.js:340](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L340) +[CozyPouchLink.js:377](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L377) *** @@ -396,7 +396,7 @@ The db info *Defined in* -[CozyPouchLink.js:129](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L129) +[CozyPouchLink.js:140](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L140) *** @@ -416,7 +416,7 @@ The db info *Defined in* -[CozyPouchLink.js:336](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L336) +[CozyPouchLink.js:373](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L373) *** @@ -436,7 +436,7 @@ The db info *Defined in* -[CozyPouchLink.js:273](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L273) +[CozyPouchLink.js:284](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L284) *** @@ -456,7 +456,7 @@ The db info *Defined in* -[CozyPouchLink.js:268](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L268) +[CozyPouchLink.js:279](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L279) *** @@ -482,7 +482,7 @@ Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done *Defined in* -[CozyPouchLink.js:254](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L254) +[CozyPouchLink.js:265](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L265) *** @@ -502,7 +502,7 @@ Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done *Defined in* -[CozyPouchLink.js:524](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L524) +[CozyPouchLink.js:561](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L561) *** @@ -532,7 +532,7 @@ Migrate the current adapter *Defined in* -[CozyPouchLink.js:163](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L163) +[CozyPouchLink.js:174](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L174) *** @@ -557,7 +557,7 @@ the need to wait for the warmup *Defined in* -[CozyPouchLink.js:510](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L510) +[CozyPouchLink.js:547](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L547) *** @@ -571,7 +571,7 @@ the need to wait for the warmup *Defined in* -[CozyPouchLink.js:182](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L182) +[CozyPouchLink.js:193](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L193) *** @@ -591,7 +591,7 @@ the need to wait for the warmup *Defined in* -[CozyPouchLink.js:316](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L316) +[CozyPouchLink.js:353](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L353) *** @@ -616,7 +616,7 @@ CozyLink.persistCozyData *Defined in* -[CozyPouchLink.js:465](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L465) +[CozyPouchLink.js:502](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L502) *** @@ -636,7 +636,7 @@ CozyLink.persistCozyData *Defined in* -[CozyPouchLink.js:148](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L148) +[CozyPouchLink.js:159](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L159) *** @@ -662,7 +662,7 @@ CozyLink.request *Defined in* -[CozyPouchLink.js:359](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L359) +[CozyPouchLink.js:396](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L396) *** @@ -680,7 +680,7 @@ CozyLink.reset *Defined in* -[CozyPouchLink.js:238](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L238) +[CozyPouchLink.js:249](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L249) *** @@ -700,7 +700,7 @@ CozyLink.reset *Defined in* -[CozyPouchLink.js:438](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L438) +[CozyPouchLink.js:475](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L475) *** @@ -719,7 +719,25 @@ Emits pouchlink:sync:start event when the replication begins *Defined in* -[CozyPouchLink.js:287](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L287) +[CozyPouchLink.js:313](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L313) + +*** + +### startReplicationWithDebounce + +▸ **startReplicationWithDebounce**(): `void` + +Debounced version of startReplication() method + +Debounce delay can be configured through constructor's `syncDebounceDelayInMs` option + +*Returns* + +`void` + +*Defined in* + +[CozyPouchLink.js:326](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L326) *** @@ -738,7 +756,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:308](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L308) +[CozyPouchLink.js:345](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L345) *** @@ -758,7 +776,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:344](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L344) +[CozyPouchLink.js:381](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L381) *** @@ -772,7 +790,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:772](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L772) +[CozyPouchLink.js:809](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L809) *** @@ -792,7 +810,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:712](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L712) +[CozyPouchLink.js:749](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L749) *** @@ -812,7 +830,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:717](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L717) +[CozyPouchLink.js:754](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L754) *** @@ -837,4 +855,4 @@ The adapter name *Defined in* -[CozyPouchLink.js:124](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L124) +[CozyPouchLink.js:135](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L135) diff --git a/packages/cozy-pouch-link/src/CozyPouchLink.js b/packages/cozy-pouch-link/src/CozyPouchLink.js index 3685ce731..321e47f4d 100644 --- a/packages/cozy-pouch-link/src/CozyPouchLink.js +++ b/packages/cozy-pouch-link/src/CozyPouchLink.js @@ -11,6 +11,7 @@ import defaults from 'lodash/defaults' import mapValues from 'lodash/mapValues' import zipWith from 'lodash/zipWith' import get from 'lodash/get' +import debounce from 'lodash/debounce' import { default as helpers } from './helpers' import { getIndexNameFromFields, getIndexFields } from './mango' @@ -37,6 +38,8 @@ const DEFAULT_OPTIONS = { replicationInterval: 30 * 1000 } +const DEFAULT_DEBOUNCE_DELAY = 10 * 1000 + const addBasicAuth = (url, basicAuth) => { return url.replace('//', `//${basicAuth}`) } @@ -68,6 +71,7 @@ const normalizeAll = client => (docs, doctype) => { * @typedef {object} PouchLinkOptions * @property {boolean} initialSync Whether or not a replication process should be started. Default is false * @property {boolean} periodicSync Whether or not the replication should be periodic. Default is true + * @property {number} [syncDebounceDelayInMs] Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds * @property {number} [replicationInterval] Milliseconds between periodic replications * @property {string[]} doctypes Doctypes to replicate * @property {Record} 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") @@ -93,7 +97,8 @@ class PouchLink extends CozyLink { doctypes, doctypesReplicationOptions, periodicSync, - initialSync + initialSync, + syncDebounceDelayInMs } = options this.options = options if (!doctypes) { @@ -112,6 +117,12 @@ class PouchLink extends CozyLink { /** @type {Record} - Stores replication states per doctype */ this.replicationStatus = this.replicationStatus || {} + + /** @private */ + this.startReplicationDebounced = debounce( + this._startReplication, + syncDebounceDelayInMs || DEFAULT_DEBOUNCE_DELAY + ) } /** @@ -275,6 +286,21 @@ class PouchLink extends CozyLink { this.client.emit('pouchlink:doctypesync:end', doctype) } + /** + * @private + */ + _startReplication() { + this.client.emit('pouchlink:sync:start') + if (this.periodicSync) { + this.pouches.startReplicationLoop() + } else { + this.pouches.replicateOnce() + } + if (this.options.onStartReplication) { + this.options.onStartReplication.apply(this) + } + } + /** * User of the link can call this to start ongoing replications. * Typically, it can be used when the application regains focus. @@ -285,15 +311,26 @@ class PouchLink extends CozyLink { * @returns {void} */ startReplication() { - this.client.emit('pouchlink:sync:start') + this.startReplicationDebounced.cancel() + return this._startReplication() + } + + /** + * Debounced version of startReplication() method + * + * Debounce delay can be configured through constructor's `syncDebounceDelayInMs` option + * + * @public + * @returns {void} + */ + startReplicationWithDebounce() { if (this.periodicSync) { - this.pouches.startReplicationLoop() - } else { - this.pouches.replicateOnce() - } - if (this.options.onStartReplication) { - this.options.onStartReplication.apply(this) + throw new Error( + 'createDebounceableReplication cannot be called when periodic sync is configured' + ) } + + return this.startReplicationDebounced() } /** diff --git a/packages/cozy-pouch-link/types/CozyPouchLink.d.ts b/packages/cozy-pouch-link/types/CozyPouchLink.d.ts index ea909ee96..0583a99c6 100644 --- a/packages/cozy-pouch-link/types/CozyPouchLink.d.ts +++ b/packages/cozy-pouch-link/types/CozyPouchLink.d.ts @@ -12,6 +12,10 @@ export type PouchLinkOptions = { * Whether or not the replication should be periodic. Default is true */ periodicSync: boolean; + /** + * Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds + */ + syncDebounceDelayInMs?: number; /** * Milliseconds between periodic replications */ @@ -38,6 +42,7 @@ export type PouchLinkOptions = { * @typedef {object} PouchLinkOptions * @property {boolean} initialSync Whether or not a replication process should be started. Default is false * @property {boolean} periodicSync Whether or not the replication should be periodic. Default is true + * @property {number} [syncDebounceDelayInMs] Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds * @property {number} [replicationInterval] Milliseconds between periodic replications * @property {string[]} doctypes Doctypes to replicate * @property {Record} 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 +79,8 @@ declare class PouchLink extends CozyLink { periodicSync: boolean; /** @type {Record} - Stores replication states per doctype */ replicationStatus: Record; + /** @private */ + private startReplicationDebounced; getReplicationURL(doctype: any): string; registerClient(client: any): Promise; client: any; @@ -118,6 +125,10 @@ declare class PouchLink extends CozyLink { handleOnSync(doctypeUpdates: any): void; handleDoctypeSyncStart(doctype: any): void; handleDoctypeSyncEnd(doctype: any): void; + /** + * @private + */ + private _startReplication; /** * User of the link can call this to start ongoing replications. * Typically, it can be used when the application regains focus. @@ -128,6 +139,15 @@ declare class PouchLink extends CozyLink { * @returns {void} */ public startReplication(): void; + /** + * Debounced version of startReplication() method + * + * Debounce delay can be configured through constructor's `syncDebounceDelayInMs` option + * + * @public + * @returns {void} + */ + public startReplicationWithDebounce(): void; /** * User of the link can call this to stop ongoing replications. * Typically, it can be used when the applications loses focus. From 0c9557caf7dd9f1357f15aacaf2484ba1a81edbc Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Tue, 5 Nov 2024 16:05:24 +0100 Subject: [PATCH 2/2] feat(cozy-pouch-link): Add a `syncDebounceMaxDelayInMs` parameter In previous commit we added the possibility to debounce the `startReplication()` method and to configure the debounce delay with `syncDebounceDelayInMs` parameter This may be problematic as we expect to call this from realtime events that may, in some rare scenario, occur too often to trigger a single replication due to the debounce To prevent this we add the `syncDebounceMaxDelayInMs` parameter that will ensure that `startReplication()` is called when the max delay is reached even if realtime events trigger the debounce for too long --- docs/api/cozy-pouch-link/classes/PouchLink.md | 90 +++++++++---------- packages/cozy-pouch-link/src/CozyPouchLink.js | 10 ++- .../cozy-pouch-link/types/CozyPouchLink.d.ts | 5 ++ 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/docs/api/cozy-pouch-link/classes/PouchLink.md b/docs/api/cozy-pouch-link/classes/PouchLink.md index 6922075d6..ff46af62e 100644 --- a/docs/api/cozy-pouch-link/classes/PouchLink.md +++ b/docs/api/cozy-pouch-link/classes/PouchLink.md @@ -32,7 +32,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:93](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L93) +[CozyPouchLink.js:95](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L95) ## Properties @@ -42,7 +42,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:160](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L160) +[CozyPouchLink.js:166](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L166) *** @@ -52,7 +52,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:109](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L109) +[CozyPouchLink.js:112](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L112) *** @@ -62,7 +62,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:110](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L110) +[CozyPouchLink.js:113](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L113) *** @@ -72,7 +72,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:111](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L111) +[CozyPouchLink.js:114](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L114) *** @@ -82,7 +82,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:115](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L115) +[CozyPouchLink.js:118](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L118) *** @@ -92,7 +92,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:103](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L103) +[CozyPouchLink.js:106](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L106) *** @@ -102,7 +102,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:116](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L116) +[CozyPouchLink.js:119](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L119) *** @@ -112,7 +112,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:230](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L230) +[CozyPouchLink.js:236](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L236) *** @@ -122,7 +122,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:119](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L119) +[CozyPouchLink.js:122](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L122) *** @@ -132,7 +132,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:112](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L112) +[CozyPouchLink.js:115](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L115) ## Methods @@ -152,7 +152,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:783](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L783) +[CozyPouchLink.js:789](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L789) *** @@ -172,7 +172,7 @@ CozyLink.constructor *Defined in* -[CozyPouchLink.js:744](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L744) +[CozyPouchLink.js:750](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L750) *** @@ -198,7 +198,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:575](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L575) +[CozyPouchLink.js:581](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L581) *** @@ -219,7 +219,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:787](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L787) +[CozyPouchLink.js:793](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L793) *** @@ -239,7 +239,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:772](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L772) +[CozyPouchLink.js:778](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L778) *** @@ -261,7 +261,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:714](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L714) +[CozyPouchLink.js:720](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L720) *** @@ -281,7 +281,7 @@ Create the PouchDB index if not existing *Defined in* -[CozyPouchLink.js:652](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L652) +[CozyPouchLink.js:658](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L658) *** @@ -305,7 +305,7 @@ Retrieve the PouchDB index if exist, undefined otherwise *Defined in* -[CozyPouchLink.js:599](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L599) +[CozyPouchLink.js:605](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L605) *** @@ -331,7 +331,7 @@ The changes *Defined in* -[CozyPouchLink.js:452](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L452) +[CozyPouchLink.js:458](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L458) *** @@ -356,7 +356,7 @@ The db info *Defined in* -[CozyPouchLink.js:467](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L467) +[CozyPouchLink.js:473](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L473) *** @@ -376,7 +376,7 @@ The db info *Defined in* -[CozyPouchLink.js:377](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L377) +[CozyPouchLink.js:383](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L383) *** @@ -396,7 +396,7 @@ The db info *Defined in* -[CozyPouchLink.js:140](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L140) +[CozyPouchLink.js:146](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L146) *** @@ -416,7 +416,7 @@ The db info *Defined in* -[CozyPouchLink.js:373](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L373) +[CozyPouchLink.js:379](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L379) *** @@ -436,7 +436,7 @@ The db info *Defined in* -[CozyPouchLink.js:284](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L284) +[CozyPouchLink.js:290](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L290) *** @@ -456,7 +456,7 @@ The db info *Defined in* -[CozyPouchLink.js:279](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L279) +[CozyPouchLink.js:285](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L285) *** @@ -482,7 +482,7 @@ Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done *Defined in* -[CozyPouchLink.js:265](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L265) +[CozyPouchLink.js:271](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L271) *** @@ -502,7 +502,7 @@ Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done *Defined in* -[CozyPouchLink.js:561](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L561) +[CozyPouchLink.js:567](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L567) *** @@ -532,7 +532,7 @@ Migrate the current adapter *Defined in* -[CozyPouchLink.js:174](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L174) +[CozyPouchLink.js:180](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L180) *** @@ -557,7 +557,7 @@ the need to wait for the warmup *Defined in* -[CozyPouchLink.js:547](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L547) +[CozyPouchLink.js:553](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L553) *** @@ -571,7 +571,7 @@ the need to wait for the warmup *Defined in* -[CozyPouchLink.js:193](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L193) +[CozyPouchLink.js:199](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L199) *** @@ -591,7 +591,7 @@ the need to wait for the warmup *Defined in* -[CozyPouchLink.js:353](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L353) +[CozyPouchLink.js:359](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L359) *** @@ -616,7 +616,7 @@ CozyLink.persistCozyData *Defined in* -[CozyPouchLink.js:502](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L502) +[CozyPouchLink.js:508](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L508) *** @@ -636,7 +636,7 @@ CozyLink.persistCozyData *Defined in* -[CozyPouchLink.js:159](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L159) +[CozyPouchLink.js:165](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L165) *** @@ -662,7 +662,7 @@ CozyLink.request *Defined in* -[CozyPouchLink.js:396](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L396) +[CozyPouchLink.js:402](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L402) *** @@ -680,7 +680,7 @@ CozyLink.reset *Defined in* -[CozyPouchLink.js:249](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L249) +[CozyPouchLink.js:255](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L255) *** @@ -700,7 +700,7 @@ CozyLink.reset *Defined in* -[CozyPouchLink.js:475](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L475) +[CozyPouchLink.js:481](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L481) *** @@ -719,7 +719,7 @@ Emits pouchlink:sync:start event when the replication begins *Defined in* -[CozyPouchLink.js:313](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L313) +[CozyPouchLink.js:319](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L319) *** @@ -737,7 +737,7 @@ Debounce delay can be configured through constructor's `syncDebounceDelayInMs` o *Defined in* -[CozyPouchLink.js:326](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L326) +[CozyPouchLink.js:332](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L332) *** @@ -756,7 +756,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:345](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L345) +[CozyPouchLink.js:351](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L351) *** @@ -776,7 +776,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:381](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L381) +[CozyPouchLink.js:387](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L387) *** @@ -790,7 +790,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:809](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L809) +[CozyPouchLink.js:815](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L815) *** @@ -810,7 +810,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:749](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L749) +[CozyPouchLink.js:755](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L755) *** @@ -830,7 +830,7 @@ Emits pouchlink:sync:stop event *Defined in* -[CozyPouchLink.js:754](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L754) +[CozyPouchLink.js:760](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L760) *** @@ -855,4 +855,4 @@ The adapter name *Defined in* -[CozyPouchLink.js:135](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L135) +[CozyPouchLink.js:141](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L141) diff --git a/packages/cozy-pouch-link/src/CozyPouchLink.js b/packages/cozy-pouch-link/src/CozyPouchLink.js index 321e47f4d..f416dd49e 100644 --- a/packages/cozy-pouch-link/src/CozyPouchLink.js +++ b/packages/cozy-pouch-link/src/CozyPouchLink.js @@ -39,6 +39,7 @@ const DEFAULT_OPTIONS = { } const DEFAULT_DEBOUNCE_DELAY = 10 * 1000 +const MAX_DEBOUNCE_DELAY = 600 * 1000 const addBasicAuth = (url, basicAuth) => { return url.replace('//', `//${basicAuth}`) @@ -72,6 +73,7 @@ const normalizeAll = client => (docs, doctype) => { * @property {boolean} initialSync Whether or not a replication process should be started. Default is false * @property {boolean} periodicSync Whether or not the replication should be periodic. Default is true * @property {number} [syncDebounceDelayInMs] Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds + * @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 * @property {number} [replicationInterval] Milliseconds between periodic replications * @property {string[]} doctypes Doctypes to replicate * @property {Record} 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") @@ -98,7 +100,8 @@ class PouchLink extends CozyLink { doctypesReplicationOptions, periodicSync, initialSync, - syncDebounceDelayInMs + syncDebounceDelayInMs, + syncDebounceMaxDelayInMs } = options this.options = options if (!doctypes) { @@ -121,7 +124,10 @@ class PouchLink extends CozyLink { /** @private */ this.startReplicationDebounced = debounce( this._startReplication, - syncDebounceDelayInMs || DEFAULT_DEBOUNCE_DELAY + syncDebounceDelayInMs || DEFAULT_DEBOUNCE_DELAY, + { + maxWait: syncDebounceMaxDelayInMs || MAX_DEBOUNCE_DELAY + } ) } diff --git a/packages/cozy-pouch-link/types/CozyPouchLink.d.ts b/packages/cozy-pouch-link/types/CozyPouchLink.d.ts index 0583a99c6..afcc03899 100644 --- a/packages/cozy-pouch-link/types/CozyPouchLink.d.ts +++ b/packages/cozy-pouch-link/types/CozyPouchLink.d.ts @@ -16,6 +16,10 @@ export type PouchLinkOptions = { * Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds */ syncDebounceDelayInMs?: number; + /** + * The maximum duration (in ms) the `startReplicationWithDebounce()` method can be delayed. Should be used only when periodicSync is false. Default is 10 minutes + */ + syncDebounceMaxDelayInMs?: number; /** * Milliseconds between periodic replications */ @@ -43,6 +47,7 @@ export type PouchLinkOptions = { * @property {boolean} initialSync Whether or not a replication process should be started. Default is false * @property {boolean} periodicSync Whether or not the replication should be periodic. Default is true * @property {number} [syncDebounceDelayInMs] Debounce delay (in ms) when calling `startReplicationWithDebounce()` method. Should be used only when periodicSync is false. Default is 10 seconds + * @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 * @property {number} [replicationInterval] Milliseconds between periodic replications * @property {string[]} doctypes Doctypes to replicate * @property {Record} 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")