From cb0062d6f402632b4cba25807023b2fcf1ba54d3 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Tue, 28 May 2024 19:23:00 +0200 Subject: [PATCH] feat(pouch-link): Allow to inject event methods in CozyPouchLink In previous commit we added a `platform` option into the PouchLink constructor in order to allow injecting custom local storage API, PouchDB adapter and isOnline method from a react-native project We also want to inject a custom evant emitter for online/offline and pause/resume events as react-native does not provide the `document.addEventListener` and `document.removeEventListener` APIs Like for the other APIs, if no injection is given, then `document` APIs will be used by default --- packages/cozy-pouch-link/src/PouchManager.js | 17 +++++++++-------- packages/cozy-pouch-link/src/platformWeb.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/cozy-pouch-link/src/PouchManager.js b/packages/cozy-pouch-link/src/PouchManager.js index 4f7fb68324..184b572d37 100644 --- a/packages/cozy-pouch-link/src/PouchManager.js +++ b/packages/cozy-pouch-link/src/PouchManager.js @@ -42,6 +42,7 @@ class PouchManager { ) this.PouchDB = options.platform?.pouchAdapter || platformWeb.pouchAdapter this.isOnline = options.platform?.isOnline || platformWeb.isOnline + this.events = options.platform?.events || platformWeb.events } async init() { @@ -77,11 +78,11 @@ class PouchManager { addListeners() { if (!this.listenerLaunched) { if (isMobileApp()) { - document.addEventListener('pause', this.stopReplicationLoop) - document.addEventListener('resume', this.startReplicationLoop) + this.events.addEventListener('pause', this.stopReplicationLoop) + this.events.addEventListener('resume', this.startReplicationLoop) } - document.addEventListener('online', this.startReplicationLoop) - document.addEventListener('offline', this.stopReplicationLoop) + this.events.addEventListener('online', this.startReplicationLoop) + this.events.addEventListener('offline', this.stopReplicationLoop) this.listenerLaunched = true } } @@ -89,11 +90,11 @@ class PouchManager { removeListeners() { if (this.listenerLaunched) { if (isMobileApp()) { - document.removeEventListener('pause', this.stopReplicationLoop) - document.removeEventListener('resume', this.startReplicationLoop) + this.events.removeEventListener('pause', this.stopReplicationLoop) + this.events.removeEventListener('resume', this.startReplicationLoop) } - document.removeEventListener('online', this.startReplicationLoop) - document.removeEventListener('offline', this.stopReplicationLoop) + this.events.removeEventListener('online', this.startReplicationLoop) + this.events.removeEventListener('offline', this.stopReplicationLoop) this.listenerLaunched = false } } diff --git a/packages/cozy-pouch-link/src/platformWeb.js b/packages/cozy-pouch-link/src/platformWeb.js index 168b8a3391..9f96d78c38 100644 --- a/packages/cozy-pouch-link/src/platformWeb.js +++ b/packages/cozy-pouch-link/src/platformWeb.js @@ -1,5 +1,14 @@ import PouchDB from 'pouchdb-browser' +const events = { + addEventListener: (eventName, handler) => { + document.addEventListener(eventName, handler) + }, + removeEventListener: (eventName, handler) => { + document.removeEventListener(eventName, handler) + } +} + const storage = { getItem: async key => { return window.localStorage.getItem(key) @@ -18,6 +27,7 @@ const isOnline = async () => { export const platformWeb = { storage, + events, pouchAdapter: PouchDB, isOnline }