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 configModule from './modules/config.js'
import chatModule from './modules/chat.js' import chatModule from './modules/chat.js'
import oauthModule from './modules/oauth.js' import oauthModule from './modules/oauth.js'
import pushNotificationsModule from './modules/pushNotifications.js'
import VueTimeago from 'vue-timeago' import VueTimeago from 'vue-timeago'
import VueI18n from 'vue-i18n' import VueI18n from 'vue-i18n'
@ -60,12 +61,13 @@ createPersistedState(persistedStateOptions).then((persistedState) => {
api: apiModule, api: apiModule,
config: configModule, config: configModule,
chat: chatModule, chat: chatModule,
oauth: oauthModule oauth: oauthModule,
pushNotifications: pushNotificationsModule
}, },
plugins: [persistedState], plugins: [persistedState],
strict: false // Socket modifies itself, let's ignore this for now. strict: false // Socket modifies itself, let's ignore this for now.
// strict: process.env.NODE_ENV !== 'production' // 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 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 { compact, map, each, merge } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
@ -87,6 +86,9 @@ const users = {
store.dispatch('stopFetching', 'friends') store.dispatch('stopFetching', 'friends')
store.commit('setBackendInteractor', backendInteractorService()) store.commit('setBackendInteractor', backendInteractorService())
}, },
setCurrentUser (store, user) {
store.commit('setCurrentUser', user)
},
loginUser (store, accessToken) { loginUser (store, accessToken) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const commit = store.commit const commit = store.commit
@ -98,7 +100,7 @@ const users = {
.then((user) => { .then((user) => {
// user.credentials = userCredentials // user.credentials = userCredentials
user.credentials = accessToken user.credentials = accessToken
commit('setCurrentUser', user) store.dispatch('setCurrentUser', user)
commit('addNewUsers', [user]) commit('addNewUsers', [user])
// Set our new backend interactor // Set our new backend interactor
@ -122,8 +124,6 @@ const users = {
// Fetch our friends // Fetch our friends
store.rootState.api.backendInteractor.fetchFriends({id: user.id}) store.rootState.api.backendInteractor.fetchFriends({id: user.id})
.then((friends) => commit('addNewUsers', friends)) .then((friends) => commit('addNewUsers', friends))
registerPushNotifications(store)
}) })
} else { } else {
// Authentication failed // Authentication failed