store.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // adapted from https://raw.githubusercontent.com/LightouchDev/MasterVyrn/master/src/main/libs/store.js
  2. // steal from https://github.com/vuejs/vuex/issues/92#issuecomment-212012430
  3. import Vue from 'vue'
  4. import Vuex from 'vuex'
  5. import data from '../../src/store/data'
  6. import { BrowserWindow, ipcMain } from 'electron'
  7. const clients = []
  8. Vue.use(Vuex)
  9. const store = new Vuex.Store({
  10. modules: {
  11. data
  12. }
  13. })
  14. store.subscribe((mutation, state) => {
  15. clients.forEach(client => {
  16. client.send('vuex-apply-mutation', mutation)
  17. })
  18. })
  19. ipcMain.on('vuex-connect', (event) => {
  20. let winId = BrowserWindow.fromWebContents(event.sender).id
  21. clients[winId] = event.sender
  22. event.returnValue = store.state
  23. })
  24. ipcMain.on('vuex-mutation', (event, args) => {
  25. try {
  26. store.commit(...args)
  27. } catch (error) {
  28. event.sender.send('vuex-error', error)
  29. }
  30. })
  31. ipcMain.on('vuex-action', (event, args) => {
  32. try {
  33. store.dispatch(...args)
  34. } catch (error) {
  35. event.sender.send('vuex-error', error)
  36. }
  37. })
  38. export default store