generated from catdad/electron-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisomorphic.js
74 lines (58 loc) · 1.92 KB
/
isomorphic.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
const electron = require('electron');
const is = require('./is.js');
const log = require('./log.js');
const gid = () => Math.random().toString(36).substr(2);
module.exports = ({ name, implementation }) => {
const { info, error } = log(`${name}-isomorphic`);
const CB_CHANNEL = `${name}-channel`;
const keys = Object.keys(implementation).filter(k => k[0] !== '_');
if (is.main && electron.ipcMain.listenerCount(CB_CHANNEL) === 0) {
info(`initializing main ipc for ${name} module`);
const callback = (ev, id) => (err, result) => {
const cbName = `${name}:callback:${id}`;
const threadTimestamp = Date.now();
const send = ev.reply ? ev.reply.bind(ev) : ev.sender.send.bind(ev.sender);
if (err) {
return send(cbName, {
ok: false,
err: err.message,
threadTimestamp
});
}
return send(cbName, {
ok: true,
value: result,
threadTimestamp
});
};
electron.ipcMain.on(CB_CHANNEL, async (ev, { id, opname, args }) => {
const cb = callback(ev, id);
if (!implementation[opname] || opname[0] === '_') {
return cb(new Error(`unknown operation "${opname}"`));
}
try {
const result = await implementation[opname](...args);
return cb(null, result);
} catch (e) {
error(e);
return cb(e);
}
});
}
const rendererSend = (opname, args) => {
const id = gid() + gid();
return new Promise((resolve, reject) => {
electron.ipcRenderer.once(`${name}:callback:${id}`, (ev, result) => {
if (result.ok) {
return resolve(result.value);
}
return reject(new Error(result.err));
});
electron.ipcRenderer.send(CB_CHANNEL, { id, opname, args });
});
};
return keys.reduce((memo, key) => {
memo[key] = is.main ? implementation[key] : (...args) => rendererSend(key, args);
return memo;
}, {});
};