Created
September 7, 2011 14:23
-
-
Save sergi/1200696 to your computer and use it in GitHub Desktop.
Test Async Queue + Streams
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
| S = require("./support/jsftp/support/streamer/core"); | |
| function queue() { | |
| var next; | |
| var buffer = Array.prototype.slice.call(arguments) | |
| function stream($, stop) { next = $; stream._update() } | |
| stream._update = function _update() { | |
| buffer.push.apply(buffer, arguments) | |
| if (next && buffer.length) { | |
| if (false !== next(buffer.shift())) _update() | |
| } | |
| } | |
| return stream | |
| } | |
| function enqueue(stream, element) { | |
| stream._update.apply(null, Array.prototype.slice.call(arguments, 1)) | |
| } | |
| var pasvReqs = queue(); | |
| !function recur() { | |
| // Take first element from `pasvReqs` and process it via `processElement` black box that will call | |
| // `recur` once it's done to process next element from the `pasvReqs` queue. | |
| S.head(pasvReqs)(function(element) { | |
| processElement(element, recur) | |
| }) | |
| }() | |
| var processElement = function(el, f) { | |
| var cb = function() { | |
| el.callback(); | |
| f(); | |
| } | |
| setTimeout(cb, parseInt(Math.random() * 1000)); | |
| } | |
| enqueue(pasvReqs, { callback: function() { console.log("1..."); }}); | |
| enqueue(pasvReqs, { callback: function() { console.log("2..."); }}); | |
| enqueue(pasvReqs, { callback: function() { console.log("3..."); }}); |
Author
sergi
commented
Sep 8, 2011
via email
Heh. It worked great, but now I have to deal with an architecture
fuckup in my implementation :)
Thanks!
…On Thu, Sep 8, 2011 at 10:17 AM, Irakli Gozalishvili ***@***.*** wrote:
Dude your comment is empty!
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1200696
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment