-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Forward operation in StackLink.request() when offline
#1487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import CozyLink from './CozyLink' | |
| import { DOCTYPE_FILES } from './const' | ||
| import { BulkEditError } from './errors' | ||
| import logger from './logger' | ||
| import { isReactNativeOfflineError } from './utils' | ||
|
|
||
| /** | ||
| * | ||
|
|
@@ -58,15 +59,17 @@ export default class StackLink extends CozyLink { | |
| * @param {object} [options] - Options | ||
| * @param {object} [options.stackClient] - A StackClient | ||
| * @param {object} [options.client] - A StackClient (deprecated) | ||
| * @param {import('cozy-pouch-link/dist/types').LinkPlatform} [options.platform] Platform specific adapters and methods | ||
| */ | ||
| constructor({ client, stackClient } = {}) { | ||
| constructor({ client, stackClient, platform } = {}) { | ||
| super() | ||
| if (client) { | ||
| logger.warn( | ||
| 'Using options.client is deprecated, prefer options.stackClient' | ||
| ) | ||
| } | ||
| this.stackClient = stackClient || client | ||
| this.isOnline = platform?.isOnline | ||
| } | ||
|
|
||
| registerClient(client) { | ||
|
|
@@ -77,11 +80,22 @@ export default class StackLink extends CozyLink { | |
| this.stackClient = null | ||
| } | ||
|
|
||
| request(operation, result, forward) { | ||
| if (operation.mutationType) { | ||
| return this.executeMutation(operation, result, forward) | ||
| async request(operation, result, forward) { | ||
| if (this.isOnline && !(await this.isOnline())) { | ||
| return forward(operation) | ||
| } | ||
|
|
||
| try { | ||
| if (operation.mutationType) { | ||
| return await this.executeMutation(operation, result, forward) | ||
| } | ||
| return await this.executeQuery(operation) | ||
| } catch (err) { | ||
| if (isReactNativeOfflineError(err)) { | ||
| return forward(operation) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this is a fallback in case the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, this is mainly because when a device loose the Internet connection, it can take a few seconds before the offline status is triggered. This often happens when you are connected to an Internet box through Wifi. If you unplug the internet connection, then your computer/phones will still displayed the "online" icon for a few seconds.
Good catch. But I'm not sure that we can check if any |
||
| } | ||
| throw err | ||
| } | ||
| return this.executeQuery(operation) | ||
| } | ||
|
|
||
| async persistData(data, forward) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,3 +68,15 @@ export const hasQueriesBeenLoaded = queriesResults => { | |
| hasQueryBeenLoaded(queryResult) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Check is the error is about ReactNative not having access to internet | ||
| * | ||
| * @param {Error} err - The error to check | ||
| * @returns {boolean} True if the error is a network error, otherwise false | ||
| */ | ||
| export const isReactNativeOfflineError = err => { | ||
| // This error message is specific to ReactNative | ||
| // Network errors on a browser would produce another error.message | ||
| return err.message === 'Network request failed' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder how stable this message is, and if it could change in future RN upgrades... |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.