second attempt to add subscribe module and fix race condition

This commit is contained in:
Egor Kislitsyn 2018-12-07 20:13:36 +07:00
parent a85d128d37
commit 11716a7a53
3 changed files with 37 additions and 6 deletions

View File

@ -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 })
})

View File

@ -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

View File

@ -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