From 11716a7a5381c8f4fe06c869a4a21c52a2120e6c Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 7 Dec 2018 20:13:36 +0700 Subject: [PATCH] second attempt to add subscribe module and fix race condition --- src/main.js | 6 ++++-- src/modules/pushNotifications.js | 29 +++++++++++++++++++++++++++++ src/modules/users.js | 8 ++++---- 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/modules/pushNotifications.js diff --git a/src/main.js b/src/main.js index 378fe95c..91592191 100644 --- a/src/main.js +++ b/src/main.js @@ -10,6 +10,7 @@ import apiModule from './modules/api.js' import configModule from './modules/config.js' import chatModule from './modules/chat.js' import oauthModule from './modules/oauth.js' +import pushNotificationsModule from './modules/pushNotifications.js' import VueTimeago from 'vue-timeago' import VueI18n from 'vue-i18n' @@ -60,12 +61,13 @@ createPersistedState(persistedStateOptions).then((persistedState) => { api: apiModule, config: configModule, chat: chatModule, - oauth: oauthModule + oauth: oauthModule, + pushNotifications: pushNotificationsModule }, plugins: [persistedState], strict: false // Socket modifies itself, let's ignore this for now. // strict: process.env.NODE_ENV !== 'production' }) - afterStoreSetup({store, i18n}) + afterStoreSetup({ store, i18n }) }) diff --git a/src/modules/pushNotifications.js b/src/modules/pushNotifications.js new file mode 100644 index 00000000..43143b1f --- /dev/null +++ b/src/modules/pushNotifications.js @@ -0,0 +1,29 @@ +import registerPushNotifications from '../services/push/push.js' + +const subscribe = { + state: { + token: null, + vapidPublicKey: null + }, + mutations: { + setApiToken (state, user) { + state.token = user.credentials + }, + setVapidPublicKey (state, vapidPublicKey) { + state.vapidPublicKey = vapidPublicKey + } + + }, + actions: { + setInstanceOption (store, { name, value }) { + store.commit('setVapidPublicKey', value) + if (store.state.token) registerPushNotifications(this) + }, + setCurrentUser (store, user) { + store.commit('setApiToken', user.credentials) + if (store.state.vapidPublicKey) registerPushNotifications(this) + } + } +} + +export default subscribe diff --git a/src/modules/users.js b/src/modules/users.js index d2c7fdf6..1256e9df 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,5 +1,4 @@ import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' -import registerPushNotifications from '../services/push/push.js' import { compact, map, each, merge } from 'lodash' import { set } from 'vue' @@ -87,6 +86,9 @@ const users = { store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService()) }, + setCurrentUser (store, user) { + store.commit('setCurrentUser', user) + }, loginUser (store, accessToken) { return new Promise((resolve, reject) => { const commit = store.commit @@ -98,7 +100,7 @@ const users = { .then((user) => { // user.credentials = userCredentials user.credentials = accessToken - commit('setCurrentUser', user) + store.dispatch('setCurrentUser', user) commit('addNewUsers', [user]) // Set our new backend interactor @@ -122,8 +124,6 @@ const users = { // Fetch our friends store.rootState.api.backendInteractor.fetchFriends({id: user.id}) .then((friends) => commit('addNewUsers', friends)) - - registerPushNotifications(store) }) } else { // Authentication failed