Created
June 13, 2011 01:34
-
-
Save josephg/1022202 to your computer and use it in GitHub Desktop.
Love coffeescript
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
# A synchronous processing queue. The queue calls process on the arguments, | |
# ensuring that process() is only executing once at a time. | |
# | |
# process(data, callback) _MUST_ eventually call its callback. | |
# | |
# Example: | |
# | |
# queue = require 'syncqueue' | |
# | |
# fn = queue (data, callback) -> | |
# asyncthing data, -> | |
# callback(321) | |
# | |
# fn(1) | |
# fn(2) | |
# fn(3, (result) -> console.log(result)) | |
# | |
# ^--- async thing will only be running once at any time. | |
module.exports = (process) -> | |
throw new Error('process is not a function') unless typeof process == 'function' | |
queue = [] | |
busy = false | |
flush = -> | |
return if busy or queue.length == 0 | |
busy = true | |
[data, callback] = queue.shift() | |
process data, (result...) -> | |
callback.apply null, result if callback | |
busy = false | |
flush() | |
(data, callback) -> | |
queue.push [data, callback] | |
flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment