forked from kriskowal/q
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq.min.js
14 lines (14 loc) · 6.56 KB
/
q.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(function(n){"function"===typeof define?define(n):"object"===typeof exports?n(require,exports):n(void 0,Q={})})(function(n,c){function s(a){return a}function u(a,b,e){a[b]||(a[b]=e);return a[b]}function o(a){return void 0===a||null===a?a:a.valueOf()}function j(){function a(a){if(b)return e=i(a),v.call(b,function(a,b){l(function(){e.promiseSend.apply(e,b)})},void 0),b=void 0,e}var b=[],e,k=p(j.prototype),c=p(f.prototype);c.promiseSend=function(){var a=g.call(arguments);b?b.push(a):l(function(){e.promiseSend.apply(e,
a)})};c.valueOf=function(){return b?c:e.valueOf()};k.promise=x(c);k.resolve=a;k.reject=function(b){return a(h(b))};return k}function f(a,b,e){void 0===b&&(b=function(a){return h("Promise does not support operation: "+a)});var k=p(f.prototype);k.promiseSend=function(e,c){var d=g.call(arguments,2),w;try{w=a[e]?a[e].apply(k,d):b.apply(k,[e].concat(d))}catch(f){w=h(f)}return(c||s)(w)};e&&(k.valueOf=e);return x(k)}function t(a){return a&&"function"===typeof a.promiseSend}function B(a){a=o(a);return void 0===
a||null===a?!1:!!a.promiseRejected}function h(a){var b=f({when:function(b){if(b){var c=y.indexOf(this);-1!==c&&(z.splice(c,1),y.splice(c,1))}return b?b(a):h(a)}},function(){return h(a)},function(){var b=p(h.prototype);b.promiseRejected=!0;b.reason=a;return b});y.push(b);z.push(a);return b}function i(a){if(t(a))return a;if(a&&"function"===typeof a.then){var b=j();a.then(b.resolve,b.reject);return b.promise}return f({when:function(){return a},get:function(b){return a[b]},put:function(b,c){return a[b]=
c},del:function(b){return delete a[b]},post:function(b,c){return a[b].apply(a,c)},apply:function(b,c){return a.apply(b,c)},viewInfo:function(){function b(a){d[a]||(d[a]=typeof c[a])}for(var c=a,d={};c;)Object.getOwnPropertyNames(c).forEach(b),c=Object.getPrototypeOf(c);return{type:typeof a,properties:d}},keys:function(){return J(a)}},void 0,function(){return a})}function C(a,b){a=i(a);return b?f({viewInfo:function(){return b}},function(b){var c=g.call(arguments);return q.apply(void 0,[a].concat(c))},
function(){return o(a)}):q(a,"viewInfo")}function d(a,b,e){function c(a){try{return b?b(a):a}catch(e){return h(e)}}function d(a){try{return e?e(a):h(a)}catch(b){return h(b)}}var f=j(),g=!1;l(function(){i(a).promiseSend("when",function(a){g||(g=!0,f.resolve(i(a).promiseSend("when",c,d)))},function(a){g||(g=!0,f.resolve(d(a)))})});return f.promise}function m(a){return function(b){var e=g.call(arguments,1);return q.apply(void 0,[b,a].concat(e))}}function q(a,b){var e=j(),c=g.call(arguments,2),a=i(a);
l(function(){a.promiseSend.apply(a,[b,e.resolve].concat(c))});return e.promise}function D(a){return d(a,function(a){var e=a.length;if(0===e)return i(a);var c=j();v.call(a,function(f,g,h){d(g,function(d){a[h]=d;0===--e&&c.resolve(a)}).fail(c.reject)},void 0);return c.promise})}function E(a){if(1<arguments.length)var b=Array.prototype.slice.call(arguments,1),a=a.bind.apply(a,b);return function(){var b=j(),c=g.call(arguments);c.push(b.node());r(a,this,c).fail(b.reject);return b.promise}}var l;try{l=
n("event-queue").enqueue}catch(K){if("undefined"!==typeof MessageChannel){var F=new MessageChannel,A={},G=A;F.port1.onmessage=function(){var a=A.next,b=a.task;A=a;b()};l=function(a){G=G.next={task:a};F.port2.postMessage(0)}}else l=function(a){setTimeout(a,0)}}var x=u(Object,"freeze",s),p=u(Object,"create",function(a){function b(){}b.prototype=a;return new b}),J=u(Object,"keys",function(a){var b=[],c;for(c in a)b.push(c);return b}),v=Array.prototype.reduce||function(a,b){var c=0,d=this.length;if(1===
arguments.length){do{if(c in this){b=this[c++];break}if(++c>=d)throw new TypeError;}while(1)}for(;c<d;c++)c in this&&(b=a(b,this[c],c));return b},g=Array.prototype.slice;c.nextTick=l;c.defer=j;j.prototype.node=function(){var a=this;return function(b,c){b?a.reject(b):2<arguments.length?a.resolve(Array.prototype.slice.call(arguments,1)):a.resolve(c)}};c.makePromise=f;f.prototype.then=function(a,b){return d(this,a,b)};v.call("isResolved,isFulfilled,isRejected,when,spread,send,get,put,del,post,invoke,keys,apply,call,bind,all,allResolved,view,viewInfo,timeout,delay,catch,finally,fail,fin,end".split(","),
function(a,b){f.prototype[b]=function(){return c[b].apply(c,[this].concat(g.call(arguments)))}},void 0);f.prototype.toSource=function(){return this.toString()};f.prototype.toString=function(){return"[object Promise]"};x(f.prototype);c.isPromise=t;c.isResolved=function(a){return!t(o(a))};c.isFulfilled=function(a){return!t(o(a))&&!B(a)};c.isRejected=B;var y=[],z=[];"undefined"!==typeof window&&console.log("Should be empty:",z);c.reject=h;h.prototype=p(f.prototype,{constructor:{value:h}});c.resolve=
i;c.ref=i;c.master=function(a){return f({isDef:function(){}},function(b){var c=g.call(arguments);return q.apply(void 0,[a].concat(c))},function(){return o(a)})};c.viewInfo=C;c.view=function(a){return C(a).when(function(b){var c;c="function"===b.type?function(){return r(a,void 0,arguments)}:{};var d=b.properties||{};Object.keys(d).forEach(function(b){"function"===d[b]&&(c[b]=function(){return H(a,b,arguments)})});return i(c)})};c.when=d;c.spread=function(a,b,c){return d(a,function(a){return b.apply(void 0,
a)},c)};c.async=function(a){return function(){function b(a,b){var j;try{j=c[a](b)}catch(i){return"[object StopIteration]"===Object.prototype.toString.call(i)?i.value:h(i)}return d(j,f,g)}var c=a.apply(this,arguments),f=b.bind(b,"send"),g=b.bind(b,"throw");return f()}};c.sender=m;c.Method=m;c.send=q;c.get=m("get");c.put=m("put");c.del=c["delete"]=m("del");var H=c.post=m("post");c.invoke=function(a,b){var c=g.call(arguments,2);return H(a,b,c)};var r=c.apply=m("apply");c.call=c["try"]=function(a,b){var c=
g.call(arguments,2);return r(a,b,c)};c.bind=function(a,b){var c=g.call(arguments,2);return function I(){var d=c.concat(g.call(arguments));if(this instanceof I){var f=function(){};f.prototype=a.prototype;var h=new f;return r(a,h,d).then(function(a){return Object(a)===a?a:h})}return r(a,b,d)}};c.keys=m("keys");c.all=D;c.allResolved=function(a){return d(a,function(a){return d(D(a.map(function(a){return d(a,s,s)})),function(){return a.map(i)})})};c["catch"]=c.fail=function(a,b){return d(a,void 0,b)};
c["finally"]=c.fin=function(a,b){return d(a,function(a){return d(b(),function(){return a})},function(a){return d(b(),function(){return h(a)})})};c.end=function(a){d(a,void 0,function(a){l(function(){throw a;})})};c.timeout=function(a,b){var c=j();d(a,c.resolve,c.reject);setTimeout(function(){c.reject("Timed out")},b);return c.promise};c.delay=function(a,b){void 0===b&&(b=a,a=void 0);var c=j();setTimeout(function(){c.resolve(a)},b);return c.promise};c.node=E;c.ncall=function(a,b){var c=g.call(arguments,
2);return E(a).apply(b,c)}});