feat: Add first-instance-ack event to the app.requestSingleInstanceLock() flow (#31460)

* feat: Add onFirstInstanceAck event for requestSingleInstanceLock

* Add tests

* Apply patch fix

* Add back missing docs

* Rebase

* Listen for exit earlier in test

* Rebase
This commit is contained in:
Raymond Zhao 2022-01-10 08:54:46 -08:00 committed by GitHub
parent 7c16ef1f62
commit 746927c972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 514 additions and 92 deletions

View file

@ -1,12 +1,18 @@
const { app } = require('electron');
// Send data from the second instance to the first instance.
const sendAdditionalData = app.commandLine.hasSwitch('send-data');
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');
let obj = {
level: 1,
testkey: 'testvalue1',
@ -15,20 +21,45 @@ let obj = {
testkey: 'testvalue2'
}
};
let ackObj = {
level: 1,
testkey: 'acktestvalue1',
inner: {
level: 2,
testkey: 'acktestvalue2'
}
};
if (app.commandLine.hasSwitch('data-content')) {
obj = JSON.parse(app.commandLine.getSwitchValue('data-content'));
if (obj === 'undefined') {
obj = undefined;
}
}
if (app.commandLine.hasSwitch('ack-content')) {
ackObj = JSON.parse(app.commandLine.getSwitchValue('ack-content'));
if (ackObj === 'undefined') {
ackObj = undefined;
}
}
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) => {
app.on('second-instance', (event, args, workingDirectory, data, ackCallback) => {
if (preventDefault) {
event.preventDefault();
}
setImmediate(() => {
console.log([JSON.stringify(args), JSON.stringify(data)].join('||'));
app.exit(0);
sendAck ? ackCallback(ackObj) : ackCallback();
setImmediate(() => {
app.exit(0);
});
});
});