Last active
December 10, 2015 09:18
-
-
Save brianmhunt/4413209 to your computer and use it in GitHub Desktop.
An extension called 'recipe' that goes in place of the `task` function of Cakefiles. The `recipe` function emits events "taskname.start" and "taskname.complete". It also facilitates other events, on demand.
This file contains 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
events = require('events') | |
recipe = (id, description, action) -> | |
# `recipe` wraps the Cake `task` command but fires a events through 'recipe'. | |
# | |
# One can listen for an event with `recipe.on('event_name', callback)`, and | |
# call that event with `recipe.emit('event_name', args)`. | |
# | |
# If `action` takes two arguments, the second argument is presumed to be a | |
# `done` callback. The presence of this callback makes the recipe | |
# asynchronous, completing only when `done` has been called. (Kudos to Mocha | |
# for this pattern). If `action` takes one or no arguments then `done` is | |
# called synchronously after `action` returns. | |
async = (action.length > 1) | |
done_cb = (args...) -> | |
recipe.emit.apply null, ["#{id}.complete"].concat(args) | |
wrappedAction = (options) => | |
# recipe.emit or flour.emit? | |
recipe.emit "#{id}.start", options | |
if async | |
action.call(action, options, done_cb) | |
else | |
action.call(action, options) | |
recipe.emit "#{id}.complete" | |
task id, description, wrappedAction | |
return | |
# this object stores the recipe events | |
recipe::_events = new events.EventEmitter() | |
recipe.on = (event, listener) -> | |
recipe::_events.on(event, listener) | |
recipe.emit = (args...) -> | |
recipe::_events.emit.apply(recipe::_events, args) | |
# | |
# Use `recipe` in place of `task` for synchronous tasks. For asynchronous tasks have the `action` argument accept | |
# two arguments, the second being a callback indicating that the asynchronous task has completed eg | |
# | |
recipe 'compile', 'make it go', (options, done) -> | |
compile_async(done) # where do_something_async accepts a callback when completed, in this case `done` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment