dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

First CTRL+C should fire cancel event so running commands can be cancelled #45

Closed scotthovestadt closed 8 years ago

scotthovestadt commented 8 years ago

Currently, my command keeps on running.

dthree commented 8 years ago

Yeah - this is a bit more tricky so was going to get to it at some point. Probably going to add in a cancel method to vorpal.command.

scotthovestadt commented 8 years ago

Not sure a cancel method will work for long-running commands. I think we may need a cancel event?

dthree commented 8 years ago

We can fire an event, but I believe we should have a cancel method that just fires a function for convenience. The only purpose of this is cleanup. For example, something like this:

vorpal
.command('do things repeatedly', function(){
  const self = this;
  setInterval(function(){
    if (self._cancelled) {
      // break off
    }
  }, 1000);
})
.cancel(function() {
  this._cancelled = true;
});

What do you think?

scotthovestadt commented 8 years ago

Yeah, you're right. It's similar to the way Bluebird did it for Promises: http://bluebirdjs.com/docs/api/cancellation.html

PS: Let's also support calling the cancel() method on a promise if it exists...?

dthree commented 8 years ago

Captain epic (@jackyjieliu) here fixed this with https://github.com/dthree/vorpal/pull/62. I just tested this and fixed a final bug. Looks good.