This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
771 additions
and
169 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.gitignore | ||
node_modules | ||
contrib |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env node | ||
|
||
function doneLogMsg(msg) { | ||
return function() { | ||
console.log(msg); | ||
}; | ||
} | ||
|
||
var ASQ = require("asynquence"); | ||
var path = require("path"); | ||
require(path.join(__dirname,"contrib.src.js")); | ||
var tests = require(path.join(__dirname,"tests.js"))(doneLogMsg); | ||
|
||
console.log("asynquence-contrib test suite"); | ||
|
||
ASQ.apply(ASQ,tests) | ||
.val(doneLogMsg("ALL CONTRIB TESTS PASSED!")) | ||
.or(function(){ | ||
doneLogMsg("*** TEST SUITE FAILURE ***")(); | ||
for (var i=0; i<arguments.length; i++) { | ||
doneLogMsg(arguments[i] + | ||
(arguments[i] && arguments[i].stack ? arguments[i].stack : "") | ||
)(); | ||
} | ||
process.exit(1); // non-zero exit code! | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "asynquence-contrib", | ||
"version": "0.1.0-a", | ||
"description": "contrib plugins for asynquence", | ||
"main": "./contrib.js", | ||
"scripts": { | ||
"test": "./node-tests.js", | ||
"bundle": "./bundle.js" | ||
}, | ||
"devDependencies": { | ||
"asynquence": "latest", | ||
"uglify-js": "~2.4.8" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/getify/asynquence.git" | ||
}, | ||
"keywords": [ | ||
"async", | ||
"flow-control", | ||
"sequences", | ||
"promise", | ||
"iterator", | ||
"generator" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/getify/asynquence/issues", | ||
"email": "getify@gmail.com" | ||
}, | ||
"homepage": "https://github.com/getify/asynquence/blob/master/contrib", | ||
"author": "Kyle Simpson <getify@gmail.com>", | ||
"license": "MIT" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// "ASQ.iterable()" | ||
ASQ.iterable = function __iterable__() { | ||
var sequence_api, | ||
ARRAY_SLICE = Array.prototype.slice, | ||
brand = "__ASQ__", ø = Object.create(null), | ||
|
||
seq_error = false, | ||
seq_aborted = false, | ||
|
||
seq_tick, | ||
|
||
val_queue = [], | ||
or_queue = [], | ||
|
||
sequence_errors = [] | ||
; | ||
|
||
function schedule(fn) { | ||
return (typeof setImmediate !== "undefined") ? | ||
setImmediate(fn) : setTimeout(fn,0) | ||
; | ||
} | ||
|
||
function notifyErrors() { | ||
var fn; | ||
|
||
seq_tick = null; | ||
|
||
while (or_queue.length > 0) { | ||
fn = or_queue.shift(); | ||
try { | ||
fn.apply(ø,sequence_errors); | ||
} | ||
catch (err) { | ||
if (checkBranding(err)) { | ||
sequence_errors = sequence_errors.concat(err); | ||
} | ||
else { | ||
sequence_errors.push(err); | ||
if (err.stack) { sequence_errors.push(err.stack); } | ||
} | ||
if (or_queue.length === 0) { | ||
console.error.apply(console,sequence_errors); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function val() { | ||
if (seq_error || seq_aborted || arguments.length === 0) { | ||
return sequence_api; | ||
} | ||
|
||
val_queue.push.apply(val_queue,arguments); | ||
|
||
return sequence_api; | ||
} | ||
|
||
function or() { | ||
if (seq_aborted || arguments.length === 0) { | ||
return sequence_api; | ||
} | ||
|
||
or_queue.push.apply(or_queue,arguments); | ||
|
||
return sequence_api; | ||
} | ||
|
||
function next() { | ||
var ret = { value: undefined, done: true }; | ||
|
||
if (seq_error || seq_aborted) { | ||
throwErr("Sequence cannot be iterated"); | ||
return ret; | ||
} | ||
|
||
if (val_queue.length > 0) { | ||
try { | ||
ret.value = val_queue.shift().apply(ø,arguments); | ||
} | ||
catch (err) { | ||
if (ASQ.isMessageWrapper(err)) { | ||
throwErr.apply(ø,err); | ||
} | ||
else if (err.stack) { | ||
throwErr(err,err.stack); | ||
} | ||
else { | ||
throwErr(err); | ||
} | ||
} | ||
ret.done = (val_queue.length === 0); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
function throwErr() { | ||
if (seq_error || seq_aborted) { | ||
return sequence_api; | ||
} | ||
|
||
sequence_errors.push.apply(sequence_errors,arguments); | ||
seq_error = true; | ||
if (!seq_tick) { | ||
seq_tick = schedule(notifyErrors); | ||
} | ||
|
||
return sequence_api; | ||
} | ||
|
||
function abort() { | ||
if (seq_error || seq_aborted) { | ||
return; | ||
} | ||
|
||
seq_aborted = true; | ||
|
||
clearTimeout(seq_tick); | ||
seq_tick = null; | ||
val_queue.length = 0; | ||
or_queue.length = 0; | ||
sequence_errors.length = 0; | ||
} | ||
|
||
|
||
// *********************************************** | ||
// Object branding utilities | ||
// *********************************************** | ||
function brandIt(obj) { | ||
Object.defineProperty(obj,brand,{ | ||
enumerable: false, | ||
value: true | ||
}); | ||
|
||
return obj; | ||
} | ||
|
||
|
||
// *********************************************** | ||
// Setup the ASQ.iterable() public API | ||
// *********************************************** | ||
sequence_api = brandIt({ | ||
val: val, | ||
then: val, | ||
or: or, | ||
next: next, | ||
"throw": throwErr, | ||
abort: abort | ||
}); | ||
|
||
// treat ASQ.iterable() constructor parameters as having been | ||
// passed to `val()` | ||
sequence_api.val.apply(ø,arguments); | ||
|
||
return sequence_api; | ||
}; |
Oops, something went wrong.