Skip to content

Commit 27a8f0b

Browse files
committed
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
1 parent fb54c99 commit 27a8f0b

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

packages/cozy-pouch-link/src/PouchManager.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class PouchManager {
4141
)
4242
this.PouchDB = options.platform?.pouchAdapter || platformWeb.pouchAdapter
4343
this.isOnline = options.platform?.isOnline || platformWeb.isOnline
44+
this.events = options.platform?.events || platformWeb.events
4445
}
4546

4647
async init() {
@@ -77,23 +78,23 @@ class PouchManager {
7778
addListeners() {
7879
if (!this.listenerLaunched) {
7980
if (isMobileApp()) {
80-
document.addEventListener('pause', this.stopReplicationLoop)
81-
document.addEventListener('resume', this.startReplicationLoop)
81+
this.events.addEventListener('pause', this.stopReplicationLoop)
82+
this.events.addEventListener('resume', this.startReplicationLoop)
8283
}
83-
document.addEventListener('online', this.startReplicationLoop)
84-
document.addEventListener('offline', this.stopReplicationLoop)
84+
this.events.addEventListener('online', this.startReplicationLoop)
85+
this.events.addEventListener('offline', this.stopReplicationLoop)
8586
this.listenerLaunched = true
8687
}
8788
}
8889

8990
removeListeners() {
9091
if (this.listenerLaunched) {
9192
if (isMobileApp()) {
92-
document.removeEventListener('pause', this.stopReplicationLoop)
93-
document.removeEventListener('resume', this.startReplicationLoop)
93+
this.events.removeEventListener('pause', this.stopReplicationLoop)
94+
this.events.removeEventListener('resume', this.startReplicationLoop)
9495
}
95-
document.removeEventListener('online', this.startReplicationLoop)
96-
document.removeEventListener('offline', this.stopReplicationLoop)
96+
this.events.removeEventListener('online', this.startReplicationLoop)
97+
this.events.removeEventListener('offline', this.stopReplicationLoop)
9798
this.listenerLaunched = false
9899
}
99100
}

packages/cozy-pouch-link/src/platformWeb.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import PouchDB from 'pouchdb-browser'
22

3+
const events = {
4+
addEventListener: (eventName, handler) => {
5+
document.addEventListener(eventName, handler)
6+
},
7+
removeEventListener: (eventName, handler) => {
8+
document.removeEventListener(eventName, handler)
9+
}
10+
}
11+
312
const storage = {
413
getItem: async key => {
514
return window.localStorage.getItem(key)
@@ -18,6 +27,7 @@ const isOnline = async () => {
1827

1928
export const platformWeb = {
2029
storage,
30+
events,
2131
pouchAdapter: PouchDB,
2232
isOnline
2333
}

0 commit comments

Comments
 (0)