asynquence
Advanced tools
Comparing version 0.3.5-d to 0.3.6-a
/*! asynquence | ||
v0.3.5-d (c) Kyle Simpson | ||
v0.3.6-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 u(){function n(){clearTimeout(I),I=null,w.length=0,C.length=0,P.length=0,z.length=0}function e(){return Q?l():(I||(I=t(l)),void 0)}function l(){var t,u;if(I=null,delete B.unpause,Q)n();else if(M)for(;C.length;){t=C.shift();try{t.apply(y,z)}catch(r){a(r)?z=z.concat(r):(z.push(r),r.stack&&z.push(r.stack)),0===C.length&&console.error.apply(console,z)}}else if(W&&w.length>0){W=!1,t=w.shift(),u=P.slice(),P.length=0,u.unshift(g());try{t.apply(y,u)}catch(r){a(r)?z=z.concat(r):z.push(r),M=!0,e()}}}function g(){function n(){M||Q||W||(W=!0,P.push.apply(P,arguments),z.length=0,e())}return n.fail=function(){M||Q||W||(M=!0,P.length=0,z.push.apply(z,arguments),e())},n.abort=function(){M||Q||(W=!1,Q=!0,P.length=0,z.length=0,e())},n.errfcb=function(e){e?n.fail(e):n.apply(y,m.call(arguments,1))},n}function h(n,e,u){function r(){clearTimeout(h),h=_=q=g=null}function l(){return v?i():(h||(h=t(i)),void 0)}function i(){if(!(M||Q||b)){var e=[];h=null,d?(n.fail.apply(y,g),r()):v?(n.abort(),r()):c()&&(b=!0,_.forEach(function(n,t){e.push(q["s"+t])}),n.apply(y,e),r())}}function c(){if(!(M||Q||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(!(M||Q||d||v||b||_[e])){var n=s.messages.apply(y,arguments);q["s"+e]=n.length>1?n:n[0],_[e]=!0,l()}}var e=_.length;return n.fail=function(){M||Q||d||v||b||_[e]||(d=!0,g=m.call(arguments),l())},n.abort=function(){M||Q||d||v||b||(v=!0,i())},n.errfcb=function(e){e?n.fail(e):n.apply(y,m.call(arguments,1))},_[e]=null,n}var f,p,g,h,d=!1,v=!1,b=!1,_=[],q={};e.some(function(n){if(d||v)return!0;f=u.slice(),f.unshift(o());try{n.apply(y,f)}catch(e){return p=e,d=!0,!0}}),p&&(a(p)?n.fail.apply(y,p):n.fail(p))}function d(){return M||Q||0===arguments.length?B:(w.push.apply(w,o(arguments,c)),e(),B)}function v(){return Q||0===arguments.length?B:(C.push.apply(C,arguments),e(),B)}function b(){if(M||Q||0===arguments.length)return B;var n=m.call(arguments);return d(function(e){var t=m.call(arguments,1);h(e,n,t)}),B}function _(){return Q||0===arguments.length?B:(m.call(arguments).forEach(function(n){d(function(e){n.apply(y,m.call(arguments,1)),e()}).or(n.fail)}),B)}function q(){return M||Q||0===arguments.length?B:(m.call(arguments).forEach(function(n){var e;a(n)&&"next"in n&&(n.then(function(){e.apply(y,arguments)}).or(function(){e.fail.apply(y,arguments)}),n=u(function(n){e=n}),e=function(){n=u.apply(y,arguments)},e.fail=function(){var e=m.call(arguments);n=u(function(n){n.fail.apply(y,e)})}),d(function(e){a(n)||(n=n.apply(y,m.call(arguments,1))),n.pipe(e)})}),B)}function A(){return M||Q||0===arguments.length?B:(m.call(o(arguments,i)).forEach(function(n){d(function(e){var t=n.apply(y,m.call(arguments,1));a(t)||(t=s.messages(t)),e.apply(y,t)})}),B)}function k(){function n(n){return function(){n.apply(y,s.isMessageWrapper(arguments[0])?arguments[0]:arguments)}}return M||Q||0===arguments.length?B:(m.call(arguments).forEach(function(e){d(function(t){"function"!=typeof e||"then"in e||(e=e.apply(y,m.call(arguments,1))),e.then(n(t),n(t.fail))})}),B)}function E(){var n;return A(function(){return n?n.apply(y,arguments):n=u.apply(y,arguments),s.messages.apply(y,arguments)}),v(function(){if(n)n.fail.apply(y,arguments);else{var e=m.call(arguments);n=u().then(function(n){n.fail.apply(y,e)})}}),u().then(function(e){n?n.pipe(e):n=e})}function x(){return M?B:(Q=!0,l(),B)}function j(){var n;return f={then_queue:w.slice(0),or_queue:C.slice(0)},n=u(),f=null,n}function O(){P.push.apply(P,arguments),I===!0&&(I=null),e()}function S(n,e){var t=arguments.length>1;switch(n){case"seq_error":if(!t)return M;M=e;break;case"seq_aborted":if(!t)return Q;Q=e;break;case"then_ready":if(!t)return W;W=e;break;case"then_queue":return w;case"or_queue":return C;case"sequence_messages":return P;case"sequence_errors":return z}}function T(){Object.keys(p).forEach(function(n){B[n]=p[n](B,S)})}var I,M=!1,Q=!1,W=!0,w=[],C=[],P=[],z=[],B=r({then:d,or:v,gate:b,pipe:_,seq:q,val:A,promise:k,fork:E,abort:x,duplicate:j});return T(),f&&(w=f.then_queue.slice(0),C=f.or_queue.slice(0),B.unpause=O,I=!0),B.then.apply(y,o(arguments,c)),B}function r(n){return Object.defineProperty(n,h,{enumerable:!1,value:!0})}function a(n){return null!=n&&"object"==typeof n&&n[h]}function l(n,e){return m.call(e).slice(1,n+1)}function i(n){return s.messages.apply(y,l(n,arguments))}function c(n){arguments[n+1].apply(y,l(n,arguments))}function o(n,e){var t,u;for(n=m.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(u=t+1;u<n.length&&("function"!=typeof n[u]&&!a(n[u]));u++);n.splice(t,u-t,e.bind.apply(e,[null,u-t].concat(n.slice(t,u))))}return n}var s,f,p={},g=(e||{})[n],m=Array.prototype.slice,h="__ASQ__",y=Object.create(null);return s=u,s.extend=function(n,e){return~["then","or","gate","pipe","seq","val","abort"].indexOf(n)||(p[n]=e),s},s.messages=function(){var n=m.call(arguments);return r(n),n},s.isSequence=function(n){return a(n)&&!Array.isArray(n)},s.isMessageWrapper=function(n){return a(n)&&Array.isArray(n)},s.unpause=function(n){return n.unpause&&n.unpause(),n},s.noConflict=function(){return e&&(e[n]=g),s},s}); | ||
!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(){}function r(n){return"undefined"!=typeof setImmediate?setImmediate(n):setTimeout(n,0)}function u(){function n(){return W?i():(I||(I=r(i)),void 0)}function e(){throw 1===D.length?D[0]:D}function i(){var t,r;if(I=null,delete F.unpause,W)clearTimeout(I),I=null,P.length=0,z.length=0,B.length=0,D.length=0;else if(M)for(0!==z.length||Q||(Q=!0,e());z.length;){Q=!0,t=z.shift();try{t.apply(d,D)}catch(u){l(u)?D=D.concat(u):(D.push(u),u.stack&&D.push(u.stack)),0===z.length&&e()}}else if(C&&P.length>0){C=!1,t=P.shift(),r=B.slice(),B.length=0,r.unshift(h());try{t.apply(d,r)}catch(u){l(u)?D=D.concat(u):D.push(u),M=!0,n()}}}function h(){function e(){M||W||C||(C=!0,B.push.apply(B,arguments),D.length=0,n())}return e.fail=function(){M||W||C||(M=!0,B.length=0,D.push.apply(D,arguments),n())},e.abort=function(){M||W||(C=!1,W=!0,B.length=0,D.length=0,n())},e.errfcb=function(n){n?e.fail(n):e.apply(d,m.call(arguments,1))},e}function y(n,e,t){function u(){clearTimeout(h),h=_=q=g=null}function a(){return v?i():(h||(h=r(i)),void 0)}function i(){if(!(M||W||b)){var e=[];h=null,y?(n.fail.apply(d,g),u()):v?(n.abort(),u()):c()&&(b=!0,_.forEach(function(n,t){e.push(q["s"+t])}),n.apply(d,e),u())}}function c(){if(!(M||W||y||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(!(M||W||y||v||b||_[e])){var n=s.messages.apply(d,arguments);q["s"+e]=n.length>1?n:n[0],_[e]=!0,a()}}var e=_.length;return n.fail=function(){M||W||y||v||b||_[e]||(y=!0,g=m.call(arguments),a())},n.abort=function(){M||W||y||v||b||(v=!0,i())},n.errfcb=function(e){e?n.fail(e):n.apply(d,m.call(arguments,1))},_[e]=null,n}var f,p,g,h,y=!1,v=!1,b=!1,_=[],q={};e.some(function(n){if(y||v)return!0;f=t.slice(),f.unshift(o());try{n.apply(d,f)}catch(e){return p=e,y=!0,!0}}),p&&(l(p)?n.fail.apply(d,p):n.fail(p))}function v(){return M||W||0===arguments.length?F:(P.push.apply(P,f(arguments,o)),n(),F)}function b(){return W||0===arguments.length?F:(z.push.apply(z,arguments),n(),F)}function _(){if(M||W||0===arguments.length)return F;var n=m.call(arguments);return v(function(e){var t=m.call(arguments,1);y(e,n,t)}),F}function q(){return W||0===arguments.length?F:(m.call(arguments).forEach(function(n){v(function(e){n.apply(d,m.call(arguments,1)),e()}).or(n.fail)}),F)}function A(){return M||W||0===arguments.length?F:(m.call(arguments).forEach(function(n){var e;l(n)&&"next"in n&&(n.then(function(){e.apply(d,arguments)}).or(function(){e.fail.apply(d,arguments)}),n=u(function(n){e=n}).or(t),e=function(){n=u.apply(d,arguments).or(t)},e.fail=function(){var e=m.call(arguments);n=u(function(n){n.fail.apply(d,e)}).or(t)}),v(function(e){l(n)||(n=n.apply(d,m.call(arguments,1))),n.pipe(e)})}),F)}function k(){return M||W||0===arguments.length?F:(m.call(f(arguments,c)).forEach(function(n){v(function(e){var t=n.apply(d,m.call(arguments,1));l(t)||(t=s.messages(t)),e.apply(d,t)})}),F)}function E(){function n(n){return function(){n.apply(d,s.isMessageWrapper(arguments[0])?arguments[0]:arguments)}}return M||W||0===arguments.length?F:(m.call(arguments).forEach(function(e){v(function(t){"function"!=typeof e||"then"in e||(e=e.apply(d,m.call(arguments,1))),e.then(n(t),n(t.fail))})}),F)}function x(){var n;return k(function(){return n?n.apply(d,arguments):n=u.apply(d,arguments).or(t),s.messages.apply(d,arguments)}),b(function(){if(n)n.fail.apply(d,arguments);else{var e=m.call(arguments);n=u().then(function(n){n.fail.apply(d,e)}).or(t)}}),u().then(function(e){n?n.pipe(e):n=e}).or(t)}function j(){return M?F:(W=!0,i(),F)}function O(){var n;return p={then_queue:P.slice(0),or_queue:z.slice(0)},n=u(),p=null,n}function S(){B.push.apply(B,arguments),I===!0&&(I=null),n()}function T(n,e){var t=arguments.length>1;switch(n){case"seq_error":if(!t)return M;M=e;break;case"seq_aborted":if(!t)return W;W=e;break;case"then_ready":if(!t)return C;C=e;break;case"then_queue":return P;case"or_queue":return z;case"sequence_messages":return B;case"sequence_errors":return D}}function w(){Object.keys(g).forEach(function(n){F[n]=g[n](F,T)})}var I,M=!1,Q=!1,W=!1,C=!0,P=[],z=[],B=[],D=[],F=a({then:v,or:b,gate:_,pipe:q,seq:A,val:k,promise:E,fork:x,abort:j,duplicate:O});return w(),p&&(P=p.then_queue.slice(0),z=p.or_queue.slice(0),F.unpause=S,I=!0),F.then.apply(d,f(arguments,o)),F}function a(n){return Object.defineProperty(n,y,{enumerable:!1,value:!0})}function l(n){return null!=n&&"object"==typeof n&&n[y]}function i(n,e){return m.call(e).slice(1,n+1)}function c(n){return s.messages.apply(d,i(n,arguments))}function o(n){arguments[n+1].apply(d,i(n,arguments))}function f(n,e){var t,r;for(n=m.call(n),t=0;t<n.length;t++)if(Array.isArray(n[t])&&l(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]&&!l(n[r]));r++);n.splice(t,r-t,e.bind.apply(e,[null,r-t].concat(n.slice(t,r))))}return n}var s,p,g={},h=(e||{})[n],m=Array.prototype.slice,y="__ASQ__",d=Object.create(null);return s=u,s.extend=function(n,e){return~["then","or","gate","pipe","seq","val","abort"].indexOf(n)||(g[n]=e),s},s.messages=function(){var n=m.call(arguments);return a(n),n},s.isSequence=function(n){return l(n)&&!Array.isArray(n)},s.isMessageWrapper=function(n){return l(n)&&Array.isArray(n)},s.unpause=function(n){return n.unpause&&n.unpause(),n},s.noConflict=function(){return e&&(e[n]=h),s},s}); |
/*! asynquence | ||
v0.3.5-d (c) Kyle Simpson | ||
v0.3.6-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
@@ -13,2 +13,4 @@ */ | ||
function ignored(){} | ||
function schedule(fn) { | ||
@@ -22,11 +24,2 @@ return (typeof setImmediate !== "undefined") ? | ||
function resetSequence() { | ||
clearTimeout(seq_tick); | ||
seq_tick = null; | ||
then_queue.length = 0; | ||
or_queue.length = 0; | ||
sequence_messages.length = 0; | ||
sequence_errors.length = 0; | ||
} | ||
function scheduleSequenceTick() { | ||
@@ -42,2 +35,6 @@ if (seq_aborted) { | ||
function throwSequenceErrors() { | ||
throw (sequence_errors.length === 1 ? sequence_errors[0] : sequence_errors); | ||
} | ||
function sequenceTick() { | ||
@@ -51,6 +48,17 @@ var fn, args; | ||
if (seq_aborted) { | ||
resetSequence(); | ||
clearTimeout(seq_tick); | ||
seq_tick = null; | ||
then_queue.length = 0; | ||
or_queue.length = 0; | ||
sequence_messages.length = 0; | ||
sequence_errors.length = 0; | ||
} | ||
else if (seq_error) { | ||
if (or_queue.length === 0 && !error_reported) { | ||
error_reported = true; | ||
throwSequenceErrors(); | ||
} | ||
while (or_queue.length) { | ||
error_reported = true; | ||
fn = or_queue.shift(); | ||
@@ -69,3 +77,3 @@ try { | ||
if (or_queue.length === 0) { | ||
console.error.apply(console,sequence_errors); | ||
throwSequenceErrors(); | ||
} | ||
@@ -419,3 +427,4 @@ } | ||
trigger = done; | ||
}); | ||
}) | ||
.or(ignored); | ||
@@ -427,3 +436,3 @@ // temporary version of `trigger`, which if called | ||
trigger = function __trigger__() { | ||
fn = createSequence.apply(ø,arguments); | ||
fn = createSequence.apply(ø,arguments).or(ignored); | ||
}; | ||
@@ -434,3 +443,4 @@ trigger.fail = function __trigger_fail__() { | ||
done.fail.apply(ø,args); | ||
}); | ||
}) | ||
.or(ignored); | ||
}; | ||
@@ -515,3 +525,3 @@ } | ||
else { | ||
trigger = createSequence.apply(ø,arguments); | ||
trigger = createSequence.apply(ø,arguments).or(ignored); | ||
} | ||
@@ -529,3 +539,4 @@ return public_api.messages.apply(ø,arguments); | ||
done.fail.apply(ø,args); | ||
}); | ||
}) | ||
.or(ignored); | ||
} | ||
@@ -544,3 +555,4 @@ }); | ||
} | ||
}); | ||
}) | ||
.or(ignored); | ||
} | ||
@@ -614,2 +626,3 @@ | ||
var seq_error = false, | ||
error_reported = false, | ||
seq_aborted = false, | ||
@@ -616,0 +629,0 @@ then_ready = true, |
{ | ||
"name": "asynquence", | ||
"version": "0.3.5-d", | ||
"version": "0.3.6-a", | ||
"description": "asynquence: async sequences & gates for flow-control", | ||
@@ -5,0 +5,0 @@ "main": "./asq.js", |
# asynquence | ||
A lightweight (**~2.0k** minzipped) micro-lib for asynchronous flow-control using sequences and gates. | ||
A tiny (**~2.0k** minzipped) lib for promise-style async sequence flow-control. | ||
## Explanation | ||
*asynquence* is a thin wrapper/abstraction on top of promises that makes using them for async flow control easier and more powerful. | ||
### TL;DR: By Example | ||
@@ -17,2 +19,3 @@ | ||
### Sequences | ||
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**. | ||
@@ -33,2 +36,3 @@ | ||
### Gates | ||
If you have two or more tasks to perform at the same time, but want to wait for them all to complete before moving on, you need a **gate**. | ||
@@ -41,2 +45,3 @@ | ||
### Conveniences | ||
There are a few convenience methods on the API, as well: | ||
@@ -105,2 +110,3 @@ | ||
### Plugin Extensions | ||
`ASQ.extend( {name}, {build} )` allows you to specify an API extension, giving it a `name` and a `build` function callback that should return the implementation of your API extension. The `build` callback is provided two parameters, the sequence `api` instance, and an `internals(..)` method, which lets you get or set values of various internal properties (generally, don't use this if you can avoid it). | ||
@@ -137,2 +143,3 @@ | ||
#### Iterable Sequences | ||
One of the contrib plugins provided is `iterable-sequence`. Unlike other plugins, which add methods onto the sequence instance API, this plugin adds a new method directly onto the main module API: `ASQ.iterable(..)`. Calling `ASQ.iterable(..)` creates a special iterable sequence, as compared to calling `ASQ(..)` to create a normal *asynquence* sequence. | ||
@@ -187,5 +194,7 @@ | ||
### Multiple parameters | ||
API methods take one or more functions as their parameters. `gate(..)` treats multiple functions as segments in the same gate. The other API methods (`then(..)`, `or(..)`, `pipe(..)`, `seq(..)`, and `val(..)`) treat multiple parameters as just separate subsequent steps in the respective sequence. These methods don't accept arrays of functions (that you might build up programatically), but since they take multiple parameters, you can use `.apply(..)` to spread those out. | ||
### Promises/A+ Compliance | ||
**The goal of *asynquence* is that you should be able to use it as your primary async flow-control library, without the need for other Promises implementations.** | ||
@@ -192,0 +201,0 @@ |
@@ -643,3 +643,5 @@ (function(name,context,dependency,definition){ | ||
// NOTE: calling doSeq2() to pass in ASQ instance itself | ||
.seq(doSeq2(msg2)); | ||
.seq(function(){ | ||
return doSeq2(msg2); | ||
}); | ||
@@ -655,3 +657,5 @@ return seq; | ||
// NOTE: calling doSeq3() to pass in ASQ instance itself | ||
.seq(doSeq3(msg + "!")); | ||
.seq(function(){ | ||
return doSeq3(msg + "!"); | ||
}); | ||
@@ -658,0 +662,0 @@ return seq; |
82351
2114
436