promise-toolbox
Advanced tools
Comparing version 0.9.4 to 0.9.5
@@ -70,3 +70,3 @@ 'use strict'; | ||
return function () { | ||
if (fn) { | ||
if (fn !== null) { | ||
result = fn.apply(this, arguments); | ||
@@ -320,3 +320,3 @@ fn = null; | ||
(token._listeners || (token._listeners = [])).push(cancel); | ||
token.addHandler(cancel); | ||
}); | ||
@@ -335,3 +335,3 @@ if (_executor !== undefined) { | ||
this._listeners = null; | ||
this._handlers = null; | ||
this._promise = null; | ||
@@ -341,5 +341,3 @@ this._reason = undefined; | ||
var _cancelOnce = function cancelOnce(message) { | ||
_cancelOnce = _noop; | ||
executor(_once(function (message) { | ||
var reason = _this._reason = message instanceof Cancel ? message : new Cancel(message); | ||
@@ -353,15 +351,30 @@ | ||
// notify sync listeners (for race) | ||
var listeners = _this._listeners; | ||
if (listeners !== null) { | ||
_this._listeners = null; | ||
_forArray(listeners, function (listener) { | ||
return void listener(reason); | ||
}); | ||
var handlers = _this._handlers; | ||
if (handlers !== null) { | ||
_this._handlers = null; | ||
var _defer = defer(), | ||
promise = _defer.promise, | ||
_resolve = _defer.resolve; | ||
var wait = 0; | ||
var onSettle = function onSettle() { | ||
if (--wait === 0) { | ||
return _resolve(); | ||
} | ||
}; | ||
for (var i = 0, n = handlers.length; i < n; ++i) { | ||
try { | ||
var result = handlers[i](reason); | ||
if (isPromise(result)) { | ||
++wait; | ||
result.then(onSettle, onSettle); | ||
} | ||
} catch (_) {} | ||
} | ||
if (wait !== 0) { | ||
return promise; | ||
} | ||
} | ||
}; | ||
var cancel = function cancel(message) { | ||
return _cancelOnce(message); | ||
}; | ||
executor(cancel); | ||
})); | ||
} | ||
@@ -382,3 +395,22 @@ | ||
} | ||
// register a handler to execute when the token is canceled | ||
// | ||
// handlers are all executed in parallel, the promise returned by | ||
// the `cancel` function will wait for all handlers to be settled | ||
}, { | ||
key: 'addHandler', | ||
value: function addHandler(handler) { | ||
var handlers = this._handlers; | ||
if (handlers === null) { | ||
if (this.requested) { | ||
throw new TypeError('cannot add a handler to an already canceled token'); | ||
} | ||
handlers = this._handlers = []; | ||
} | ||
handlers.push(handler); | ||
} | ||
}, { | ||
key: 'throwIfRequested', | ||
@@ -385,0 +417,0 @@ value: function throwIfRequested() { |
@@ -1,2 +0,2 @@ | ||
(function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.promiseToolbox=a()}})(function(){return function b(c,d,e){function a(h,i){if(!d[h]){if(!c[h]){var j="function"==typeof require&&require;if(!i&&j)return j(h,!0);if(g)return g(h,!0);var k=new Error("Cannot find module '"+h+"'");throw k.code="MODULE_NOT_FOUND",k}var f=d[h]={exports:{}};c[h][0].call(f.exports,function(b){var d=c[h][1][b];return a(d?d:b)},f,f.exports,b,c,d,e)}return d[h].exports}for(var g="function"==typeof require&&require,f=0;f<e.length;f++)a(e[f]);return a}({1:[function(a,b,c){"use strict";function d(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return b&&("object"==typeof b||"function"==typeof b)?b:a}function e(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}function f(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function g(a){return E(this).then(function(b){return H(b,a)})}function h(a){return a&&this.then(function(b){return a(null,b)},function(b){return a(b)}).then(null,u),this}function j(a,b){this.d=b,this.p=a}function i(a){var b=this;return E(this).then(function(){return E(a()).then(function(){return b})},function(){return E(a()).then(function(){return b})})}function k(){return E(this).then(fa,ga)}Object.defineProperty(c,"__esModule",{value:!0}),c.try=c.attempt=c.TimeoutError=c.promisifyAll=c.promisify=c.finally=c.fromEvents=c.fromEvent=c.fromCallback=c.forOwn=c.forIterable=c.forIn=c.forEach=c.forArray=c.makeAsyncIterator=c.defer=c.cancellable=c.cancelable=c.CancelToken=c.Cancel=c.nodeify=c.wrapApply=c.wrapCall=c.isPromise=void 0;var l=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();c.all=g,c.asCallback=h,c.catchPlus=function(){var a,b=this,c=arguments.length-1;if(0>c||"function"!=typeof(a=arguments[c]))return this;var d;if(0!=c){d=Array(c);for(var e=0;e<c;++e)d[e]=arguments[e]}return E(this).then(null,function(e){if(d!==void 0){for(var f=0;f<c;++f)if(O(d[f],e))return a(e);}else if(!N(e))return a(e);return b})},c.delay=function(a){var b=this;return C(this)?this.then(function(b){return new Promise(function(c){setTimeout(function(){return c(b)},a)})}):new Promise(function(c){setTimeout(function(){return c(b)},a)})},c.disposer=function(a){return new j(this,a)},c.using=function(){var a=this,b=arguments.length-1;if(1>b)throw new TypeError("using expects at least 2 arguments");var c=arguments[b],d=arguments[0],e=1<b||!o(d);if(e){d=Array(b);for(var f=0;f<b;++f)d[f]=arguments[f]}else b=d.length;var g=t(function(a,c){var e=b,f=function(){0==--e&&a(c)},g=function(a){setTimeout(function(){throw a},0)};v(d,function(a){var b;null!=a&&"function"==typeof(b=a.d)?(a.p.then(function(a){return F(b,a).then(f,g)},f),a.p=a.d=null):--e})});return new Promise(function(f,h){var k=Array(b),i=b,l=function(a){l=n,o=function(){return g(h,a)},n()},m=function(a){return l(a)},n=function(){0==--i&&o()},o=function(){return(e?G:F)(c,k,a).then(function(a){return g(f,a)},function(a){return g(h,a)})};v(d,function(a,b){var c=a instanceof j?a.p:a;return null===c?void m(new TypeError("resource has already been disposed of")):void c.then(function(a){k[b]=a,n()},m)})})},c.ignoreErrors=function(){return C(this)?this.then(null,X):this},c.join=function(){var a,b=arguments.length-1,c=arguments[b];if(0==b)return new Promise(function(a){return a(c())});if(1!=b){a=Array(b);for(var d=0;d<b;++d)a[d]=arguments[d]}else if(!q(a=arguments[0]))return E(a).then(function(a){return c(a)});return H(a).then(function(a){return c.apply(null,a)})},c.lastly=i,c.reflect=k,c.settle=function(){return g.call(this,function(a){return k.call(a)})},c.some=function(a){return E(this).then(function(b){return ha(b,a)})},c.tap=function(a,b){var c=this,d=function(){return c};return E(this).then(a&&function(b){return E(a(b)).then(d)},b&&function(a){return E(b(a)).then(d)})},c.timeout=function(a,b){var c=this;return new Promise(function(d,e){var f=setTimeout(function(){if(f=null,"function"==typeof c.cancel&&c.cancel(),b)try{d(b())}catch(a){e(a)}else e(new ia)},a);E(c).then(function(a){null!==f&&clearTimeout(f),d(a)},function(a){null!==f&&clearTimeout(f),e(a)})})},c.unpromisify=function(){var a=this;return _(function(){var b,c=arguments.length-1;if(0>c||"function"!=typeof(b=arguments[c]))throw new Error("missing callback");for(var d=Array(c),e=0;e<c;++e)d[e]=arguments[e];G(a,d,this).then(function(a){return b(null,a)},function(a){return b(a)})},a.name,a.length+1)};var m=a("make-error");if("function"!=typeof Promise||"function"!=typeof Promise.reject||"function"!=typeof Promise.resolve)throw new Error("a standard Promise implementation is required (https://github.com/JsCommunity/promise-toolbox#usage)");var n=function(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:a.length;return c-=b.length,0<=c&&a.indexOf(b,c)===c},o=Array.isArray||function(a){return a instanceof Array},p=function(a){return"number"==typeof a&&0<=a&&a<Infinity&&Math.floor(a)===a},q=function(a){return a&&"function"!=typeof a&&p(a.length)},r="function"==typeof Symbol&&Symbol.iterator||"@@iterator",s=function(a){return a&&"function"==typeof a[r]},t=function(a){var b;return function(){return a&&(b=a.apply(this,arguments),a=null),b}},u=function(){},v=function(a,b){for(var c=a.length,d=0;d<c;++d)b(a[d],d,a)},w=function(a,b){for(var c in a)b(a[c],c,a)},x=function(a,b){for(var c,d=a[Symbol.iterator]();!(c=d.next()).done;)b(c.value,null,a)},y=Object.prototype.hasOwnProperty,z=function(a,b){for(var c in a)y.call(a,c)&&b(a[c],c,a)},A=function(a,b){return o(a)?v(a,b):s(a)?x(a,b):q(a)?v(a,b):z(a,b)},B=function(a,b){var c=q(a)?Array(a.length):{};return b&&A(a,function(d,e){c[e]=b(d,e,a)}),c},C=c.isPromise=function(a){return null!=a&&"function"==typeof a.then},D=function(a){return function(b,c){var d=Promise.resolve();return a(b,function(a,e){d=C(a)?d.then(function(){return a.then(function(a){return c(a,e,b)})}):d.then(function(){return c(a,e,b)})}),d}},E=function(a){return C(a)?a:Promise.resolve(a)},F=c.wrapCall=function(a,b,c){try{return E(a.call(c,b))}catch(a){return Promise.reject(a)}},G=c.wrapApply=function(a,b,c){try{return E(a.apply(c,b))}catch(a){return Promise.reject(a)}},H=function(a,b){return new Promise(function(c,d){var e=B(a,b),f=1,g=function(){if(0==--f){var a=e;e=null,c(a)}},h=function(a,b){e&&(e[b]=a,g())},i=function(a){e&&(e=null,d(a))};A(b?e:a,function(a,b){++f,C(a)?a.then(function(a){return h(a,b)},i):h(a,b)}),g()})};c.nodeify=h;var I=c.Cancel=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"this action has been canceled";f(this,a),Object.defineProperty(this,"message",{enumerable:!0,value:b})}return l(a,[{key:"toString",value:function(){return`Cancel: ${this.message}`}}]),a}(),J="CancelToken",K="function"==typeof Symbol&&Symbol.toStringTag||"@@toStringTag",L=c.CancelToken=function(){function a(b){var c=this;f(this,a),this._listeners=null,this._promise=null,this._reason=void 0,this._resolve=null;var d=function(a){d=u;var b=c._reason=a instanceof I?a:new I(a),e=c._resolve;null!==e&&(c._resolve=null,e(b));var f=c._listeners;null!==f&&(c._listeners=null,v(f,function(a){a(b)}))};b(function(a){return d(a)})}return l(a,null,[{key:"isCancelToken",value:function(a){return null!=a&&"function"==typeof a[K]&&a[K]()===J}},{key:"source",value:function(){var b,c=new a(function(a){b=a});return{cancel:b,token:c}}},{key:"race",value:function(b,c){return new a(function(a){A(b,function(b){var c=b.reason;return void 0===c?void(b._listeners||(b._listeners=[])).push(a):(a(c),!1)}),void 0!==c&&c(a)})}}]),l(a,[{key:"fork",value:function(b){if(void 0!==b)return a.race([this],b);var d,c=a.race([this],function(a){d=a});return{cancel:d,token:c}}},{key:"throwIfRequested",value:function(){var a=this._reason;if(void 0!==a)throw a}},{key:K,value:function(){return J}},{key:"promise",get:function(){var a=this,b=this._promise;if(null===b){var c=this._reason;b=this._promise=void 0===c?new Promise(function(b){a._resolve=b}):Promise.resolve(c)}return b}},{key:"reason",get:function(){return this._reason}},{key:"requested",get:function(){return void 0!==this._reason}}]),a}(),M=c.cancelable=function(a,b,c){function d(){var a=arguments.length;if(0!==a&&L.isCancelToken(arguments[0]))return e.apply(this,arguments);var b=L.source(),c=b.cancel,d=b.token,f=Array(a+1);f[0]=d;for(var g=0;g<a;++g)f[g+1]=arguments[g];var h=e.apply(this,f);return h.cancel=c,h}var e=c===void 0?a:c.value;return void 0===c?d:(c.value=d,c)};c.cancellable=M;var N=function(a){return a instanceof ReferenceError||a instanceof SyntaxError||a instanceof TypeError},O=function(a,b){if("function"==typeof a)return a===Error||a.prototype instanceof Error?b instanceof a:a(b);if(null!=b&&"object"==typeof a){for(var c in a)if(y.call(a,c)&&b[c]!==a[c])return!1;return!0}},P=c.defer=function(){var a,b,c=new Promise(function(c,d){a=c,b=d});return{promise:c,reject:b,resolve:a}},Q=c.makeAsyncIterator=function(a){var b=D(a);return function(a){return E(this).then(function(c){return b(c,a)}).then(u)}},R=c.forArray=Q(v),S=c.forEach=Q(A),T=c.forIn=Q(w),U=c.forIterable=Q(x),V=c.forOwn=Q(z),W=c.fromCallback=function(a){return new Promise(function(b,c){a(function(a,d){return a?c(a):b(d)})})},X=function(a){if(N(a))throw a},Y=function(a,b,c){var d=b.addEventListener||b.addListener||b.on;if(void 0===d)throw new Error("cannot register event listener");var e=b.removeEventListener||b.removeListener||b.off,f=[],g=u;return void 0!==e&&(g=t(function(){for(var a=0,c=f.length;a<c;a+=2)e.call(b,f[a],f[a+1])}),a.promise.then(g)),c?function(a,c){function e(){g();for(var b=arguments.length,d=Array(b),e=0;e<b;++e)d[e]=arguments[e];d.event=a,c(d)}f.push(a,e),d.call(b,a,e)}:function(a,c){var e=function(a){g(),c(a)};f.push(a,e),d.call(b,a,e)}},Z=c.fromEvent=M(function(a,b,c){var d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:{};return new Promise(function(e,f){var g=Y(a,b,d.array);if(g(c,e),!d.ignoreErrors){var h=d.error,i=void 0===h?"error":h;i!==c&&g(i,f)}})}),$=c.fromEvents=M(function(a,b,c){var d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:["error"];return new Promise(function(e,f){var g=Y(a,b,!0);v(c,function(a){return g(a,e)}),v(d,function(a){return g(a,f)})})});c.finally=i;var _=function(){var a=Object.defineProperties;try{var b=a(function(){},{length:{value:2},name:{value:"foo"}});if(2===b.length&&"foo"===b.name)return function(b,c,d){return a(b,{length:{configurable:!0,value:d},name:{configurable:!0,value:c}})}}catch(a){}return function(a){return a}}(),aa=c.promisify=function(a,b){return _(function(){for(var c=this,d=arguments.length,e=Array(d+1),f=0;f<d;++f)e[f]=arguments[f];return new Promise(function(f,g){e[d]=function(a,b){return a?g(a):f(b)},a.apply(b||c,e)})},a.name,a.length&&a.length-1)},ba=function(a){return!(n(a,"Sync")||n(a,"Async"))&&a},ca=c.promisifyAll=function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},c=b.mapper,d=void 0===c?ba:c,e=b.target,f=void 0===e?{}:e,g=b.context,h=void 0===g?a:g;return w(a,function(b,c){var e;"function"==typeof b&&(e=d(c,b,a))&&(f[e]=aa(b,h))}),f},da=function(){return!1},ea=function(){return!0},fa=function(a){return function(b){return{__proto__:a,value:function(){return b}}}}({isFulfilled:ea,isPending:da,isRejected:da,isResolved:ea,reason:function(){throw new Error("no reason, the promise has resolved")}}),ga=function(a){return function(b){return{__proto__:a,reason:function(){return b}}}}({isFulfilled:da,isPending:da,isRejected:ea,isResolved:da,value:function(){throw new Error("no value, the promise has rejected")}}),ha=function(a,b){return new Promise(function(c,d){var e=[],f=[],g=function(a){e&&(e.push(a),0==--b&&(c(e),e=f=null))},h=-b,i=function(a){e&&(f.push(a),0==--h&&(d(f),e=f=null))};A(a,function(a){++h,E(a).then(g,i)})})},ia=c.TimeoutError=function(a){function b(){return f(this,b),d(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,"operation timed out"))}return e(b,a),b}(m.BaseError),ja=c.attempt=function(a){return new Promise(function(b){return b(a())})};c.try=ja},{"make-error":2}],2:[function(a,b,c){"use strict";function d(a){a&&e(this,"message",{configurable:!0,value:a,writable:!0});var b=this.constructor.name;b&&b!==this.name&&e(this,"name",{configurable:!0,value:b,writable:!0}),f(this,this.constructor)}var e=Object.defineProperty,f=Error.captureStackTrace;f||(f=function(a){var b=new Error;e(a,"stack",{configurable:!0,get:function(){var a=b.stack;return e(this,"stack",{value:a}),a},set:function(b){e(a,"stack",{configurable:!0,value:b,writable:!0})}})}),d.prototype=Object.create(Error.prototype,{constructor:{configurable:!0,value:d,writable:!0}});var g=function(){function a(a,b){return e(a,"name",{configurable:!0,value:b})}try{var b=function(){};if(a(b,"foo"),"foo"===b.name)return a}catch(a){}}();c=b.exports=function(a,b){if(null==b||b===Error)b=d;else if("function"!=typeof b)throw new TypeError("super_ should be a function");var c;if("string"==typeof a)c=a,a=function(){b.apply(this,arguments)},g&&(g(a,c),c=null);else if("function"!=typeof a)throw new TypeError("constructor should be either a string or a function");a.super_=a["super"]=b;var e={constructor:{configurable:!0,value:a,writable:!0}};return null!=c&&(e.name={configurable:!0,value:c,writable:!0}),a.prototype=Object.create(b.prototype,e),a},c.BaseError=d},{}]},{},[1])(1)}); | ||
(function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.promiseToolbox=a()}})(function(){return function b(c,d,e){function a(h,i){if(!d[h]){if(!c[h]){var j="function"==typeof require&&require;if(!i&&j)return j(h,!0);if(g)return g(h,!0);var k=new Error("Cannot find module '"+h+"'");throw k.code="MODULE_NOT_FOUND",k}var f=d[h]={exports:{}};c[h][0].call(f.exports,function(b){var d=c[h][1][b];return a(d?d:b)},f,f.exports,b,c,d,e)}return d[h].exports}for(var g="function"==typeof require&&require,f=0;f<e.length;f++)a(e[f]);return a}({1:[function(a,b,c){"use strict";function d(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return b&&("object"==typeof b||"function"==typeof b)?b:a}function e(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}function f(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function g(a){return E(this).then(function(b){return H(b,a)})}function h(a){return a&&this.then(function(b){return a(null,b)},function(b){return a(b)}).then(null,u),this}function j(a,b){this.d=b,this.p=a}function i(a){var b=this;return E(this).then(function(){return E(a()).then(function(){return b})},function(){return E(a()).then(function(){return b})})}function k(){return E(this).then(fa,ga)}Object.defineProperty(c,"__esModule",{value:!0}),c.try=c.attempt=c.TimeoutError=c.promisifyAll=c.promisify=c.finally=c.fromEvents=c.fromEvent=c.fromCallback=c.forOwn=c.forIterable=c.forIn=c.forEach=c.forArray=c.makeAsyncIterator=c.defer=c.cancellable=c.cancelable=c.CancelToken=c.Cancel=c.nodeify=c.wrapApply=c.wrapCall=c.isPromise=void 0;var l=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();c.all=g,c.asCallback=h,c.catchPlus=function(){var a,b=this,c=arguments.length-1;if(0>c||"function"!=typeof(a=arguments[c]))return this;var d;if(0!=c){d=Array(c);for(var e=0;e<c;++e)d[e]=arguments[e]}return E(this).then(null,function(e){if(d!==void 0){for(var f=0;f<c;++f)if(O(d[f],e))return a(e);}else if(!N(e))return a(e);return b})},c.delay=function(a){var b=this;return C(this)?this.then(function(b){return new Promise(function(c){setTimeout(function(){return c(b)},a)})}):new Promise(function(c){setTimeout(function(){return c(b)},a)})},c.disposer=function(a){return new j(this,a)},c.using=function(){var a=this,b=arguments.length-1;if(1>b)throw new TypeError("using expects at least 2 arguments");var c=arguments[b],d=arguments[0],e=1<b||!o(d);if(e){d=Array(b);for(var f=0;f<b;++f)d[f]=arguments[f]}else b=d.length;var g=t(function(a,c){var e=b,f=function(){0==--e&&a(c)},g=function(a){setTimeout(function(){throw a},0)};v(d,function(a){var b;null!=a&&"function"==typeof(b=a.d)?(a.p.then(function(a){return F(b,a).then(f,g)},f),a.p=a.d=null):--e})});return new Promise(function(f,h){var k=Array(b),i=b,l=function(a){l=n,o=function(){return g(h,a)},n()},m=function(a){return l(a)},n=function(){0==--i&&o()},o=function(){return(e?G:F)(c,k,a).then(function(a){return g(f,a)},function(a){return g(h,a)})};v(d,function(a,b){var c=a instanceof j?a.p:a;return null===c?void m(new TypeError("resource has already been disposed of")):void c.then(function(a){k[b]=a,n()},m)})})},c.ignoreErrors=function(){return C(this)?this.then(null,X):this},c.join=function(){var a,b=arguments.length-1,c=arguments[b];if(0==b)return new Promise(function(a){return a(c())});if(1!=b){a=Array(b);for(var d=0;d<b;++d)a[d]=arguments[d]}else if(!q(a=arguments[0]))return E(a).then(function(a){return c(a)});return H(a).then(function(a){return c.apply(null,a)})},c.lastly=i,c.reflect=k,c.settle=function(){return g.call(this,function(a){return k.call(a)})},c.some=function(a){return E(this).then(function(b){return ha(b,a)})},c.tap=function(a,b){var c=this,d=function(){return c};return E(this).then(a&&function(b){return E(a(b)).then(d)},b&&function(a){return E(b(a)).then(d)})},c.timeout=function(a,b){var c=this;return new Promise(function(d,e){var f=setTimeout(function(){if(f=null,"function"==typeof c.cancel&&c.cancel(),b)try{d(b())}catch(a){e(a)}else e(new ia)},a);E(c).then(function(a){null!==f&&clearTimeout(f),d(a)},function(a){null!==f&&clearTimeout(f),e(a)})})},c.unpromisify=function(){var a=this;return _(function(){var b,c=arguments.length-1;if(0>c||"function"!=typeof(b=arguments[c]))throw new Error("missing callback");for(var d=Array(c),e=0;e<c;++e)d[e]=arguments[e];G(a,d,this).then(function(a){return b(null,a)},function(a){return b(a)})},a.name,a.length+1)};var m=a("make-error");if("function"!=typeof Promise||"function"!=typeof Promise.reject||"function"!=typeof Promise.resolve)throw new Error("a standard Promise implementation is required (https://github.com/JsCommunity/promise-toolbox#usage)");var n=function(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:a.length;return c-=b.length,0<=c&&a.indexOf(b,c)===c},o=Array.isArray||function(a){return a instanceof Array},p=function(a){return"number"==typeof a&&0<=a&&a<Infinity&&Math.floor(a)===a},q=function(a){return a&&"function"!=typeof a&&p(a.length)},r="function"==typeof Symbol&&Symbol.iterator||"@@iterator",s=function(a){return a&&"function"==typeof a[r]},t=function(a){var b;return function(){return null!==a&&(b=a.apply(this,arguments),a=null),b}},u=function(){},v=function(a,b){for(var c=a.length,d=0;d<c;++d)b(a[d],d,a)},w=function(a,b){for(var c in a)b(a[c],c,a)},x=function(a,b){for(var c,d=a[Symbol.iterator]();!(c=d.next()).done;)b(c.value,null,a)},y=Object.prototype.hasOwnProperty,z=function(a,b){for(var c in a)y.call(a,c)&&b(a[c],c,a)},A=function(a,b){return o(a)?v(a,b):s(a)?x(a,b):q(a)?v(a,b):z(a,b)},B=function(a,b){var c=q(a)?Array(a.length):{};return b&&A(a,function(d,e){c[e]=b(d,e,a)}),c},C=c.isPromise=function(a){return null!=a&&"function"==typeof a.then},D=function(a){return function(b,c){var d=Promise.resolve();return a(b,function(a,e){d=C(a)?d.then(function(){return a.then(function(a){return c(a,e,b)})}):d.then(function(){return c(a,e,b)})}),d}},E=function(a){return C(a)?a:Promise.resolve(a)},F=c.wrapCall=function(a,b,c){try{return E(a.call(c,b))}catch(a){return Promise.reject(a)}},G=c.wrapApply=function(a,b,c){try{return E(a.apply(c,b))}catch(a){return Promise.reject(a)}},H=function(a,b){return new Promise(function(c,d){var e=B(a,b),f=1,g=function(){if(0==--f){var a=e;e=null,c(a)}},h=function(a,b){e&&(e[b]=a,g())},i=function(a){e&&(e=null,d(a))};A(b?e:a,function(a,b){++f,C(a)?a.then(function(a){return h(a,b)},i):h(a,b)}),g()})};c.nodeify=h;var I=c.Cancel=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"this action has been canceled";f(this,a),Object.defineProperty(this,"message",{enumerable:!0,value:b})}return l(a,[{key:"toString",value:function(){return`Cancel: ${this.message}`}}]),a}(),J="CancelToken",K="function"==typeof Symbol&&Symbol.toStringTag||"@@toStringTag",L=c.CancelToken=function(){function a(b){var c=this;f(this,a),this._handlers=null,this._promise=null,this._reason=void 0,this._resolve=null,b(t(function(a){var b=c._reason=a instanceof I?a:new I(a),d=c._resolve;null!==d&&(c._resolve=null,d(b));var e=c._handlers;if(null!==e){c._handlers=null;for(var f=P(),g=f.promise,h=f.resolve,j=0,k=function(){if(0==--j)return h()},l=0,i=e.length;l<i;++l)try{var m=e[l](b);C(m)&&(++j,m.then(k,k))}catch(a){}if(0!=j)return g}}))}return l(a,null,[{key:"isCancelToken",value:function(a){return null!=a&&"function"==typeof a[K]&&a[K]()===J}},{key:"source",value:function(){var b,c=new a(function(a){b=a});return{cancel:b,token:c}}},{key:"race",value:function(b,c){return new a(function(a){A(b,function(b){var c=b.reason;return void 0===c?void b.addHandler(a):(a(c),!1)}),void 0!==c&&c(a)})}}]),l(a,[{key:"fork",value:function(b){if(void 0!==b)return a.race([this],b);var d,c=a.race([this],function(a){d=a});return{cancel:d,token:c}}},{key:"addHandler",value:function(a){var b=this._handlers;if(null===b){if(this.requested)throw new TypeError("cannot add a handler to an already canceled token");b=this._handlers=[]}b.push(a)}},{key:"throwIfRequested",value:function(){var a=this._reason;if(void 0!==a)throw a}},{key:K,value:function(){return J}},{key:"promise",get:function(){var a=this,b=this._promise;if(null===b){var c=this._reason;b=this._promise=void 0===c?new Promise(function(b){a._resolve=b}):Promise.resolve(c)}return b}},{key:"reason",get:function(){return this._reason}},{key:"requested",get:function(){return void 0!==this._reason}}]),a}(),M=c.cancelable=function(a,b,c){function d(){var a=arguments.length;if(0!==a&&L.isCancelToken(arguments[0]))return e.apply(this,arguments);var b=L.source(),c=b.cancel,d=b.token,f=Array(a+1);f[0]=d;for(var g=0;g<a;++g)f[g+1]=arguments[g];var h=e.apply(this,f);return h.cancel=c,h}var e=c===void 0?a:c.value;return void 0===c?d:(c.value=d,c)};c.cancellable=M;var N=function(a){return a instanceof ReferenceError||a instanceof SyntaxError||a instanceof TypeError},O=function(a,b){if("function"==typeof a)return a===Error||a.prototype instanceof Error?b instanceof a:a(b);if(null!=b&&"object"==typeof a){for(var c in a)if(y.call(a,c)&&b[c]!==a[c])return!1;return!0}},P=c.defer=function(){var a,b,c=new Promise(function(c,d){a=c,b=d});return{promise:c,reject:b,resolve:a}},Q=c.makeAsyncIterator=function(a){var b=D(a);return function(a){return E(this).then(function(c){return b(c,a)}).then(u)}},R=c.forArray=Q(v),S=c.forEach=Q(A),T=c.forIn=Q(w),U=c.forIterable=Q(x),V=c.forOwn=Q(z),W=c.fromCallback=function(a){return new Promise(function(b,c){a(function(a,d){return a?c(a):b(d)})})},X=function(a){if(N(a))throw a},Y=function(a,b,c){var d=b.addEventListener||b.addListener||b.on;if(void 0===d)throw new Error("cannot register event listener");var e=b.removeEventListener||b.removeListener||b.off,f=[],g=u;return void 0!==e&&(g=t(function(){for(var a=0,c=f.length;a<c;a+=2)e.call(b,f[a],f[a+1])}),a.promise.then(g)),c?function(a,c){function e(){g();for(var b=arguments.length,d=Array(b),e=0;e<b;++e)d[e]=arguments[e];d.event=a,c(d)}f.push(a,e),d.call(b,a,e)}:function(a,c){var e=function(a){g(),c(a)};f.push(a,e),d.call(b,a,e)}},Z=c.fromEvent=M(function(a,b,c){var d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:{};return new Promise(function(e,f){var g=Y(a,b,d.array);if(g(c,e),!d.ignoreErrors){var h=d.error,i=void 0===h?"error":h;i!==c&&g(i,f)}})}),$=c.fromEvents=M(function(a,b,c){var d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:["error"];return new Promise(function(e,f){var g=Y(a,b,!0);v(c,function(a){return g(a,e)}),v(d,function(a){return g(a,f)})})});c.finally=i;var _=function(){var a=Object.defineProperties;try{var b=a(function(){},{length:{value:2},name:{value:"foo"}});if(2===b.length&&"foo"===b.name)return function(b,c,d){return a(b,{length:{configurable:!0,value:d},name:{configurable:!0,value:c}})}}catch(a){}return function(a){return a}}(),aa=c.promisify=function(a,b){return _(function(){for(var c=this,d=arguments.length,e=Array(d+1),f=0;f<d;++f)e[f]=arguments[f];return new Promise(function(f,g){e[d]=function(a,b){return a?g(a):f(b)},a.apply(b||c,e)})},a.name,a.length&&a.length-1)},ba=function(a){return!(n(a,"Sync")||n(a,"Async"))&&a},ca=c.promisifyAll=function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},c=b.mapper,d=void 0===c?ba:c,e=b.target,f=void 0===e?{}:e,g=b.context,h=void 0===g?a:g;return w(a,function(b,c){var e;"function"==typeof b&&(e=d(c,b,a))&&(f[e]=aa(b,h))}),f},da=function(){return!1},ea=function(){return!0},fa=function(a){return function(b){return{__proto__:a,value:function(){return b}}}}({isFulfilled:ea,isPending:da,isRejected:da,isResolved:ea,reason:function(){throw new Error("no reason, the promise has resolved")}}),ga=function(a){return function(b){return{__proto__:a,reason:function(){return b}}}}({isFulfilled:da,isPending:da,isRejected:ea,isResolved:da,value:function(){throw new Error("no value, the promise has rejected")}}),ha=function(a,b){return new Promise(function(c,d){var e=[],f=[],g=function(a){e&&(e.push(a),0==--b&&(c(e),e=f=null))},h=-b,i=function(a){e&&(f.push(a),0==--h&&(d(f),e=f=null))};A(a,function(a){++h,E(a).then(g,i)})})},ia=c.TimeoutError=function(a){function b(){return f(this,b),d(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,"operation timed out"))}return e(b,a),b}(m.BaseError),ja=c.attempt=function(a){return new Promise(function(b){return b(a())})};c.try=ja},{"make-error":2}],2:[function(a,b,c){"use strict";function d(a){a&&e(this,"message",{configurable:!0,value:a,writable:!0});var b=this.constructor.name;b&&b!==this.name&&e(this,"name",{configurable:!0,value:b,writable:!0}),f(this,this.constructor)}var e=Object.defineProperty,f=Error.captureStackTrace;f||(f=function(a){var b=new Error;e(a,"stack",{configurable:!0,get:function(){var a=b.stack;return e(this,"stack",{value:a}),a},set:function(b){e(a,"stack",{configurable:!0,value:b,writable:!0})}})}),d.prototype=Object.create(Error.prototype,{constructor:{configurable:!0,value:d,writable:!0}});var g=function(){function a(a,b){return e(a,"name",{configurable:!0,value:b})}try{var b=function(){};if(a(b,"foo"),"foo"===b.name)return a}catch(a){}}();c=b.exports=function(a,b){if(null==b||b===Error)b=d;else if("function"!=typeof b)throw new TypeError("super_ should be a function");var c;if("string"==typeof a)c=a,a=function(){b.apply(this,arguments)},g&&(g(a,c),c=null);else if("function"!=typeof a)throw new TypeError("constructor should be either a string or a function");a.super_=a["super"]=b;var e={constructor:{configurable:!0,value:a,writable:!0}};return null!=c&&(e.name={configurable:!0,value:c,writable:!0}),a.prototype=Object.create(b.prototype,e),a},c.BaseError=d},{}]},{},[1])(1)}); | ||
{ | ||
"name": "promise-toolbox", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"license": "ISC", | ||
@@ -5,0 +5,0 @@ "description": "Essential utils for promises", |
@@ -133,2 +133,37 @@ # promise-toolbox [![Build Status](https://travis-ci.org/JsCommunity/promise-toolbox.png?branch=master)](https://travis-ci.org/JsCommunity/promise-toolbox) | ||
#### Registering async handlers | ||
> Asynchronous handlers are executed on token cancelation and the | ||
promise returned by the `cancel` function will wait for all handlers | ||
to settle. | ||
```js | ||
function httpRequest (cancelToken, opts) { | ||
const req = http.request(opts) | ||
req.end() | ||
cancelToken.addHandler(() => { | ||
req.abort() | ||
// waits for the socket to really close for the cancelation to be | ||
// complete | ||
return fromEvent(req, 'close') | ||
}) | ||
return fromEvent(req, 'response') | ||
} | ||
const { cancel, token } = CancelToken.source() | ||
httpRequest(token, { | ||
hostname: 'example.org' | ||
}).then(response => { | ||
// do something with the response of the request | ||
}) | ||
// wraps with Promise.resolve() because cancel only returns a promise | ||
// if a handler has returned a promise | ||
Promise.resolve(cancel()).then(() => { | ||
// the request has been properly canceled | ||
}) | ||
``` | ||
#### Is cancel token? | ||
@@ -135,0 +170,0 @@ |
Sorry, the diff of this file is not supported yet
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
118061
1031
722