From 6acd889589e46b18491d96b5fa992154b4e58d88 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 10 Dec 2019 21:30:27 +0200 Subject: Option to enable streaming --- src/components/settings/settings.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/components/settings/settings.js') diff --git a/src/components/settings/settings.js b/src/components/settings/settings.js index c49083f9..2d7723cc 100644 --- a/src/components/settings/settings.js +++ b/src/components/settings/settings.js @@ -84,7 +84,7 @@ const settings = { } }]) .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}), - // Special cases (need to transform values) + // Special cases (need to transform values or perform actions first) muteWordsString: { get () { return this.$store.getters.mergedConfig.muteWords.join('\n') }, set (value) { @@ -93,6 +93,18 @@ const settings = { value: filter(value.split('\n'), (word) => trim(word).length > 0) }) } + }, + useStreamingApi: { + get () { return this.$store.getters.mergedConfig.useStreamingApi }, + set (value) { + const promise = value + ? this.$store.dispatch('enableMastoSockets') + : this.$store.dispatch('disableMastoSockets') + + promise.then(() => { + this.$store.dispatch('setOption', { name: 'useStreamingApi', value }) + }) + } } }, // Updating nested properties -- cgit v1.2.3-70-g09d2 From 43197c424366099301e59d3d1c7be58b987cb833 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 26 Dec 2019 14:12:35 +0200 Subject: Some error handling --- src/components/settings/settings.js | 4 ++ src/modules/api.js | 87 ++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 40 deletions(-) (limited to 'src/components/settings/settings.js') 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') }) -- cgit v1.2.3-70-g09d2