Skip to content

Commit fb54c99

Browse files
committed
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
1 parent 5f4d434 commit fb54c99

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class PouchManager {
4040
options.platform?.storage || platformWeb.storage
4141
)
4242
this.PouchDB = options.platform?.pouchAdapter || platformWeb.pouchAdapter
43+
this.isOnline = options.platform?.isOnline || platformWeb.isOnline
4344
}
4445

4546
async init() {
@@ -174,7 +175,7 @@ class PouchManager {
174175

175176
/** Starts replication */
176177
async replicateOnce() {
177-
if (!window.navigator.onLine) {
178+
if (!(await this.isOnline())) {
178179
logger.info(
179180
'PouchManager: The device is offline so the replication has been skipped'
180181
)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ const storage = {
1212
}
1313
}
1414

15+
const isOnline = async () => {
16+
return window.navigator.onLine
17+
}
18+
1519
export const platformWeb = {
1620
storage,
17-
pouchAdapter: PouchDB
21+
pouchAdapter: PouchDB,
22+
isOnline
1823
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @typedef {object} LinkPlatform
2727
* @property {LocalStorage} storage Methods to access local storage
2828
* @property {any} pouchAdapter PouchDB class (can be pouchdb-core or pouchdb-browser)
29+
* @property {function(): Promise<boolean>} isOnline Method that check if the app is connected to internet
2930
*/
3031

3132
export default {}

0 commit comments

Comments
 (0)