asynquence
Advanced tools
Comparing version 0.3.2-b to 0.3.3-a
/*! asynquence | ||
v0.3.2-b (c) Kyle Simpson | ||
v0.3.3-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.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}); | ||
!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(O),O=null,Q.length=0,w.length=0,C.length=0,M.length=0}function e(){return T?l():(O||(O=t(l)),void 0)}function l(){var t,r;if(O=null,T)n();else if(S)for(;w.length;){t=w.shift();try{t.apply(h,M)}catch(u){a(u)?M=M.concat(u):(M.push(u),u.stack&&M.push(u.stack)),0===w.length&&console.error.apply(console,M)}}else if(I&&Q.length>0){I=!1,t=Q.shift(),r=C.slice(),C.length=0,r.unshift(p());try{t.apply(h,r)}catch(u){a(u)?M=M.concat(u):M.push(u),S=!0,e()}}}function p(){function n(){S||T||I||(I=!0,C.push.apply(C,arguments),M.length=0,e())}return n.fail=function(){S||T||I||(S=!0,C.length=0,M.push.apply(M,arguments),e())},n.abort=function(){S||T||(I=!1,T=!0,C.length=0,M.length=0,e())},n.errfcb=function(e){e?n.fail(e):n.apply(h,g.call(arguments,1))},n}function m(n,e,r){function u(){clearTimeout(y),y=_=q=m=null}function l(){return d?c():(y||(y=t(c)),void 0)}function c(){if(!(S||T||b)){var e=[];y=null,v?(n.fail.apply(h,m),u()):d?(n.abort(),u()):i()&&(b=!0,_.forEach(function(n,t){e.push(q["s"+t])}),n.apply(h,e),u())}}function i(){if(!(S||T||v||d||b||0===_.length)){var n=!0;return _.some(function(e){return null===e?(n=!1,!0):void 0}),n}}function f(){function n(){if(!(S||T||v||d||b||_[e])){var n=o.messages.apply(h,arguments);q["s"+e]=n.length>1?n:n[0],_[e]=!0,l()}}var e=_.length;return n.fail=function(){S||T||v||d||b||_[e]||(v=!0,m=g.call(arguments),l())},n.abort=function(){S||T||v||d||b||(d=!0,c())},n.errfcb=function(e){e?n.fail(e):n.apply(h,g.call(arguments,1))},_[e]=null,n}var s,p,m,y,v=!1,d=!1,b=!1,_=[],q={};e.some(function(n){if(v||d)return!0;s=r.slice(),s.unshift(f());try{n.apply(h,s)}catch(e){return p=e,v=!0,!0}}),p&&(a(p)?n.fail.apply(h,p):n.fail(p))}function y(){return S||T||0===arguments.length?P:(Q.push.apply(Q,f(arguments,i)),e(),P)}function v(){return T||0===arguments.length?P:(w.push.apply(w,arguments),e(),P)}function d(){if(S||T||0===arguments.length)return P;var n=g.call(arguments);return y(function(e){var t=g.call(arguments,1);m(e,n,t)}),P}function b(){return T||0===arguments.length?P:(g.call(arguments).forEach(function(n){y(function(e){n.apply(h,g.call(arguments,1)),e()}).or(n.fail)}),P)}function _(){return S||T||0===arguments.length?P:(g.call(arguments).forEach(function(n){var e;a(n)&&"next"in n&&(n.then(function(){e.apply(h,arguments)}).or(function(){e.fail.apply(h,arguments)}),n=r(function(n){e=n}),e=function(){n=r.apply(h,arguments)},e.fail=function(){var e=g.call(arguments);n=r(function(n){n.fail.apply(h,e)})}),y(function(e){a(n)||(n=n.apply(h,g.call(arguments,1))),n.pipe(e)})}),P)}function q(){return S||T||0===arguments.length?P:(g.call(f(arguments,c)).forEach(function(n){y(function(e){var t=n.apply(h,g.call(arguments,1));a(t)||(t=o.messages(t)),e.apply(h,t)})}),P)}function k(){return S||T||0===arguments.length?P:(g.call(arguments).forEach(function(n){y(function(e){"function"!=typeof n||"then"in n||(n=n.apply(h,g.call(arguments,1))),n.then(e,e.fail)})}),P)}function E(){var n;return q(function(){return n?n.apply(h,arguments):n=r.apply(h,arguments),o.messages.apply(h,arguments)}),v(function(){if(n)n.fail.apply(h,arguments);else{var e=g.call(arguments);n=r().then(function(n){n.fail.apply(h,e)})}}),r().then(function(e){n?n.pipe(e):n=e})}function x(){return S?P:(T=!0,l(),P)}function A(n,e){var t=arguments.length>1;switch(n){case"seq_error":if(!t)return S;S=e;break;case"seq_aborted":if(!t)return T;T=e;break;case"then_ready":if(!t)return I;I=e;break;case"then_queue":return Q;case"or_queue":return w;case"sequence_messages":return C;case"sequence_errors":return M}}function j(){Object.keys(s).forEach(function(n){P[n]=s[n](P,A)})}var O,S=!1,T=!1,I=!0,Q=[],w=[],C=[],M=[],P=u({then:y,or:v,gate:d,pipe:b,seq:_,val:q,promise:k,fork:E,abort:x});return j(),P.then.apply(h,f(arguments,i)),P}function u(n){return Object.defineProperty(n,m,{enumerable:!1,value:!0}),n}function a(n){return null!=n&&"object"==typeof n&&n[m]}function l(n,e){return g.call(e).slice(1,n+1)}function c(n){return o.messages.apply(h,l(n,arguments))}function i(n){arguments[n+1].apply(h,l(n,arguments))}function f(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 o,s={},p=(e||{})[n],g=Array.prototype.slice,m="__ASQ__",h=Object.create(null);return o=r,o.extend=function(n,e){return~["then","or","gate","pipe","seq","val","abort"].indexOf(n)||(s[n]=e),o},o.messages=function(){var n=g.call(arguments);return u(n),n},o.isMessageWrapper=o.isSequence=a,o.noConflict=function(){return e&&(e[n]=p),o},o}); |
/*! asynquence | ||
v0.3.2-b (c) Kyle Simpson | ||
v0.3.3-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
@@ -493,2 +493,41 @@ */ | ||
function fork() { | ||
var trigger; | ||
// listen for success at this point in the sequence | ||
val(function __val__(){ | ||
if (trigger) { | ||
trigger.apply(ø,arguments); | ||
} | ||
else { | ||
trigger = createSequence.apply(ø,arguments); | ||
} | ||
return public_api.messages.apply(ø,arguments); | ||
}); | ||
// listen for error at this point in the sequence | ||
or(function __or__(){ | ||
if (trigger) { | ||
trigger.fail.apply(ø,arguments); | ||
} | ||
else { | ||
var args = ARRAY_SLICE.call(arguments); | ||
trigger = createSequence().then(function __then__(done){ | ||
done.fail.apply(ø,args); | ||
}); | ||
} | ||
}); | ||
// create the forked sequence which will receive | ||
// the success/error stream from the main sequence | ||
return createSequence() | ||
.then(function __then__(done){ | ||
if (!trigger) { | ||
trigger = done; | ||
} | ||
else { | ||
trigger.pipe(done); | ||
} | ||
}); | ||
} | ||
function abort() { | ||
@@ -561,2 +600,3 @@ if (seq_error) { | ||
promise: promise, | ||
fork: fork, | ||
abort: abort | ||
@@ -563,0 +603,0 @@ }) |
{ | ||
"name": "asynquence", | ||
"version": "0.3.2-b", | ||
"version": "0.3.3-a", | ||
"description": "asynquence: async sequences & gates for flow-control", | ||
@@ -5,0 +5,0 @@ "main": "./asq.js", |
# asynquence | ||
A lightweight (**~1.7k** minzipped) micro-lib for asynchronous flow-control using sequences and gates. | ||
A lightweight (**~1.8k** minzipped) micro-lib for asynchronous flow-control using sequences and gates. | ||
@@ -65,2 +65,10 @@ ## Explanation | ||
* `fork()` creates a new sequence that forks off of the main sequence. Success or Error message(s) stream along to the forked sequence as expected, but the main sequence continues as its own sequence beyond the fork point, and neither sequence will have any further effect on the other. | ||
This API method is primarily useful to create multiple "listeners" at the same point of a sequence. For example: `sq = ASQ()...; sq2 = sq.fork().then(..); sq3 = sq.fork().then(..); sq.then(..)`. In that snippet, there'd be 3 `then(..)` listeners that would be equally and simultaneously triggered when the main `sq` sequence reached that point. | ||
**Note:** Unlike most other API methods, `fork()` returns a new sequence instance, so chaining after `fork()` would not be chaining off of the main sequence but off of the forked sequence. | ||
`sq.fork()` is (sort-of) sugar short-hand for `ASQ().seq(sq)`. | ||
* `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}.errfcb` provides a properly formatted callback for the occasion. | ||
@@ -67,0 +75,0 @@ |
120
tests.js
@@ -1033,4 +1033,122 @@ (function(name,context,dependency,definition){ | ||
tests.push(function(testDone){ | ||
var label = "Core Test #22", timeout; | ||
var label = "Core Test #22", timeout, sq, sq2, sq3, sq4, sq5; | ||
sq = ASQ(function(done){ | ||
setTimeout(function(){ | ||
done("Hello"); | ||
},100); | ||
}); | ||
// first fork-listener | ||
sq2 = sq.fork().val(function(msg){ | ||
if (!( | ||
arguments.length === 1 && | ||
msg === "Hello" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}); | ||
// second fork-listener | ||
sq3 = sq.fork().val(function(msg){ | ||
if (!( | ||
arguments.length === 1 && | ||
msg === "Hello" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}); | ||
// main sequence-listener | ||
sq.val(function(msg){ | ||
if (!( | ||
arguments.length === 1 && | ||
msg === "Hello" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}); | ||
// test sending an error into the forks | ||
sq.then(function(done){ | ||
setTimeout(function(){ | ||
done.fail("World"); | ||
},100); | ||
}); | ||
// second fork-listener | ||
sq4 = sq.fork() | ||
.val(function(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
}) | ||
.or(function(msg){ | ||
if (!( | ||
arguments.length === 1 && | ||
msg === "World" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}); | ||
// third fork-listener | ||
sq5 = sq.fork() | ||
.val(function(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
}) | ||
.or(function(msg){ | ||
if (!( | ||
arguments.length === 1 && | ||
msg === "World" | ||
)) { | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
}); | ||
// main sequence listener | ||
sq.val(function(){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
}) | ||
.or(function(msg){ | ||
var args = ARRAY_SLICE.call(arguments); | ||
// defer this error handling while the other | ||
// forks are error-notified | ||
setTimeout(function(){ | ||
clearTimeout(timeout); | ||
if (args.length === 1 && | ||
msg === "World" | ||
) { | ||
PASS(testDone,label); | ||
} | ||
else { | ||
args.unshift(testDone,label); | ||
FAIL.apply(FAIL,args); | ||
} | ||
},0); | ||
}); | ||
timeout = setTimeout(function(){ | ||
FAIL(testDone,label + " (from timeout)"); | ||
},1000); | ||
}); | ||
tests.push(function(testDone){ | ||
var label = "Core Test #23", timeout; | ||
// testing a custom plugin which will pass along | ||
@@ -1037,0 +1155,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
71416
1835
403