diff options
author | Max <max@nextcloud.com> | 2022-02-07 14:57:33 +0300 |
---|---|---|
committer | Max <max@nextcloud.com> | 2022-02-09 11:42:47 +0300 |
commit | 95c706d16bc64cbc612fc9fd3d134e28f0269333 (patch) | |
tree | e4297d909fbb009b8d50ea6452b2586fe4351d43 /src/services | |
parent | c1af1a31584ffe9dac327c1e6eff037c77fa267c (diff) |
fix: use tiptap.commands to focus
Either handle network errors or the response.
There are three ways to handle errors and responses with axios:
```
axios.post(...).then(...).catch(...)
axios.post(...).catch(...).then(...)
axios.post(...).then(onResponse, onErr)
```
Use the last one.
First one will catch errors triggered by the emits in the happy path
with the network error handling.
Second one will handle the response,
even if errors have been caught.
Signed-off-by: Max <max@nextcloud.com>
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/PollingBackend.js | 127 |
1 files changed, 66 insertions, 61 deletions
diff --git a/src/services/PollingBackend.js b/src/services/PollingBackend.js index 7af6baae0..2237a394c 100644 --- a/src/services/PollingBackend.js +++ b/src/services/PollingBackend.js @@ -128,76 +128,81 @@ class PollingBackend { manualSave: !!this._manualSave, token: this._authority.options.shareToken, filePath: this._authority.options.filePath, - }).then((response) => { - this.fetchRetryCounter = 0 + }).then(this._handleResponse.bind(this), this._handleError.bind(this)) + this._manualSave = false + this._forcedSave = false + } - if (this._authority.document.lastSavedVersion < response.data.document.lastSavedVersion) { - console.debug('Saved document', response.data.document) - this._authority.emit('save', { document: response.data.document, sessions: response.data.sessions }) - } + _handleResponse(response) { + this.fetchRetryCounter = 0 - this._authority.emit('change', { document: response.data.document, sessions: response.data.sessions }) - this._authority.document = response.data.document - this._authority.sessions = response.data.sessions - - if (response.data.steps.length === 0) { - if (!this.initialLoadingFinished) { - this.initialLoadingFinished = true - } - if (this._authority.checkIdle()) { - return - } - this.lock = false - if (response.data.sessions.filter((session) => session.lastContact > Date.now() / 1000 - COLLABORATOR_DISCONNECT_TIME).length < 2) { - this.maximumRefetchTimer() - } else { - this.increaseRefetchTimer() - } - this._authority.emit('stateChange', { dirty: false }) - this._authority.emit('stateChange', { initialLoading: true }) - return - } + if (this._authority.document.lastSavedVersion < response.data.document.lastSavedVersion) { + console.debug('Saved document', response.data.document) + this._authority.emit('save', { document: response.data.document, sessions: response.data.sessions }) + } - this._authority._receiveSteps(response.data) - this.lock = false - this._forcedSave = false - if (this.initialLoadingFinished) { - this.resetRefetchTimer() + this._authority.emit('change', { document: response.data.document, sessions: response.data.sessions }) + this._authority.document = response.data.document + this._authority.sessions = response.data.sessions + + if (response.data.steps.length === 0) { + if (!this.initialLoadingFinished) { + this.initialLoadingFinished = true + } + if (this._authority.checkIdle()) { + return } - }).catch((e) => { this.lock = false - if (!e.response || e.code === 'ECONNABORTED') { - if (this.fetchRetryCounter++ >= MAX_RETRY_FETCH_COUNT) { - console.error('[PollingBackend:fetchSteps] Network error when fetching steps, emitting CONNECTION_FAILED') - this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false }) - - } else { - console.error(`[PollingBackend:fetchSteps] Network error when fetching steps, retry ${this.fetchRetryCounter}`) - } - } else if (e.response.status === 409 && e.response.data.document.currentVersion === this._authority.document.currentVersion) { - // Only emit conflict event if we have synced until the latest version - console.error('Conflict during file save, please resolve') - this._authority.emit('error', ERROR_TYPE.SAVE_COLLISSION, { - outsideChange: e.response.data.outsideChange, - }) - } else if (e.response.status === 403) { - this._authority.emit('error', ERROR_TYPE.SOURCE_NOT_FOUND, {}) - this.disconnect() - } else if (e.response.status === 404) { - this._authority.emit('error', ERROR_TYPE.SOURCE_NOT_FOUND, {}) - this.disconnect() - } else if (e.response.status === 503) { + if (response.data.sessions.filter((session) => session.lastContact > Date.now() / 1000 - COLLABORATOR_DISCONNECT_TIME).length < 2) { + this.maximumRefetchTimer() + } else { this.increaseRefetchTimer() + } + this._authority.emit('stateChange', { dirty: false }) + this._authority.emit('stateChange', { initialLoading: true }) + return + } + + this._authority._receiveSteps(response.data) + this.lock = false + this._forcedSave = false + if (this.initialLoadingFinished) { + this.resetRefetchTimer() + } + } + + _handleError(e) { + this.lock = false + if (!e.response || e.code === 'ECONNABORTED') { + if (this.fetchRetryCounter++ >= MAX_RETRY_FETCH_COUNT) { + console.error('[PollingBackend:fetchSteps] Network error when fetching steps, emitting CONNECTION_FAILED') this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false }) - console.error('Failed to fetch steps due to unavailable service', e) + } else { - this.disconnect() - this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false }) - console.error('Failed to fetch steps due to other reason', e) + console.error(`[PollingBackend:fetchSteps] Network error when fetching steps, retry ${this.fetchRetryCounter}`) } - }) - this._manualSave = false - this._forcedSave = false + } else if (e.response.status === 409 && e.response.data.document.currentVersion === this._authority.document.currentVersion) { + // Only emit conflict event if we have synced until the latest version + console.error('Conflict during file save, please resolve') + this._authority.emit('error', ERROR_TYPE.SAVE_COLLISSION, { + outsideChange: e.response.data.outsideChange, + }) + } else if (e.response.status === 403) { + this._authority.emit('error', ERROR_TYPE.SOURCE_NOT_FOUND, {}) + this.disconnect() + } else if (e.response.status === 404) { + this._authority.emit('error', ERROR_TYPE.SOURCE_NOT_FOUND, {}) + this.disconnect() + } else if (e.response.status === 503) { + this.increaseRefetchTimer() + this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false }) + console.error('Failed to fetch steps due to unavailable service', e) + } else { + this.disconnect() + this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, { retry: false }) + console.error('Failed to fetch steps due to other reason', e) + } + } sendSteps(_sendable) { |