forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload_utils.js
74 lines (67 loc) · 1.74 KB
/
preload_utils.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
/* global window */
const { ipcRenderer: ipc } = require('electron');
exports.installGetter = function installGetter(name, functionName) {
ipc.on(`get-${name}`, async () => {
const getFn = window.Events[functionName];
if (!getFn) {
ipc.send(
`get-success-${name}`,
`installGetter: ${functionName} not found for event ${name}`
);
return;
}
try {
ipc.send(`get-success-${name}`, null, await getFn());
} catch (error) {
ipc.send(
`get-success-${name}`,
error && error.stack ? error.stack : error
);
}
});
};
exports.installSetter = function installSetter(name, functionName) {
ipc.on(`set-${name}`, async (_event, value) => {
const setFn = window.Events[functionName];
if (!setFn) {
ipc.send(
`set-success-${name}`,
`installSetter: ${functionName} not found for event ${name}`
);
return;
}
try {
await setFn(value);
ipc.send(`set-success-${name}`);
} catch (error) {
ipc.send(
`set-success-${name}`,
error && error.stack ? error.stack : error
);
}
});
};
exports.makeGetter = function makeGetter(name) {
return () =>
new Promise((resolve, reject) => {
ipc.once(`get-success-${name}`, (event, error, value) => {
if (error) {
return reject(error);
}
return resolve(value);
});
ipc.send(`get-${name}`);
});
};
exports.makeSetter = function makeSetter(name) {
return value =>
new Promise((resolve, reject) => {
ipc.once(`set-success-${name}`, (event, error) => {
if (error) {
return reject(error);
}
return resolve();
});
ipc.send(`set-${name}`, value);
});
};