New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New makeSingleInstance API #12782

Merged
merged 7 commits into from May 7, 2018

Conversation

Projects
None yet
4 participants
@MarshallOfSound
Member

MarshallOfSound commented May 2, 2018

Closes #12752

New usage is below

const {app} = require('electron')
let myWindow = null

const gotTheLock = app.requestSingleInstanceLock()

app.on('second-instance', (commandLine, workingDirectory) => {
  // Someone tried to run a second instance, we should focus our window.
  if (myWindow) {
    if (myWindow.isMinimized()) myWindow.restore()
    myWindow.focus()
  }
})

if (!gotTheLock) {
  return app.quit()
}

// Create myWindow, load the rest of the app, etc...
app.on('ready', () => {
})

IMPORTANT: This is not a breaking change, the old syntax still works but is deprecated and should be removed in 4.0.0

MarshallOfSound added some commits May 2, 2018

Refactor app.makeSingleInstance
* new API `app.isPrimaryInstance()`
* new API `app.isSingleInstance()`
* new event `app.on('second-instance')`
* deprecated old syntax `app.makeSingleInstance(cb)`
* deprecated old syntax of `app.makeSingleInstance() --> bool` in favor
of `app.isPrimaryInstance()`

@MarshallOfSound MarshallOfSound requested review from electron/docs as code owners May 2, 2018

@MarshallOfSound

This comment has been minimized.

Show comment
Hide comment
@MarshallOfSound

MarshallOfSound May 2, 2018

Member

Although the new API requires two API calls instead of one to make your app single instance and then figure out if you should quit or not I think it is a much better API as it is way clearer what is going on for devs 👍

Member

MarshallOfSound commented May 2, 2018

Although the new API requires two API calls instead of one to make your app single instance and then figure out if you should quit or not I think it is a much better API as it is way clearer what is going on for devs 👍

@ckerr

Implementation comments inline.

Coding aside, I do wonder if this public API could be simplified before merging. We've expanded from one method to three, which must be called in a specific order or exceptions get thrown, and which in practice wouldn't be called without the other being called too.

I agree with the idea of switching from a callback to a signal, but maybe this could be done with a simpler API by just removing the callback arg from app.makeSingleInstance()?

Show outdated Hide outdated atom/browser/api/atom_api_app.cc
Show outdated Hide outdated atom/browser/api/atom_api_app.cc
Show outdated Hide outdated atom/browser/api/atom_api_app.cc
Show outdated Hide outdated atom/browser/api/atom_api_app.cc
Show outdated Hide outdated atom/browser/api/atom_api_app.cc
Show outdated Hide outdated atom/browser/api/atom_api_app.h
Show outdated Hide outdated docs/api/app.md
Show outdated Hide outdated docs/api/app.md
@MarshallOfSound

This comment has been minimized.

Show comment
Hide comment
@MarshallOfSound

MarshallOfSound May 2, 2018

Member

We've expanded from one method to three

nit: Only 2, the third method is not required, it's just there as a helper for modules to use.

Explained my reasoning behind it here: #12782 (comment)

Member

MarshallOfSound commented May 2, 2018

We've expanded from one method to three

nit: Only 2, the third method is not required, it's just there as a helper for modules to use.

Explained my reasoning behind it here: #12782 (comment)

MarshallOfSound added some commits May 3, 2018

@ckerr

Changes requested, but I like where this is heading and appreciate the collaboration. 👍

}
return !app.requestSingleInstanceLock()
}

This comment has been minimized.

@ckerr

ckerr May 3, 2018

Member

Needs a deprecate mesage for app.releaseSingleInstance() too

@ckerr

ckerr May 3, 2018

Member

Needs a deprecate mesage for app.releaseSingleInstance() too

Show outdated Hide outdated atom/browser/api/atom_api_app.cc
Emit("second-instance", cmd, cwd);
}
bool App::HasSingleInstanceLock() const {

This comment has been minimized.

@ckerr

ckerr May 3, 2018

Member

not to beat a dead horse, but what is the use case for this method being public API? 😃

I'm not completely opposed to this; more like I need help understanding who would use this

@ckerr

ckerr May 3, 2018

Member

not to beat a dead horse, but what is the use case for this method being public API? 😃

I'm not completely opposed to this; more like I need help understanding who would use this

This comment has been minimized.

@MarshallOfSound

MarshallOfSound May 3, 2018

Member

Example: A helper module for handling opening files in an Electron app, will need to know whether to expect second-instance events or whether to handle multiple instances 👍

Basically helpful for third party modules that devs might use, not first party code inside apps.

@MarshallOfSound

MarshallOfSound May 3, 2018

Member

Example: A helper module for handling opening files in an Electron app, will need to know whether to expect second-instance events or whether to handle multiple instances 👍

Basically helpful for third party modules that devs might use, not first party code inside apps.

Show outdated Hide outdated docs/api/app.md
Show outdated Hide outdated docs/api/app.md
@@ -109,6 +109,15 @@ for (let name of events) {
})
}
// TODO(MarshallOfSound): Remove in 4.0

This comment has been minimized.

@ckerr

ckerr May 3, 2018

Member

Should be added to docs/tutorial/planned-breaking-changes.md

@ckerr

ckerr May 3, 2018

Member

Should be added to docs/tutorial/planned-breaking-changes.md

Show outdated Hide outdated docs/api/app.md
Show outdated Hide outdated docs/api/app.md
@ckerr

ckerr approved these changes May 3, 2018

LGTM. 👍

Since this deprecates existing API I'd like to get a second signoff on this before merging -- but FWIW this addresses all the issues I've commented on over the last day. 💯

@codebytere

Per readthrough and @ckerr's comments, i think this is ready for prime-time 🚀

@MarshallOfSound MarshallOfSound merged commit 5b5c161 into master May 7, 2018

10 checks passed

WIP ready for review
Details
ci/circleci: electron-linux-arm Your tests passed on CircleCI!
Details
ci/circleci: electron-linux-arm-test Your tests passed on CircleCI!
Details
ci/circleci: electron-linux-arm64 Your tests passed on CircleCI!
Details
ci/circleci: electron-linux-arm64-test Your tests passed on CircleCI!
Details
ci/circleci: electron-linux-ia32 Your tests passed on CircleCI!
Details
ci/circleci: electron-linux-x64 Your tests passed on CircleCI!
Details
ci/circleci: electron-mas-x64 Your tests passed on CircleCI!
Details
ci/circleci: electron-osx-x64 Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details

@MarshallOfSound MarshallOfSound deleted the single-instance-event branch May 7, 2018

niik added a commit to niik/electron-typescript-definitions that referenced this pull request May 9, 2018

@shesek

This comment has been minimized.

Show comment
Hide comment
@shesek

shesek Jul 10, 2018

It looks like the event listener is called with (event, args, cwd), not with the documented (args, cwd). Which is the correct behavior?

shesek commented Jul 10, 2018

It looks like the event listener is called with (event, args, cwd), not with the documented (args, cwd). Which is the correct behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment