Skip to content

Commit

Permalink
feat(pouch-link): Allow to inject event methods in CozyPouchLink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ldoppea committed May 28, 2024
1 parent afe1683 commit cb0062d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/cozy-pouch-link/src/PouchManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -77,23 +78,23 @@ 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
}
}

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
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/cozy-pouch-link/src/platformWeb.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -18,6 +27,7 @@ const isOnline = async () => {

export const platformWeb = {
storage,
events,
pouchAdapter: PouchDB,
isOnline
}

0 comments on commit cb0062d

Please sign in to comment.