From 90afcd3420a911856bde794f2b4dc1380a1a0751 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 8 Mar 2021 22:24:39 +0200 Subject: [PATCH] WIP some work on making errors less spammy --- src/i18n/en.json | 3 +- src/modules/api.js | 44 +++++++++++-------- .../notifications_fetcher.service.js | 14 +++--- .../timeline_fetcher.service.js | 14 +++--- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/i18n/en.json b/src/i18n/en.json index 217ac7c8..635bfd56 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -664,8 +664,7 @@ "no_more_statuses": "No more statuses", "no_statuses": "No statuses", "socket_reconnected": "Realtime connection established", - "socket_broke": "Realtime connection lost: CloseEvent code {0}", - "socket_closed": "No realtime connection, updates can arrive with a delaye until connection is re-established" + "socket_broke": "Realtime connection lost: CloseEvent code {0}" }, "status": { "favorites": "Favorites", diff --git a/src/modules/api.js b/src/modules/api.js index b482637d..e392d5b0 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -5,6 +5,8 @@ import { Socket } from 'phoenix' const api = { state: { + connectionBroken: false, + retryMultiplier: 1, backendInteractor: backendInteractorService(), fetchers: {}, socket: null, @@ -34,12 +36,18 @@ const api = { }, setMastoUserSocketStatus (state, value) { state.mastoUserSocketStatus = value + }, + recoverConnection (state) { + state.connectionBroken = false + }, + breakConnection (state) { + state.connectionBroken = true } }, actions: { // Global MastoAPI socket control, in future should disable ALL sockets/(re)start relevant sockets enableMastoSockets (store) { - const { state, dispatch } = store + const { state, dispatch, commit } = store // Do not initialize unless nonexistent or closed if ( state.mastoUserSocket && @@ -50,11 +58,13 @@ const api = { ) { return } + commit('recoverConnection') return dispatch('startMastoUserSocket') }, disableMastoSockets (store) { - const { state, dispatch } = store + const { state, dispatch, commit } = store if (!state.mastoUserSocket) return + commit('recoverConnection') return dispatch('stopMastoUserSocket') }, @@ -102,6 +112,7 @@ const api = { state.mastoUserSocket.addEventListener('open', () => { // Do not show notification when we just opened up the page if (state.mastoUserSocketStatus !== null) { + commit('recoverConnection') dispatch('pushGlobalNotice', { level: 'success', messageKey: 'timeline.socket_reconnected', @@ -114,15 +125,6 @@ const api = { console.error('Error in MastoAPI websocket:', error) commit('setMastoUserSocketStatus', WSConnectionStatus.ERROR) dispatch('clearOpenedChats') - /* Since data in WS event for error is useless it's better to show - * generic warning instead of in "close" which actually has some - * useful data - */ - dispatch('pushGlobalNotice', { - level: 'error', - messageKey: 'timeline.socket_closed', - timeout: 5000 - }) }) state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => { const ignoreCodes = new Set([ @@ -137,13 +139,19 @@ const api = { dispatch('startFetchingTimeline', { timeline: 'friends' }) dispatch('startFetchingNotifications') dispatch('startFetchingChats') - dispatch('restartMastoUserSocket') - dispatch('pushGlobalNotice', { - level: 'error', - messageKey: 'timeline.socket_broke', - messageArgs: [code], - timeout: 5000 - }) + setTimeout(() => { + console.log('TEST') + dispatch('restartMastoUserSocket') + }, 1000) + if (!state.connectionBroken) { + dispatch('pushGlobalNotice', { + level: 'error', + messageKey: 'timeline.socket_broke', + messageArgs: [code], + timeout: 5000 + }) + } + commit('breakConnection') } commit('setMastoUserSocketStatus', WSConnectionStatus.CLOSED) dispatch('clearOpenedChats') diff --git a/src/services/notifications_fetcher/notifications_fetcher.service.js b/src/services/notifications_fetcher/notifications_fetcher.service.js index f83f871e..fc5e76ba 100644 --- a/src/services/notifications_fetcher/notifications_fetcher.service.js +++ b/src/services/notifications_fetcher/notifications_fetcher.service.js @@ -57,12 +57,14 @@ const fetchNotifications = ({ store, args, older }) => { return notifications }) .catch((error) => { - store.dispatch('pushGlobalNotice', { - level: 'error', - messageKey: 'notifications.error', - messageArgs: [error.message], - timeout: 5000 - }) + if (!store.rootState.api.connectionBroken) { + store.dispatch('pushGlobalNotice', { + level: 'error', + messageKey: 'notifications.error', + messageArgs: [error.message], + timeout: 5000 + }) + } }) } diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index 46bba41a..eb4a3e0d 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -66,12 +66,14 @@ const fetchAndUpdate = ({ return { statuses, pagination } }) .catch((error) => { - store.dispatch('pushGlobalNotice', { - level: 'error', - messageKey: 'timeline.error', - messageArgs: [error.message], - timeout: 5000 - }) + if (!store.rootState.api.connectionBroken) { + store.dispatch('pushGlobalNotice', { + level: 'error', + messageKey: 'timeline.error', + messageArgs: [error.message], + timeout: 5000 + }) + } }) }