/
main.js
55 lines (46 loc) · 1.31 KB
/
main.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
const { app } = require('electron');
app.whenReady().then(() => {
console.log('started'); // ping parent
});
// Send data from the second instance to the first instance.
const sendAdditionalData = app.commandLine.hasSwitch('send-data');
// Prevent the default behaviour of second-instance, which sends back an empty ack.
const preventDefault = app.commandLine.hasSwitch('prevent-default');
// Send an object back for the ack rather than undefined.
const sendAck = app.commandLine.hasSwitch('send-ack');
const obj = {
level: 1,
testkey: 'testvalue1',
inner: {
level: 2,
testkey: 'testvalue2'
}
};
const ackObj = {
level: 1,
testkey: 'acktestvalue1',
inner: {
level: 2,
testkey: 'acktestvalue2'
}
};
app.on('first-instance-ack', (event, additionalData) => {
console.log(JSON.stringify(additionalData));
});
const gotTheLock = sendAdditionalData
? app.requestSingleInstanceLock(obj) : app.requestSingleInstanceLock();
app.on('second-instance', (event, args, workingDirectory, data, ackCallback) => {
if (preventDefault) {
event.preventDefault();
}
setImmediate(() => {
console.log([JSON.stringify(args), JSON.stringify(data)].join('||'));
sendAck ? ackCallback(ackObj) : ackCallback();
setImmediate(() => {
app.exit(0);
});
});
});
if (!gotTheLock) {
app.exit(1);
}