From afe1683fe433195d8095e2c975406e3f3cb9864e Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Tue, 28 May 2024 19:22:18 +0200 Subject: [PATCH] feat(pouch-link): Allow to inject an `isOnline` method in CozyPouchLink 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 --- packages/cozy-pouch-link/src/PouchManager.js | 3 ++- packages/cozy-pouch-link/src/platformWeb.js | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/cozy-pouch-link/src/PouchManager.js b/packages/cozy-pouch-link/src/PouchManager.js index 6f1007365..4f7fb6832 100644 --- a/packages/cozy-pouch-link/src/PouchManager.js +++ b/packages/cozy-pouch-link/src/PouchManager.js @@ -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() { @@ -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' ) diff --git a/packages/cozy-pouch-link/src/platformWeb.js b/packages/cozy-pouch-link/src/platformWeb.js index e09b9de5c..168b8a339 100644 --- a/packages/cozy-pouch-link/src/platformWeb.js +++ b/packages/cozy-pouch-link/src/platformWeb.js @@ -12,7 +12,12 @@ const storage = { } } +const isOnline = async () => { + return window.navigator.onLine +} + export const platformWeb = { storage, - pouchAdapter: PouchDB + pouchAdapter: PouchDB, + isOnline }