forked from dfilatov/vow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vow.min.js
10 lines (10 loc) · 5.82 KB
/
vow.min.js
1
2
3
4
5
6
7
8
9
10
/**
* Vow
*
* Copyright (c) 2012-2013 Filatov Dmitry (dfilatov@yandex-team.ru)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @version 0.3.2
*/(function(e){var t=function(e){this._res=e,this._isFulfilled=!!arguments.length,this._isRejected=!1,this._fulfilledCallbacks=[],this._rejectedCallbacks=[],this._progressCallbacks=[]};t.prototype={valueOf:function(){return this._res},isFulfilled:function(){return this._isFulfilled},isRejected:function(){return this._isRejected},isResolved:function(){return this._isFulfilled||this._isRejected},fulfill:function(e){if(this.isResolved())return;this._isFulfilled=!0,this._res=e,this._callCallbacks(this._fulfilledCallbacks,e),this._fulfilledCallbacks=this._rejectedCallbacks=this._progressCallbacks=r},reject:function(e){if(this.isResolved())return;this._isRejected=!0,this._res=e,this._callCallbacks(this._rejectedCallbacks,e),this._fulfilledCallbacks=this._rejectedCallbacks=this._progressCallbacks=r},notify:function(e){if(this.isResolved())return;this._callCallbacks(this._progressCallbacks,e)},then:function(e,n,i,s){n&&!o(n)?(s=n,n=r):i&&!o(i)&&(s=i,i=r);var u=new t,a;return this._isRejected||(a={promise:u,fn:o(e)?e:r,ctx:s},this._isFulfilled?this._callCallbacks([a],this._res):this._fulfilledCallbacks.push(a)),this._isFulfilled||(a={promise:u,fn:n,ctx:s},this._isRejected?this._callCallbacks([a],this._res):this._rejectedCallbacks.push(a)),this.isResolved()||this._progressCallbacks.push({promise:u,fn:i,ctx:s}),u},fail:function(e,t){return this.then(r,e,t)},always:function(e,t){var n=this,r=function(){return e(n)};return this.then(r,r,t)},progress:function(e,t){return this.then(r,r,e,t)},spread:function(e,t,n){return this.then(function(t){return e.apply(this,t)},t,n)},done:function(){this.fail(s)},delay:function(e){return this.then(function(n){var r=new t;return setTimeout(function(){r.fulfill(n)},e),r})},timeout:function(e){var n=new t,r=setTimeout(function(){n.reject(Error("timed out"))},e);return n.sync(this),n.always(function(){clearTimeout(r)}),n},sync:function(e){var t=this;e.then(function(e){t.fulfill(e)},function(e){t.reject(e)})},_callCallbacks:function(e,t){var r=e.length;if(!r)return;var s=this.isResolved(),o=this.isFulfilled();i(function(){var i=0,u,a,f;while(i<r){u=e[i++],a=u.promise,f=u.fn;if(f){var l=u.ctx,c;try{c=l?f.call(l,t):f(t)}catch(h){a.reject(h);continue}s?n.isPromise(c)?function(e){c.then(function(t){e.fulfill(t)},function(t){e.reject(t)})}(a):a.fulfill(c):a.notify(c)}else s?o?a.fulfill(t):a.reject(t):a.notify(t)}})}};var n={promise:function(e){return arguments.length?this.isPromise(e)?e:new t(e):new t},when:function(e,t,n,r,i){return this.promise(e).then(t,n,r,i)},fail:function(e,t,n){return this.when(e,r,t,n)},always:function(e,t,n){return this.promise(e).always(t,n)},progress:function(e,t,n){return this.promise(e).progress(t,n)},spread:function(e,t,n,r){return this.promise(e).spread(t,n,r)},done:function(e){this.isPromise(e)&&e.done()},isPromise:function(e){return e&&o(e.then)},valueOf:function(e){return this.isPromise(e)?e.valueOf():e},isFulfilled:function(e){return this.isPromise(e)?e.isFulfilled():!0},isRejected:function(e){return this.isPromise(e)?e.isRejected():!1},isResolved:function(e){return this.isPromise(e)?e.isResolved():!0},fulfill:function(e){return this.when(e,r,function(e){return e})},reject:function(e){return this.when(e,function(e){var n=new t;return n.reject(e),n})},resolve:function(e){return this.isPromise(e)?e:this.when(e)},invoke:function(e){try{return this.promise(e.apply(null,u.call(arguments,1)))}catch(t){return this.reject(t)}},forEach:function(e,t,n,r){var i=r?r.length:e.length,s=0;while(s<i)this.when(e[r?r[s]:s],t,n),++s},all:function(e){var r=new t,i=f(e),s=i?l(e):c(e),o=s.length,u=i?[]:{};if(!o)return r.fulfill(u),r;var a=o,h=function(){if(!--a){var t=0;while(t<o)u[s[t]]=n.valueOf(e[s[t++]]);r.fulfill(u)}},p=function(e){r.reject(e)};return this.forEach(e,h,p,s),r},allResolved:function(e){var n=new t,r=f(e),i=r?l(e):c(e),s=i.length,o=r?[]:{};if(!s)return n.fulfill(o),n;var u=function(){--s||n.fulfill(e)};return this.forEach(e,u,u,i),n},any:function(e){var n=new t,r=e.length;if(!r)return n.reject(Error()),n;var i=0,s,o=function(e){n.fulfill(e)},u=function(e){i||(s=e),++i===r&&n.reject(s)};return this.forEach(e,o,u),n},delay:function(e,t){return this.promise(e).delay(t)},timeout:function(e,t){return this.promise(e).timeout(t)}},r,i=function(){if(typeof process=="object")return process.nextTick;if(e.setImmediate)return e.setImmediate;var t=[],n=function(){var e=t,n=0,r=t.length;t=[];while(n<r)e[n++]()};if(e.postMessage){var r=!0;if(e.attachEvent){var i=function(){r=!1};e.attachEvent("onmessage",i),e.postMessage("__checkAsync","*"),e.detachEvent("onmessage",i)}if(r){var s="__promise"+ +(new Date),o=function(e){e.data===s&&(e.stopPropagation&&e.stopPropagation(),n())};return e.addEventListener?e.addEventListener("message",o,!0):e.attachEvent("onmessage",o),function(n){t.push(n)===1&&e.postMessage(s,"*")}}}var u=e.document;if("onreadystatechange"in u.createElement("script")){var a=function(){var e=u.createElement("script");e.onreadystatechange=function(){e.parentNode.removeChild(e),e=e.onreadystatechange=null,n()},(u.documentElement||u.body).appendChild(e)};return function(e){t.push(e)===1&&a()}}return function(e){setTimeout(e,0)}}(),s=function(e){i(function(){throw e})},o=function(e){return typeof e=="function"},u=Array.prototype.slice,a=Object.prototype.toString,f=Array.isArray||function(e){return a.call(e)==="[object Array]"},l=function(e){var t=[],n=0,r=e.length;while(n<r)t.push(n++);return t},c=Object.keys||function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push(n);return t};typeof exports=="object"?module.exports=n:typeof modules=="object"?modules.define("vow",function(e){e(n)}):typeof define=="function"?define(function(e,t,r){r.exports=n}):e.Vow=n})(this);