Vuex plugin for electron that persisting part or all state to a local file.
import Vuex from 'vuex'
import { plugin as createPersistedState } from 'vuex-electron-persisted-state'
const store = Vuex.Store({})
export default new Vuex.Store({
modules: {
user: {
state: {
userInfo: null,
...
},
mutations: {
UPDATE_USER_INFO: (state, payload) => { state.userInfo = payload },
CLEAR_USER_INFO: (state, payload) => { state.userInfo = null },
...
},
actions: {...}
},
friend: {
state: {
friendList: [],
...
},
mutations: {
ADD_FRIEND: (state, payload) => { state.friendList.push(payload) },
REMOVE_FRIEND: (state, payload) => {
const findex = state.friendList.findIndex(it => it.uid === payload.uid)
findex !== -1 && state.friendList.splice(findex, 1)
},
...
},
actions: {...}
},
otherModule: {...}
},
plugins: [
createPersistedState({
name: 'myinfo',
fileExtension: 'dat',
cwd: 'path/to/save/dir/',
encryptionKey: '12345',
keypath: {
userInfo: 'user.userInfo',
friendList: 'friend.friendList'
},
whitelist: [
'UPDATE_USER_INFO',
'CLEAR_USER_INFO',
'ADD_FRIEND',
'REMOVE_FRIEND'
]
})
]
})
import store from './src/store/index'
import { configure } from 'vuex-electron-persisted-state'
let userInfoPersisted
let friendListPersisted
ipcMain.on('login-success', () => {
userInfoPersisted = configure(store, {
name: 'user-info',
keypath: {
userInfo: 'user.userInfo'
},
whitelist: [
'UPDATE_USER_INFO',
'CLEAR_USER_INFO'
],
afterinit () {
webContents.getAllWebContents().forEach((item) => {
item.send('sync-vuex')
})
}
})
friendListPersisted = configure(store, {
name: 'friend-list',
keypath: {
friendList: 'friend.friendList'
},
whitelist: [
'ADD_FRIEND',
'REMOVE_FRIEND'
],
afterinit () {
webContents.getAllWebContents().forEach((item) => {
item.send('sync-vuex')
})
}
})
})
ipcMain.on('logout', () => {
userInfoPersisted.terminate()
friendListPersisted.terminate()
})
import store from './src/store/index'
import { configure } from 'vuex-electron-persisted-state'
configure(store, {
keypath: {
data: ''
},
blacklist: (mutation) {
return mutation.type === 'SYNC_CURRENT_MUTATION'
}
})
})