From 43197c424366099301e59d3d1c7be58b987cb833 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 26 Dec 2019 14:12:35 +0200 Subject: [PATCH] Some error handling --- src/components/settings/settings.js | 4 ++ src/modules/api.js | 87 ++++++++++++++++------------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/components/settings/settings.js b/src/components/settings/settings.js index 2d7723cc..31a9e9be 100644 --- a/src/components/settings/settings.js +++ b/src/components/settings/settings.js @@ -103,6 +103,10 @@ const settings = { promise.then(() => { this.$store.dispatch('setOption', { name: 'useStreamingApi', value }) + }).catch((e) => { + console.error('Failed starting MastoAPI Streaming socket', e) + this.$store.dispatch('disableMastoSockets') + this.$store.dispatch('setOption', { name: 'useStreamingApi', value: false }) }) } } diff --git a/src/modules/api.js b/src/modules/api.js index dc91d00e..b6dd7fcf 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -35,60 +35,67 @@ const api = { enableMastoSockets (store) { const { state, dispatch } = store if (state.mastoUserSocket) return - dispatch('startMastoUserSocket') + return dispatch('startMastoUserSocket') }, disableMastoSockets (store) { const { state, dispatch } = store if (!state.mastoUserSocket) return - dispatch('stopMastoUserSocket') + return dispatch('stopMastoUserSocket') }, // MastoAPI 'User' sockets startMastoUserSocket (store) { - const { state, dispatch } = store - state.mastoUserSocket = state.backendInteractor.startUserSocket({ store }) - state.mastoUserSocket.addEventListener( - 'message', - ({ detail: message }) => { - if (!message) return // pings - if (message.event === 'notification') { - dispatch('addNewNotifications', { - notifications: [message.notification], - older: false - }) - } else if (message.event === 'update') { - dispatch('addNewStatuses', { - statuses: [message.status], - userId: false, - showImmediately: false, - timeline: 'friends' - }) - } - } - ) - state.mastoUserSocket.addEventListener('error', ({ detail: error }) => { - console.error('Error in MastoAPI websocket:', error) - }) - state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => { - const ignoreCodes = new Set([ - 1000, // Normal (intended) closure - 1001 // Going away - ]) - const { code } = closeEvent - if (ignoreCodes.has(code)) { - console.debug(`Not restarting socket becasue of closure code ${code} is in ignore list`) - } else { - console.warn(`MastoAPI websocket disconnected, restarting. CloseEvent code: ${code}`) - dispatch('startFetchingTimeline', { timeline: 'friends' }) - dispatch('startFetchingNotifications') - dispatch('restartMastoUserSocket') + return new Promise((resolve, reject) => { + try { + const { state, dispatch } = store + state.mastoUserSocket = state.backendInteractor.startUserSocket({ store }) + state.mastoUserSocket.addEventListener( + 'message', + ({ detail: message }) => { + if (!message) return // pings + if (message.event === 'notification') { + dispatch('addNewNotifications', { + notifications: [message.notification], + older: false + }) + } else if (message.event === 'update') { + dispatch('addNewStatuses', { + statuses: [message.status], + userId: false, + showImmediately: false, + timeline: 'friends' + }) + } + } + ) + state.mastoUserSocket.addEventListener('error', ({ detail: error }) => { + console.error('Error in MastoAPI websocket:', error) + }) + state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => { + const ignoreCodes = new Set([ + 1000, // Normal (intended) closure + 1001 // Going away + ]) + const { code } = closeEvent + if (ignoreCodes.has(code)) { + console.debug(`Not restarting socket becasue of closure code ${code} is in ignore list`) + } else { + console.warn(`MastoAPI websocket disconnected, restarting. CloseEvent code: ${code}`) + dispatch('startFetchingTimeline', { timeline: 'friends' }) + dispatch('startFetchingNotifications') + dispatch('restartMastoUserSocket') + } + }) + resolve() + } catch (e) { + reject(e) } }) }, restartMastoUserSocket ({ dispatch }) { // This basically starts MastoAPI user socket and stops conventional // fetchers when connection reestablished - dispatch('startMastoUserSocket').then(() => { + return dispatch('startMastoUserSocket').then(() => { dispatch('stopFetchingTimeline', { timeline: 'friends' }) dispatch('stopFetchingNotifications') })