forked from kuaifan/dootask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron-preload.js
67 lines (57 loc) · 1.59 KB
/
electron-preload.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
const {
contextBridge,
ipcRenderer
} = require("electron");
let reqId = 1;
let reqInfo = {};
let fileChangedListeners = {};
ipcRenderer.on('mainResp', (event, resp) => {
let callbacks = reqInfo[resp.reqId];
if (resp.error) {
callbacks.error(resp.msg, resp.e);
} else {
callbacks.callback(resp.data);
}
delete reqInfo[resp.reqId];
});
ipcRenderer.on('fileChanged', (event, resp) => {
let listener = fileChangedListeners[resp.path];
if (listener) {
listener(resp.curr, resp.prev);
}
});
contextBridge.exposeInMainWorld(
'electron', {
request: (msg, callback, error) => {
msg.reqId = reqId++;
reqInfo[msg.reqId] = {callback: callback, error: error};
if (msg.action == 'watchFile') {
fileChangedListeners[msg.path] = msg.listener;
delete msg.listener;
}
ipcRenderer.send('rendererReq', msg);
},
registerMsgListener: (action, callback) => {
ipcRenderer.on(action, (event, args) => {
callback(args);
});
},
listenOnce: (action, callback) => {
ipcRenderer.once(action, (event, args) => {
callback(args);
});
},
sendMessage: (action, args) => {
ipcRenderer.send(action, args);
},
sendSyncMessage: (action, args) => {
ipcRenderer.sendSync(action, args);
}
}
);
contextBridge.exposeInMainWorld(
'process', {
type: process.type,
versions: process.versions
}
);