-
Notifications
You must be signed in to change notification settings - Fork 96
/
shared-mutations.js
118 lines (94 loc) · 3.12 KB
/
shared-mutations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { ipcMain, ipcRenderer } from "electron"
const IPC_EVENT_CONNECT = "vuex-mutations-connect"
const IPC_EVENT_NOTIFY_MAIN = "vuex-mutations-notify-main"
const IPC_EVENT_NOTIFY_RENDERERS = "vuex-mutations-notify-renderers"
class SharedMutations {
constructor(options, store) {
this.options = options
this.store = store
}
loadOptions() {
if (!this.options.type) this.options.type = process.type === "renderer" ? "renderer" : "main"
if (!this.options.ipcMain) this.options.ipcMain = ipcMain
if (!this.options.ipcRenderer) this.options.ipcRenderer = ipcRenderer
}
connect(payload) {
this.options.ipcRenderer.send(IPC_EVENT_CONNECT, payload)
}
onConnect(handler) {
this.options.ipcMain.on(IPC_EVENT_CONNECT, handler)
}
notifyMain(payload) {
this.options.ipcRenderer.send(IPC_EVENT_NOTIFY_MAIN, payload)
}
onNotifyMain(handler) {
this.options.ipcMain.on(IPC_EVENT_NOTIFY_MAIN, handler)
}
notifyRenderers(connections, payload) {
Object.keys(connections).forEach((processId) => {
connections[processId].send(IPC_EVENT_NOTIFY_RENDERERS, payload)
})
}
onNotifyRenderers(handler) {
this.options.ipcRenderer.on(IPC_EVENT_NOTIFY_RENDERERS, handler)
}
rendererProcessLogic() {
// Connect renderer to main process
this.connect()
// Save original Vuex methods
this.store.originalCommit = this.store.commit
this.store.originalDispatch = this.store.dispatch
// Don't use commit in renderer outside of actions
this.store.commit = () => {
throw new Error(`[Vuex Electron] Please, don't use direct commit's, use dispatch instead of this.`)
}
// Forward dispatch to main process
this.store.dispatch = (type, payload) => {
this.notifyMain({ type, payload })
}
// Subscribe on changes from main process and apply them
this.onNotifyRenderers((event, { type, payload }) => {
this.store.originalCommit(type, payload)
})
}
mainProcessLogic() {
const connections = {}
// Save new connection
this.onConnect((event) => {
const win = event.sender
const winId = win.id
connections[winId] = win
// Remove connection when window is closed
win.on("destroyed", () => {
delete connections[winId]
})
})
// Subscribe on changes from renderer processes
this.onNotifyMain((event, { type, payload }) => {
this.store.dispatch(type, payload)
})
// Subscribe on changes from Vuex store
this.store.subscribe((mutation) => {
const { type, payload } = mutation
// Forward changes to renderer processes
this.notifyRenderers(connections, { type, payload })
})
}
activatePlugin() {
switch (this.options.type) {
case "renderer":
this.rendererProcessLogic()
break
case "main":
this.mainProcessLogic()
break
default:
throw new Error(`[Vuex Electron] Type should be "renderer" or "main".`)
}
}
}
export default (options = {}) => (store) => {
const sharedMutations = new SharedMutations(options, store)
sharedMutations.loadOptions()
sharedMutations.activatePlugin()
}