asynquence
Advanced tools
Comparing version 0.3.1-a to 0.3.2-a
/*! asynquence | ||
v0.3.1-a (c) Kyle Simpson | ||
v0.3.2-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
*/ | ||
!function(n,e,t){"undefined"!=typeof module&&module.exports?module.exports=t():"function"==typeof define&&define.amd?define(t):e[n]=t(n,e)}("ASQ",this,function(n,e){"use strict";function t(n){return"undefined"!=typeof setImmediate?setImmediate(n):setTimeout(n,0)}function r(){function n(){clearTimeout(j),j=null,I.length=0,Q.length=0,w.length=0,C.length=0}function e(){return S?l():(j||(j=t(l)),void 0)}function l(){var t,r;if(j=null,S)n();else if(O)for(;Q.length;){t=Q.shift();try{t.apply(m,C)}catch(u){a(u)?C=C.concat(u):(C.push(u),u.stack&&C.push(u.stack)),0===Q.length&&console.error.apply(console,C)}}else if(T&&I.length>0){T=!1,t=I.shift(),r=w.slice(),w.length=0,r.unshift(p());try{t.apply(m,r)}catch(u){a(u)?C=C.concat(u):C.push(u),O=!0,e()}}}function p(){function n(){O||S||T||(T=!0,w.push.apply(w,arguments),C.length=0,e())}return n.fail=function(){O||S||T||(O=!0,w.length=0,C.push.apply(C,arguments),e())},n.abort=function(){O||S||(T=!1,S=!0,w.length=0,C.length=0,e())},n}function h(n,e,r){function u(){clearTimeout(y),y=_=q=h=null}function l(){return v?c():(y||(y=t(c)),void 0)}function c(){if(!(O||S||b)){var e=[];y=null,d?(n.fail.apply(m,h),u()):v?(n.abort(),u()):i()&&(b=!0,_.forEach(function(n,t){e.push(q["s"+t])}),n.apply(m,e),u())}}function i(){if(!(O||S||d||v||b||0===_.length)){var n=!0;return _.some(function(e){return null===e?(n=!1,!0):void 0}),n}}function o(){function n(){if(!(O||S||d||v||b||_[e])){var n=f.messages.apply(m,arguments);q["s"+e]=n.length>1?n:n[0],_[e]=!0,l()}}var e=_.length;return n.fail=function(){O||S||d||v||b||_[e]||(d=!0,h=g.call(arguments),l())},n.abort=function(){O||S||d||v||b||(v=!0,c())},_[e]=null,n}var s,p,h,y,d=!1,v=!1,b=!1,_=[],q={};e.some(function(n){if(d||v)return!0;s=r.slice(),s.unshift(o());try{n.apply(m,s)}catch(e){return p=e,d=!0,!0}}),p&&(a(p)?n.fail.apply(m,p):n.fail(p))}function y(){return O||S||0===arguments.length?M:(I.push.apply(I,o(arguments,i)),e(),M)}function d(){return S||0===arguments.length?M:(Q.push.apply(Q,arguments),e(),M)}function v(){if(O||S||0===arguments.length)return M;var n=g.call(arguments);return y(function(e){var t=g.call(arguments,1);h(e,n,t)}),M}function b(){return S||0===arguments.length?M:(g.call(arguments).forEach(function(n){y(function(e){n.apply(m,g.call(arguments,1)),e()}).or(n.fail)}),M)}function _(){return O||S||0===arguments.length?M:(g.call(arguments).forEach(function(n){var e;a(n)&&"next"in n&&(n.then(function(){e.apply(m,arguments)}).or(function(){e.fail.apply(m,arguments)}),n=r(function(n){e=n}),e=function(){n=r.apply(m,arguments)},e.fail=function(){var e=g.call(arguments);n=r(function(n){n.fail.apply(m,e)})}),y(function(e){a(n)||(n=n.apply(m,g.call(arguments,1))),n.pipe(e)})}),M)}function q(){return O||S||0===arguments.length?M:(g.call(o(arguments,c)).forEach(function(n){y(function(e){var t=n.apply(m,g.call(arguments,1));a(t)||(t=f.messages(t)),e.apply(m,t)})}),M)}function k(){return O||S||0===arguments.length?M:(g.call(arguments).forEach(function(n){y(function(e){"function"!=typeof n||"then"in n||(n=n.apply(m,g.call(arguments,1))),n.then(e,e.fail)})}),M)}function E(){return O?M:(S=!0,l(),M)}function x(n,e){var t=arguments.length>1;switch(n){case"seq_error":if(!t)return O;O=e;break;case"seq_aborted":if(!t)return S;S=e;break;case"then_ready":if(!t)return T;T=e;break;case"then_queue":return I;case"or_queue":return Q;case"sequence_messages":return w;case"sequence_errors":return C}}function A(){Object.keys(s).forEach(function(n){M[n]=s[n](M,x)})}var j,O=!1,S=!1,T=!0,I=[],Q=[],w=[],C=[],M=u({then:y,or:d,gate:v,pipe:b,seq:_,val:q,promise:k,abort:E});return A(),M.then.apply(m,o(arguments,i)),M}function u(n){return Object.defineProperty(n,h,{enumerable:!1,value:!0}),n}function a(n){return null!=n&&"object"==typeof n&&n[h]}function l(n,e){return g.call(e).slice(1,n+1)}function c(n){return f.messages.apply(m,l(n,arguments))}function i(n){arguments[n+1].apply(m,l(n,arguments))}function o(n,e){var t,r;for(n=g.call(n),t=0;t<n.length;t++)if(Array.isArray(n[t])&&a(n[t]))n[t]=e.bind.apply(e,[null,n[t].length].concat(n[t]));else if("function"!=typeof n[t]){for(r=t+1;r<n.length&&("function"!=typeof n[r]&&!a(n[r]));r++);n.splice(t,r-t,e.bind.apply(e,[null,r-t].concat(n.slice(t,r))))}return n}var f,s={},p=(e||{})[n],g=Array.prototype.slice,h="__ASQ__",m=Object.create(null);return f=r,f.extend=function(n,e){return~["then","or","gate","pipe","seq","val","abort"].indexOf(n)||(s[n]=e),f},f.messages=function(){var n=g.call(arguments);return u(n),n},f.isMessageWrapper=f.isSequence=a,f.noConflict=function(){return e&&(e[n]=p),f},f}); | ||
!function(n,e,t){"undefined"!=typeof module&&module.exports?module.exports=t():"function"==typeof define&&define.amd?define(t):e[n]=t(n,e)}("ASQ",this,function(n,e){"use strict";function t(n){return"undefined"!=typeof setImmediate?setImmediate(n):setTimeout(n,0)}function r(){function n(){clearTimeout(j),j=null,I.length=0,Q.length=0,w.length=0,C.length=0}function e(){return S?l():(j||(j=t(l)),void 0)}function l(){var t,r;if(j=null,S)n();else if(O)for(;Q.length;){t=Q.shift();try{t.apply(m,C)}catch(u){a(u)?C=C.concat(u):(C.push(u),u.stack&&C.push(u.stack)),0===Q.length&&console.error.apply(console,C)}}else if(T&&I.length>0){T=!1,t=I.shift(),r=w.slice(),w.length=0,r.unshift(p());try{t.apply(m,r)}catch(u){a(u)?C=C.concat(u):C.push(u),O=!0,e()}}}function p(){function n(){O||S||T||(T=!0,w.push.apply(w,arguments),C.length=0,e())}return n.fail=function(){O||S||T||(O=!0,w.length=0,C.push.apply(C,arguments),e())},n.abort=function(){O||S||(T=!1,S=!0,w.length=0,C.length=0,e())},n.errfcb=function(e){e?n.fail(e):n.apply(m,g.call(arguments,1))},n}function h(n,e,r){function u(){clearTimeout(y),y=_=q=h=null}function l(){return v?c():(y||(y=t(c)),void 0)}function c(){if(!(O||S||b)){var e=[];y=null,d?(n.fail.apply(m,h),u()):v?(n.abort(),u()):i()&&(b=!0,_.forEach(function(n,t){e.push(q["s"+t])}),n.apply(m,e),u())}}function i(){if(!(O||S||d||v||b||0===_.length)){var n=!0;return _.some(function(e){return null===e?(n=!1,!0):void 0}),n}}function o(){function n(){if(!(O||S||d||v||b||_[e])){var n=f.messages.apply(m,arguments);q["s"+e]=n.length>1?n:n[0],_[e]=!0,l()}}var e=_.length;return n.fail=function(){O||S||d||v||b||_[e]||(d=!0,h=g.call(arguments),l())},n.abort=function(){O||S||d||v||b||(v=!0,c())},n.errfcb=function(e){e?n.fail(e):n.apply(m,g.call(arguments,1))},_[e]=null,n}var s,p,h,y,d=!1,v=!1,b=!1,_=[],q={};e.some(function(n){if(d||v)return!0;s=r.slice(),s.unshift(o());try{n.apply(m,s)}catch(e){return p=e,d=!0,!0}}),p&&(a(p)?n.fail.apply(m,p):n.fail(p))}function y(){return O||S||0===arguments.length?M:(I.push.apply(I,o(arguments,i)),e(),M)}function d(){return S||0===arguments.length?M:(Q.push.apply(Q,arguments),e(),M)}function v(){if(O||S||0===arguments.length)return M;var n=g.call(arguments);return y(function(e){var t=g.call(arguments,1);h(e,n,t)}),M}function b(){return S||0===arguments.length?M:(g.call(arguments).forEach(function(n){y(function(e){n.apply(m,g.call(arguments,1)),e()}).or(n.fail)}),M)}function _(){return O||S||0===arguments.length?M:(g.call(arguments).forEach(function(n){var e;a(n)&&"next"in n&&(n.then(function(){e.apply(m,arguments)}).or(function(){e.fail.apply(m,arguments)}),n=r(function(n){e=n}),e=function(){n=r.apply(m,arguments)},e.fail=function(){var e=g.call(arguments);n=r(function(n){n.fail.apply(m,e)})}),y(function(e){a(n)||(n=n.apply(m,g.call(arguments,1))),n.pipe(e)})}),M)}function q(){return O||S||0===arguments.length?M:(g.call(o(arguments,c)).forEach(function(n){y(function(e){var t=n.apply(m,g.call(arguments,1));a(t)||(t=f.messages(t)),e.apply(m,t)})}),M)}function k(){return O||S||0===arguments.length?M:(g.call(arguments).forEach(function(n){y(function(e){"function"!=typeof n||"then"in n||(n=n.apply(m,g.call(arguments,1))),n.then(e,e.fail)})}),M)}function E(){return O?M:(S=!0,l(),M)}function x(n,e){var t=arguments.length>1;switch(n){case"seq_error":if(!t)return O;O=e;break;case"seq_aborted":if(!t)return S;S=e;break;case"then_ready":if(!t)return T;T=e;break;case"then_queue":return I;case"or_queue":return Q;case"sequence_messages":return w;case"sequence_errors":return C}}function A(){Object.keys(s).forEach(function(n){M[n]=s[n](M,x)})}var j,O=!1,S=!1,T=!0,I=[],Q=[],w=[],C=[],M=u({then:y,or:d,gate:v,pipe:b,seq:_,val:q,promise:k,abort:E});return A(),M.then.apply(m,o(arguments,i)),M}function u(n){return Object.defineProperty(n,h,{enumerable:!1,value:!0}),n}function a(n){return null!=n&&"object"==typeof n&&n[h]}function l(n,e){return g.call(e).slice(1,n+1)}function c(n){return f.messages.apply(m,l(n,arguments))}function i(n){arguments[n+1].apply(m,l(n,arguments))}function o(n,e){var t,r;for(n=g.call(n),t=0;t<n.length;t++)if(Array.isArray(n[t])&&a(n[t]))n[t]=e.bind.apply(e,[null,n[t].length].concat(n[t]));else if("function"!=typeof n[t]){for(r=t+1;r<n.length&&("function"!=typeof n[r]&&!a(n[r]));r++);n.splice(t,r-t,e.bind.apply(e,[null,r-t].concat(n.slice(t,r))))}return n}var f,s={},p=(e||{})[n],g=Array.prototype.slice,h="__ASQ__",m=Object.create(null);return f=r,f.extend=function(n,e){return~["then","or","gate","pipe","seq","val","abort"].indexOf(n)||(s[n]=e),f},f.messages=function(){var n=g.call(arguments);return u(n),n},f.isMessageWrapper=f.isSequence=a,f.noConflict=function(){return e&&(e[n]=p),f},f}); |
/*! asynquence | ||
v0.3.1-a (c) Kyle Simpson | ||
v0.3.2-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
@@ -137,2 +137,12 @@ */ | ||
// handles "error-first" (aka "node-style") callbacks | ||
done.errfcb = function __errorfirst_callback__(err){ | ||
if (err) { | ||
done.fail(err); | ||
} | ||
else { | ||
done.apply(ø,ARRAY_SLICE.call(arguments,1)); | ||
} | ||
}; | ||
return done; | ||
@@ -265,2 +275,12 @@ } | ||
// handles "error-first" (aka "node-style") callbacks | ||
done.errfcb = function __errorfirst_callback__(err){ | ||
if (err) { | ||
done.fail(err); | ||
} | ||
else { | ||
done.apply(ø,ARRAY_SLICE.call(arguments,1)); | ||
} | ||
}; | ||
// placeholder for when a gate-segment completes | ||
@@ -396,3 +416,3 @@ segment_completion[segment_completion_idx] = null; | ||
// wrap a normal sequence around the iterable sequence, | ||
// which when called replaces the temporary `trigger` | ||
// which when called replaces the temporary `trigger` | ||
// (defined below) with a real sequence trigger | ||
@@ -399,0 +419,0 @@ fn = createSequence(function __create_sequence__(done){ |
{ | ||
"name": "asynquence", | ||
"version": "0.3.1-a", | ||
"version": "0.3.2-a", | ||
"description": "asynquence: async sequences & gates for flow-control", | ||
@@ -5,0 +5,0 @@ "main": "./asq.js", |
@@ -9,10 +9,11 @@ # asynquence | ||
* [Sequences & gates](https://gist.github.com/getify/5959149), at a glance. | ||
* Example/explanation of [promise-style sequences](https://gist.github.com/jakearchibald/0e652d95c07442f205ce#comment-977119). | ||
* More advanced example of ["nested" composition of sequences](https://gist.github.com/getify/10273f3de07dda27ebce). | ||
* [Iterable sequences](#iterable-sequences): [sync loop](https://gist.github.com/getify/8211148#file-ex1-sync-iteration-js) and [async loop](https://gist.github.com/getify/8211148#file-ex2-async-iteration-js) | ||
* [Sequences & gates](https://gist.github.com/getify/5959149), at a glance | ||
* Refactoring ["callback hell"-style to asynquence](https://gist.github.com/getify/8459026) | ||
* Example/explanation of [promise-style sequences](https://gist.github.com/jakearchibald/0e652d95c07442f205ce#comment-977119) | ||
* More advanced example of ["nested" composition of sequences](https://gist.github.com/getify/10273f3de07dda27ebce) | ||
* [Iterable sequences](#iterable-sequences): [sync loop](https://gist.github.com/getify/8211148#file-ex1-sync-iteration-js) and [async loop](https://gist.github.com/getify/8211148#file-ex2-async-iteration-js) and [async batch iteration of list](https://gist.github.com/getify/8464917) | ||
* API [Usage Examples](#usage-examples) | ||
### Sequences | ||
If you want to perform two or more asynchronous tasks one after the other (like animation delays, XHR calls, etc). You want to set up an ordered series of tasks and make sure the previous one finishes before the next one is processed. You need a **sequence**. | ||
Say you want to perform two or more asynchronous tasks one after the other (like animation delays, XHR calls, file I/O, etc). You need to set up an ordered series of tasks and make sure the previous one finishes before the next one is processed. You need a **sequence**. | ||
@@ -55,3 +56,3 @@ You create a sequence by calling `ASQ(...)`. **Each time you call `ASQ()`, you create a new, separate sequence.** | ||
`val(Fn)` is sugar short-hand for `then(function(done){ done(Fn.apply(null,[].slice.call(arguments,1))); }) | ||
`val(Fn)` is sugar short-hand for `then(function(done){ done(Fn.apply(null,[].slice.call(arguments,1))); })`. | ||
@@ -66,2 +67,8 @@ This method will also accept non-function values as sequence value-messages. `val(Va)` is sugar short-hand for `then(function(done){ done(Va); })`. | ||
* `errfcb` is a flag on the triggers that are passed into `then(..)` steps and `gate(..)` segments. If you're using methods which expect an "error-first" style (aka, "node-style") callback, `{trigger}.errcb` provides a properly formatted callback for the occasion. | ||
If the "error-first" callback is then invoked with the first ("error") parameter set, the main sequence is flagged for error as usual. Otherwise, the main sequence proceeds as success. Messages sent to the callback are passed through to the main sequence as success/error as expected. | ||
`ASQ(function(done){ somethingAsync(done.errcb); })` is sugar short-hand for `ASQ(function(done){ somethingAsync(function(err){ if (err) done.fail(err); else done.apply(null,[].slice.call(arguments,1))}); })`. | ||
You can also `abort()` a sequence at any time, which will prevent any further actions from occurring on that sequence (all callbacks will be ignored). The call to `abort()` can happen on the sequence API itself, or using the `abort` flag on a completion trigger in any step (see example below). | ||
@@ -68,0 +75,0 @@ |
120
tests.js
@@ -27,2 +27,3 @@ (function(name,context,dependency,definition){ | ||
var ARRAY_SLICE = Array.prototype.slice; | ||
var ø = Object.create(null); | ||
var tests = []; | ||
@@ -848,3 +849,3 @@ | ||
function Pr(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
var args = ASQ.messages.apply(ø,arguments); | ||
var def = Q.defer(); | ||
@@ -857,4 +858,4 @@ setTimeout(function(){ | ||
function Br(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
function bPr(){ | ||
var args = ASQ.messages.apply(ø,arguments); | ||
var def = Q.defer(); | ||
@@ -886,2 +887,3 @@ setTimeout(function(){ | ||
Array.isArray(msg) && | ||
ASQ.isMessageWrapper(msg) && | ||
msg.length === 2 && | ||
@@ -898,3 +900,3 @@ msg[0] === "Hello" && | ||
}) | ||
.promise(Br) // Note: a broken promise! | ||
.promise(bPr) // Note: a broken promise! | ||
.val(function(msg1,msg2){ | ||
@@ -928,2 +930,112 @@ clearTimeout(timeout); | ||
function Ef(err,msg,delay,cb) { | ||
setTimeout(function(){ | ||
if (!Array.isArray(err)) err = [err]; | ||
if (!Array.isArray(msg)) msg = [msg]; | ||
msg = err.concat(msg); | ||
cb.apply(ø,msg); | ||
},delay); | ||
} | ||
ASQ(function(done){ | ||
Ef(/*err=*/void 0,/*success=*/["Yay","Man"],100,done.errfcb); | ||
}) | ||
.val(function(msg1,msg2){ | ||
if (!( | ||
arguments.length === 2 && | ||
msg1 === "Yay" && | ||
msg2 === "Man" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}) | ||
.gate( | ||
function(done){ | ||
Ef(/*err=*/void 0,/*success=*/void 0,100,done.errfcb); | ||
}, | ||
function(done){ | ||
Ef(/*err=*/void 0,/*success=*/"Hello",200,done.errfcb); | ||
}, | ||
function(done){ | ||
done.errfcb(/*err=*/void 0,/*success=*/"World","!"); | ||
} | ||
) | ||
.val(function(msg1,msg2,msg3){ | ||
if (!( | ||
arguments.length === 3 && | ||
msg1 === undefined && | ||
msg2 === "Hello" && | ||
Array.isArray(msg3) && | ||
ASQ.isMessageWrapper(msg3) && | ||
msg3.length === 2 && | ||
msg3[0] === "World" && | ||
msg3[1] === "!" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}) | ||
.then(function(mainDone){ | ||
ASQ(function(done){ | ||
// force an "error" on this inner sequence | ||
Ef(/*err=*/"Boo",/*success=*/"Ignored",100,done.errfcb); | ||
}) | ||
.then(function(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
}) | ||
.or(function(){ | ||
mainDone.apply(ø,arguments); | ||
}); | ||
}) | ||
.val(function(msg){ | ||
if (!( | ||
arguments.length === 1 && | ||
msg === "Boo" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}) | ||
.gate( | ||
function(done){ | ||
done("Ignored"); | ||
}, | ||
function(done){ | ||
Ef(/*err=*/"All done",/*success=*/"Ignored 2",100,done.errfcb); | ||
} | ||
) | ||
.val(function(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
}) | ||
.or(function(msg){ | ||
clearTimeout(timeout); | ||
if ( | ||
arguments.length === 1 && | ||
msg === "All done" | ||
) { | ||
PASS(testDone,label); | ||
} | ||
else { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}); | ||
timeout = setTimeout(function(){ | ||
FAIL(testDone,label + " (from timeout)"); | ||
},1000); | ||
}); | ||
tests.push(function(testDone){ | ||
var label = "Core Test #22", timeout; | ||
// testing a custom plugin which will pass along | ||
@@ -930,0 +1042,0 @@ // any messages received to it, but will inject |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66816
1689
395