Skip to content

Commit

Permalink
feat(pouch-link): Allow to inject an isOnline method 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 and
PouchDB adapter from a react-native project

We also want to inject a custom `isOnline` method as react-native does
not provide the `window.navigator.onLine` API

Like for the other APIs, if no injection is given, then
`window.navigator.onLine` will be used by default
  • Loading branch information
Ldoppea committed May 28, 2024
1 parent c3ffd2e commit afe1683
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/cozy-pouch-link/src/PouchManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class PouchManager {
options.platform?.storage || platformWeb.storage
)
this.PouchDB = options.platform?.pouchAdapter || platformWeb.pouchAdapter
this.isOnline = options.platform?.isOnline || platformWeb.isOnline
}

async init() {
Expand Down Expand Up @@ -170,7 +171,7 @@ class PouchManager {

/** Starts replication */
async replicateOnce() {
if (!window.navigator.onLine) {
if (!(await this.isOnline())) {
logger.info(
'PouchManager: The device is offline so the replication has been skipped'
)
Expand Down
7 changes: 6 additions & 1 deletion packages/cozy-pouch-link/src/platformWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const storage = {
}
}

const isOnline = async () => {
return window.navigator.onLine
}

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

0 comments on commit afe1683

Please sign in to comment.