Created
February 28, 2014 13:04
-
-
Save PeterHancock/9270706 to your computer and use it in GitHub Desktop.
async shotgun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cmd.js | |
exports.invoke = function(shell, options) { | |
shell.emit('async', function(resume) { | |
someAsyncFn(function(result){ | |
shell.log(result); | |
resume(); | |
}); | |
//app.js | |
var rl = require('readline'), | |
shell = ... | |
var ready = true; | |
//..constructors .. | |
rl.on('line', function(userInput) { | |
shell.execute(userInput); | |
if (ready) { | |
rl.prompt(); | |
} | |
}); | |
shell.on('async', function(fn) { | |
ready = false; | |
fn(function(){ | |
ready = true; | |
rl.prompt(); | |
}) | |
}); |
That sounds great! I assume the done event will only be fired at the end of execute if invoke is synchronous
That's correct. chevcast/shotgun#23
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nevermind. I think I'm understanding. The readline prompt resumes before the async is finished. I think I have the solution for this. I'm going to scan the invoke function signature and count the number of arguments the function is expecting. If it asks for two then I will assume they are
shell
andoptions
and the function will behave synchronously. If it asks for three then the third argument will be a callback that your command can call when it's all finished.All this callback will do is fire the "done" event from the shell. This means that you'll be able to handle the done event and do your
rl.prompt()
from there.Thank you for pointing out this flaw :)
Here's what my intended solution will look like when I'm done:
I will also make
done
accept anerr
argument which will cause the shell to emit an error event before the done event.