simonw/datasette-app

Don't error if DB is already open

simonw opened this issue · 1 comments

I found out today that running this opens the database in Datasette Desktop, which is really cool!

open vaccinate.db

But... if you then run the same command a second time you get the following error:

vaccinate__location__80_842_rows

It would be better if it ignored the error and instead opened a window for that database - or re-focused a window if one was already open somewhere.

Some preliminary research tells me that the electron API provides us a way to deal with this:

When open vaccinate.db is called, a new app instance will launch – one of the initial steps should be to call app.requestSingleInstanceLock() which:

  • returns true if your process is the primary instance of your application and your app should continue loading.
  • returns false if your process should immediately quit as it has sent its parameters to another instance that has already acquired the lock.

Our scenario where an instance of the app is already open should return false, and the second-instance event should be fired (and the primary instance of the app should handle the event accordingly):

app.on('second-instance', (event, commandLine, workingDirectory) => {
    // handle event here to re-focus existing open window
  })

If the file is already open, we can simply re-focus the window – if a new file is being called using open (rather than within the app), then we can open a new window accordingly.