From 172ebaf4e67358852bfaafd8f069763ca5e602b1 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 24 Nov 2019 22:01:12 +0200 Subject: [PATCH] improved initial notifications fetching --- src/components/notifications/notifications.js | 5 ++++ src/modules/api.js | 23 ++++++++++++++++--- src/modules/users.js | 2 +- .../backend_interactor_service.js | 9 ++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js index 6c4054fd..a89c0cdc 100644 --- a/src/components/notifications/notifications.js +++ b/src/components/notifications/notifications.js @@ -47,6 +47,11 @@ const Notifications = { components: { Notification }, + created () { + const { dispatch } = this.$store + + dispatch('fetchAndUpdateNotifications') + }, watch: { unseenCount (count) { if (count > 0) { diff --git a/src/modules/api.js b/src/modules/api.js index 1bf65db5..0e7e5e19 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -31,18 +31,32 @@ const api = { }, actions: { startMastoSocket (store) { - store.state.mastoSocket = store.state.backendInteractor + const { state, dispatch } = store + state.mastoSocket = state.backendInteractor .startUserSocket({ store, onMessage: (message) => { if (!message) return if (message.event === 'notification') { - store.dispatch('addNewNotifications', { notifications: [message.notification], older: false }) + dispatch('addNewNotifications', { + notifications: [message.notification], + older: false + }) } else if (message.event === 'update') { - store.dispatch('addNewStatuses', { statuses: [message.status], userId: false, showImmediately: false, timeline: 'friends' }) + dispatch('addNewStatuses', { + statuses: [message.status], + userId: false, + showImmediately: false, + timeline: 'friends' + }) } } }) + state.mastoSocket.addEventListener('error', error => { + console.error('Error with MastoAPI websocket:', error) + dispatch('startFetchingTimeline', { timeline: 'friends' }) + dispatch('startFetchingNotifications') + }) }, startFetchingTimeline (store, { timeline = 'friends', tag = false, userId = false }) { // Don't start fetching if we already are. @@ -58,6 +72,9 @@ const api = { const fetcher = store.state.backendInteractor.startFetchingNotifications({ store }) store.commit('addFetcher', { fetcherName: 'notifications', fetcher }) }, + fetchAndUpdateNotifications (store) { + store.state.backendInteractor.fetchAndUpdateNotifications({ store }) + }, startFetchingFollowRequest (store) { // Don't start fetching if we already are. if (store.state.fetchers['followRequest']) return diff --git a/src/modules/users.js b/src/modules/users.js index 861a2f4f..eff0c5d5 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -470,7 +470,7 @@ const users = { } store.dispatch('startMastoSocket').catch((error) => { - console.error(error) + console.error('Failed initializing MastoAPI Streaming socket', error) // Start getting fresh posts. store.dispatch('startFetchingTimeline', { timeline: 'friends' }) diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js index 0cef4640..850b7867 100644 --- a/src/services/backend_interactor_service/backend_interactor_service.js +++ b/src/services/backend_interactor_service/backend_interactor_service.js @@ -12,18 +12,23 @@ const backendInteractorService = credentials => ({ return notificationsFetcher.startFetching({ store, credentials }) }, + fetchAndUpdateNotifications ({ store }) { + return notificationsFetcher.fetchAndUpdate({ store, credentials }) + }, + startFetchingFollowRequest ({ store }) { return followRequestFetcher.startFetching({ store, credentials }) }, startUserSocket ({ store, onMessage }) { - const serv = store.rootState.instance.server.replace('https', 'wss') - // const serb = 'ws://localhost:8080/' + const serv = store.rootState.instance.server.replace('http', 'ws') const url = serv + getMastodonSocketURI({ credentials, stream: 'user' }) const socket = new WebSocket(url) console.log(socket) if (socket) { socket.addEventListener('message', (wsEvent) => onMessage(handleMastoWS(wsEvent))) + socket.addEventListener('error', (error) => console.error('WebSocket Error:', error)) + return socket } else { throw new Error('failed to connect to socket') }