-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.sequence.min.js
More file actions
6 lines (6 loc) · 9.42 KB
/
Copy pathjquery.sequence.min.js
File metadata and controls
6 lines (6 loc) · 9.42 KB
1
2
3
4
5
6
/*******************************************
* jQuery-Sequence - v0.2.1 - 2012-07-03
* https://github.com/katowulf/jquery-sequence
* Copyright (c) 2012 Michael "Kato" Wulf; Licensed MIT, GPL
*******************************************/
(function($){"use strict";var j;var l=$.Sequence=function(){this.master=$.Deferred();this.returnVals=[];this.fxns={};this.last=$.Deferred().resolve(this);this.shared={pauseEvent:false,abortEvent:false,lastIf:j};var d=this.shared;this.fxns.wait=_wrapFx(d,function(a,b){var c=$.Deferred();_waitFor(a).then(function(){_resolve(c,d,[b])});return c},{prevPos:1})};l.CB=new Object();l.PREV=new Object();l.ERR=new Object();l.start=function(a,b){var c=new l(),parms=_startParms($.makeArray(arguments));a&&_registerAll(c,parms.fxns);if(parms.timeout){var d=setTimeout(function(){c.abort('timeout exceeded');d=null},parms.timeout);c.master.always(function(){if(d){clearTimeout(d);d=null}})}return c};l.prototype.wait=function(b){var c=this.last,def=this.last=$.Deferred(),shared=this.shared;c.then(function(){var a=$.makeArray(arguments);_waitFor(b).then(function(){_resolve(def,shared,a)})},function(){def.reject.apply(def,$.makeArray(arguments))});return this};l.prototype.handle=function(a,b,c){var d=_parms(arguments);this.last=_ex(this.returnVals,this.master,this.last,_wrapFx(this.shared,d.fx,d.opts,true),d.scope,d.args,this.shared);return this};l.prototype.run=function(a,b){var c=$.makeArray(arguments);a=(typeof(c[0])==='object')?c.shift():null;b=c.shift();if(!(b in this.fxns)){throw new Error('invalid function name "'+b+'" (did you forget to call register?)');}this.last=_ex(this.returnVals,this.master,this.last,this.fxns[b],a,c,this.shared);return this};$.Sequence.prototype._=$.Sequence.prototype.run;l.prototype.register=function(a,b,c){c=$.extend({},c);if((a in this)){throw new Error(a+' already exists in Sequence, it cannot be overwritten by register() :(');}this[a]=function(){return this.run.apply(this,[a].concat($.makeArray(arguments)))};this.fxns[a]=_wrapFx(this.shared,b,c,('cbPos'in c));return this};l.prototype.wrap=function(a,b,c){var d=_parms(arguments);this.last=_ex(this.returnVals,this.master,this.last,_wrapFx(this.shared,d.fx,d.opts,false),d.scope,d.args,this.shared);return this};l.prototype.then=function(a,b){this.last.then(_catch(a),_catch(b));return this};l.prototype.parallel=function(b){var c=this;return this.wrap(function(a){var i=b.length,sharedContext={vals:[],done:0,max:b.length,masterDef:$.Deferred()};while(i--){_parallel(_altParms(c.shared,c.fxns,b[i]),i,a,sharedContext)}return sharedContext.masterDef.promise()},l.PREV)};l.prototype.if=function(c,d,e){var f=$.makeArray(arguments),fxns=this.fxns,shared=this.shared;c=f.shift();return this.wrap(function(a){shared.lastIf=c(a,shared.lastIf);if(shared.lastIf){var b=_altParms(shared,fxns,f),def=$.Deferred();b.fx(def,b.scope,b.args,a);return def.promise()}else{return a}},l.PREV)};l.prototype.end=function(a){var b=this.returnVals,master=this.master,shared=this.shared;this.last.then(function(){_resolve(master,shared,[b])},function(){if(a){throw _result(arguments);}else{master.reject.call(master,_result(arguments))}});return master.promise()};l.prototype.pause=function(){this.shared.pauseEvent=$.Deferred();return this};l.prototype.unpause=function(){var e=this.shared.pauseEvent;e&&e.resolve();return this};l.prototype.abort=function(a){var e=this.shared.abortEvent=new Error(a||'Sequence aborted');e.sequenceAborted=true;if(this.shared.pauseEvent){this.shared.pauseEvent.reject(e)}this.last.reject(e);return this};function _parms(a){var b=$.makeArray(a),out={opts:{},scope:null},pos=0;while(b.length&&pos++<3){if($.isFunction(b[0])){out.fx=b.shift();break}else if($.isPlainObject(b[0])){out.opts=$.extend({},b.shift())}else if(typeof(b[0])==='object'){out.scope=b.shift()}else if(b[0]!==null){throw new Error('Invalid argument '+b[0]+' at pos '+pos);}}if(!('fx'in out)){throw new Error('Function to call was not included in arguments');}out.args=b;return out}function _altParms(a,b,c){var i=-1,len=c.length,k,parms,isWrapped,wait=0;outer:while(++i<len&&i<3){switch(typeof(c[i])){case'string':k=c[i];if(!(k in b)){throw new Error('Invalid function name '+k);}c[i]=b[k];isWrapped=true;break outer;case'function':isWrapped=false;break outer;default:}}parms=_parms(c);parms.wait=wait;if(!isWrapped){parms.fx=_wrapFx(a,parms.fx,parms.opts,_usesCallback(parms))}return parms}function _ex(a,b,c,d,e,f,g){var h=$.Deferred(),pauseEvent=g.pauseEvent||$.Deferred().resolve();if(b.isResolved()||b.isRejected()){return b.promise()}if(g.abortEvent){h.reject(g.abortEvent)}else{pauseEvent.then(function(){if(c.isRejected()){h=c}else{c.then(function(){d(h,e,f,_result(arguments))},function(){h.reject.apply(h,arguments)})}});h.always(function(){if(!c.isRejected()){a.push(_result(arguments))}})}return h}function _wrapFx(f,g,h,i){return function(a,b,c,d){try{var v=g.apply(b,_fxArgs(c,_ctx(f,h,a,i,d)));if(!i){if(_isDeferred(v)){v.then(function(){_resolve(a,f,$.makeArray(arguments))},function(){a.reject.apply(a,arguments)})}else if(v instanceof Error){a.reject(v)}else{_resolve(a,f,[v])}}}catch(e){a.reject(e)}}}function _resolve(a,b,c){if(b.abortEvent){a.reject(b.abortEvent)}else if(b.pauseEvent){b.pauseEvent.then(function(){a.resolve.apply(a,c)},function(e){a.reject(e)})}else{a.resolve.apply(a,c)}}function _fxArgs(a,b){var i,d,out=$.makeArray(a);m.fill(out,b);if('defaults'in b.opts){d=b.opts.defaults;i=d.length;while(i--){if(!_exists(out[i])){out[i]=d[i]}else if(typeof(d[i])==='object'&&typeof(out[i])==='object'){out[i]=$.extend(true,{},d[i],out[i])}}}return out}function _cb(a,b){return function(v){if(v instanceof Error){a.reject.apply(a,arguments)}else{_resolve(a,b,$.makeArray(arguments))}}}function _errCb(a){return function(){a.reject.apply(a,arguments)}}function _ctx(a,b,c,d,e){return{shared:a,opts:$.extend({},b),prev:e,def:c,placeholders:d?['cb','err','prev']:['prev']}}function _catch(b){if(!b){return j}return function(){try{b.apply(null,$.makeArray(arguments))}catch(e){var a=(typeof window.console=='object')?window.console:{error:function(){}};a.error(e)}}}function _result(a){if(a.length>1){return $.makeArray(a)}return a[0]}function _exists(a){return a!==j&&a!==null&&a!==''}function _isDeferred(o){return typeof(o)==='object'&&('then'in o)&&('always'in o)&&('fail'in o)}function _registerAll(a,b){var k,o,f;if(b&&$.isPlainObject(b)){for(k in b){if(b.hasOwnProperty(k)){f=b[k];switch(typeof(f)){case'function':o=null;break;case'object':o=$.extend({},f);f=o.fx;delete o.fx;break;default:throw new Error('each element of hash passed into start() must be an object or function');}a.register(k,f,o)}}}}function _parallel(a,b,c,d){var f=$.Deferred();if(a.opts.wait>0){_waitFor(a.opts.wait).then(function(){a.fx(f,a.scope,a.args,c)})}else{a.fx(f,a.scope,a.args,c)}f.done(function(){d.vals[b]=_result(arguments);if(++d.done==d.max){d.masterDef.resolve(d.vals)}}).fail(function(e){d.masterDef.reject(e)});return f.promise()}function _usesCallback(a){if('cbPos'in a.opts){return true}var b=a.args,i=b.length;while(i--){if(b[i]===l.CB){return true}}return false}var m={};(function(d){m.fill=function(a,b){var p,k,len,keys=b.placeholders,i=keys.length,fromOpts=[],fromPlaceholders=[];while(i--){k=keys[i];p=_positionPlaceholder(a,b.opts,k);if(p.pos>-1){p.val=_valFor(k,b);(p.fromPlaceholder&&fromPlaceholders.push(p))||fromOpts.push(p)}}fromPlaceholders.sort(_sortPlaceholders);fromOpts.sort(_sortPlaceholders);len=fromPlaceholders.length;for(i=0;i<len;i++){p=fromPlaceholders[i];a[p.pos]=p.val}len=fromOpts.length;for(i=0;i<len;i++){_addToArgs(a,fromOpts[i])}};function _positionPlaceholder(a,b,c){var i,ph=_phFor(c),defaultPos=_defaultPosFor(c),optsPos=c+'Pos',optsKey=c+'Key',out={prefix:c,pos:-1,key:d,fromPlaceholder:false},hasPos=(optsPos in b&&b[optsPos]>=0),hasKey=(optsKey in b&&typeof(b[optsKey])in{number:1,string:1});if(hasPos){out.pos=b[optsPos];if(hasKey){out.key=b[optsKey]}}else{i=a.length;while(i--){if(a[i]===ph){out.pos=i;out.fromPlaceholder=true;break}}if(i<0&&typeof(defaultPos)=='number'){out.pos=defaultPos}}return out}function _sortPlaceholders(a,b){return a.pos-b.pos}function _containerFor(k){return typeof(k)==='number'?[]:{}}function _addToArgs(a,p){var b=(p.key!==d),ins=(a.length<p.pos+1);if(b){if(ins){a[p.pos]=_containerFor(p.key)}a[p.pos][p.key]=p.val}else{if(ins){a[p.pos]=p.val}else{a.splice(p.pos,0,p.val)}}}function _phFor(k){switch(k){case'cb':return l.CB;case'err':return l.ERR;case'prev':return l.PREV;default:throw new Error('Invalid placeholder type (must be one of cb/err/prev): '+k);}}function _defaultPosFor(k){switch(k){case'cb':return 0;case'err':return false;case'prev':return false;default:throw new Error('Invalid placeholder type (must be one of cb/err/prev): '+k);}}function _valFor(k,a){switch(k){case'cb':return _cb(a.def,a.shared);case'err':return _errCb(a.def);case'prev':return a.prev;default:throw new Error('Invalid placeholder type (must be one of cb/err/prev): '+k);}}})();function _waitFor(c){var d=new Date().valueOf(),def=$.Deferred();setTimeout(function(){var a=new Date().valueOf()-d;if(a<c){var b=setInterval(function(){if(new Date().valueOf()-d>=c){clearInterval(b);def.resolve(true)}},Math.max(a,10))}else{def.resolve(true)}},c);return def.promise()}function _remove(a,b,c){var d=a.slice((c||b)+1||a.length);a.length=b<0?a.length+b:b;return a.push.apply(a,d)}function _startParms(a){var b={fxns:false,timeout:false},i=a.length;while(i--){switch(typeof(a[i])){case'number':b.timeout=a[i];break;case'object':b.fxns=a[i];break;default:throw new Error('Invalid argument to $.Sequence.start() of type '+typeof(a[i]));}}return b}})(jQuery);