bitovi / launchpad

NodeJS Browser Launcher
MIT License
47 stars 28 forks source link

instance.on('stop'...) is called prematurely #60

Closed unional closed 8 years ago

unional commented 8 years ago

I run the following on vscode debug and npm start, same result:

var createServer = require('http-server').createServer;
var launch = require('launchpad');

var server = createServer({
  root: __dirname,
  cache: -1
});

var port = 8080;
server.listen(port);

launch.local(function (error, launcher) {
  launcher.chrome(`http://localhost:${port}`, function (err, instance) {
    instance.on('stop', function () {
      console.log('Browser closed');
      process.exit(0);
    });
  });
});

The Browser closed is printed out when the tab on the browser just created and loading the page.

This does work if there is no pre-existing chrome instance, i.e. When there is no chrome instance exists, running this code will create a new chrome window and the stop event is not fired. When I close the window, then stop event is fired correctly.

Where can I find information on the events?

daffl commented 8 years ago

stop is the only event a local instance sends and having it fire right away when the browser is already open is because the process launchpad starts in that case does terminate right after it opened the tab.

I do not know of a cross-platform way to figure out when that browser has actually been closed.

unional commented 8 years ago

I see. And chrome internally create another process to host the tab? My understanding is that each chrome tab is a standalone process.

daffl commented 8 years ago

Tabs are child process of the chrome main process but if it is open already the default behaviour of running Chrome from the command line is to just add the tab to the existing main process. It would be possible to to open a new instance with profile support (see #11).

unional commented 8 years ago

Thanks for the clarification. I'll not listen to the stop event in the time being.

11 seems to be a hard nut to crack, or just didn't have time to tackle it?

daffl commented 8 years ago

I don't think it's too hard to crack, basically you just have to generate a temporary profile name and figure out what the command line parameters are the browser need to initialize with a new profile. I personally didn't need it yet (launchpad was mostly for kicking off browsers on CI servers where you have a new system every time) but can help if someone would like to tackle it.

unional commented 8 years ago

For now, I'll close this as an duplicate for #11