promise-toolbox
Advanced tools
Comparing version 0.8.3 to 0.9.0
@@ -14,2 +14,4 @@ 'use strict'; | ||
exports.delay = delay; | ||
exports.disposer = disposer; | ||
exports.using = using; | ||
exports.join = join; | ||
@@ -65,2 +67,13 @@ exports.lastly = lastly; | ||
var _once = function _once(fn) { | ||
var result = void 0; | ||
return function () { | ||
if (fn) { | ||
result = fn.apply(this, arguments); | ||
fn = null; | ||
} | ||
return result; | ||
}; | ||
}; | ||
var _noop = function _noop() {}; | ||
@@ -152,2 +165,10 @@ | ||
try { | ||
return _wrap(fn.call(thisArg, args)); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
var _wrapApply = function _wrapApply(fn, args, thisArg) { | ||
try { | ||
return _wrap(fn.apply(thisArg, args)); | ||
@@ -346,2 +367,18 @@ } catch (error) { | ||
_createClass(CancelToken, [{ | ||
key: 'fork', | ||
value: function fork(executor) { | ||
if (executor !== undefined) { | ||
return CancelToken.race([this, new CancelToken(executor)]); | ||
} | ||
var _CancelToken$source2 = CancelToken.source(), | ||
cancel = _CancelToken$source2.cancel, | ||
token = _CancelToken$source2.token; | ||
return { | ||
cancel, | ||
token: CancelToken.race([this, token]) | ||
}; | ||
} | ||
}, { | ||
key: 'throwIfRequested', | ||
@@ -418,5 +455,5 @@ value: function throwIfRequested() { | ||
var _CancelToken$source2 = CancelToken.source(), | ||
cancel = _CancelToken$source2.cancel, | ||
token = _CancelToken$source2.token; | ||
var _CancelToken$source3 = CancelToken.source(), | ||
cancel = _CancelToken$source3.cancel, | ||
token = _CancelToken$source3.token; | ||
@@ -556,2 +593,113 @@ var args = new Array(length + 1); | ||
function Resource(promise, disposer) { | ||
this.d = disposer; | ||
this.p = promise; | ||
} | ||
// Usage: promise::disposer(disposer) | ||
function disposer(disposer) { | ||
return new Resource(this, disposer); | ||
} | ||
// Usage: using(disposers…, handler) | ||
function using() { | ||
var _this5 = this; | ||
var nResources = arguments.length - 1; | ||
if (nResources < 1) { | ||
throw new TypeError('using expects at least 2 arguments'); | ||
} | ||
var handler = arguments[nResources]; | ||
var resources = arguments[0]; | ||
var spread = nResources > 1 || !_isArray(resources); | ||
if (spread) { | ||
resources = new Array(nResources); | ||
for (var i = 0; i < nResources; ++i) { | ||
resources[i] = arguments[i]; | ||
} | ||
} else { | ||
nResources = resources.length; | ||
} | ||
var dispose = _once(function (fn, value) { | ||
var leftToProcess = nResources; | ||
var onSettle = function onSettle() { | ||
if (--leftToProcess === 0) { | ||
fn(value); | ||
} | ||
}; | ||
// like Bluebird, on failure to dispose a resource, throw an async error | ||
var onFailure = function onFailure(reason) { | ||
setTimeout(function () { | ||
throw reason; | ||
}, 0); | ||
}; | ||
_forArray(resources, function (resource) { | ||
var d = void 0; | ||
if (resource != null && typeof (d = resource.d) === 'function') { | ||
resource.p.then(function (value) { | ||
return _wrapCall(d, value).then(onSettle, onFailure); | ||
}, onSettle); | ||
resource.p = resource.d = null; | ||
} else { | ||
--leftToProcess; | ||
} | ||
}); | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
var values = new Array(nResources); | ||
var leftToProcess = nResources; | ||
var _onProviderFailure_ = function onProviderFailure_(reason) { | ||
_onProviderFailure_ = onProviderSettle; | ||
onSettle = function onSettle() { | ||
return dispose(reject, reason); | ||
}; | ||
onProviderSettle(); | ||
}; | ||
var onProviderFailure = function onProviderFailure(reason) { | ||
return _onProviderFailure_(reason); | ||
}; | ||
var onProviderSettle = function onProviderSettle() { | ||
if (--leftToProcess === 0) { | ||
onSettle(); | ||
} | ||
}; | ||
var onSettle = function onSettle() { | ||
return (spread ? _wrapApply : _wrapCall)(handler, values, _this5).then(function (value) { | ||
return dispose(resolve, value); | ||
}, function (reason) { | ||
return dispose(reject, reason); | ||
}); | ||
}; | ||
_forArray(resources, function (resource, i) { | ||
var p = resource instanceof Resource ? resource.p : resource; | ||
if (p === null) { | ||
onProviderFailure(new TypeError('resource has already been disposed of')); | ||
return; | ||
} | ||
p.then(function (value) { | ||
values[i] = value; | ||
onProviderSettle(); | ||
}, onProviderFailure); | ||
}); | ||
}); | ||
} | ||
// ------------------------------------------------------------------- | ||
var makeAsyncIterator = exports.makeAsyncIterator = function makeAsyncIterator(iterator) { | ||
@@ -623,11 +771,11 @@ var asyncIterator = _makeAsyncIterator(iterator); | ||
function lastly(cb) { | ||
var _this5 = this; | ||
var _this6 = this; | ||
return _wrap(this).then(function (value) { | ||
return _wrap(cb()).then(function () { | ||
return _this5; | ||
return _this6; | ||
}); | ||
}, function (reason) { | ||
return _wrap(cb()).then(function () { | ||
return _this5; | ||
return _this6; | ||
}); | ||
@@ -673,3 +821,3 @@ }); | ||
return _setFunctionNameAndLength(function () { | ||
var _this6 = this; | ||
var _this7 = this; | ||
@@ -688,3 +836,3 @@ var length = arguments.length; | ||
fn.apply(context || _this6, args); | ||
fn.apply(context || _this7, args); | ||
}); | ||
@@ -840,6 +988,6 @@ }, fn.name, fn.length && fn.length - 1); | ||
function tap(onResolved, onRejected) { | ||
var _this7 = this; | ||
var _this8 = this; | ||
var forward = function forward() { | ||
return _this7; | ||
return _this8; | ||
}; | ||
@@ -872,3 +1020,3 @@ | ||
function timeout(ms, cb) { | ||
var _this9 = this; | ||
var _this10 = this; | ||
@@ -879,4 +1027,4 @@ return new Promise(function (resolve, reject) { | ||
if (typeof _this9.cancel === 'function') { | ||
_this9.cancel(); | ||
if (typeof _this10.cancel === 'function') { | ||
_this10.cancel(); | ||
} | ||
@@ -895,3 +1043,3 @@ | ||
_wrap(_this9).then(function (value) { | ||
_wrap(_this10).then(function (value) { | ||
handle !== null && clearTimeout(handle); | ||
@@ -933,3 +1081,3 @@ resolve(value); | ||
_wrapCall(fn, args, this).then(function (result) { | ||
_wrapApply(fn, args, this).then(function (result) { | ||
return cb(null, result); | ||
@@ -936,0 +1084,0 @@ }, function (reason) { |
@@ -1,2 +0,2 @@ | ||
(function(b){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=b();else if("function"==typeof define&&define.amd)define([],b);else{var c;c="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,c.promiseToolbox=b()}})(function(){return function h(j,k,m){function p(w,y){if(!k[w]){if(!j[w]){var z="function"==typeof require&&require;if(!y&&z)return z(w,!0);if(q)return q(w,!0);var A=new Error("Cannot find module '"+w+"'");throw A.code="MODULE_NOT_FOUND",A}var B=k[w]={exports:{}};j[w][0].call(B.exports,function(C){var D=j[w][1][C];return p(D?D:C)},B,B.exports,h,j,k,m)}return k[w].exports}for(var q="function"==typeof require&&require,v=0;v<m.length;v++)p(m[v]);return p}({1:[function(h,j,k){"use strict";function m(Ea,Fa){if(!Ea)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return Fa&&("object"==typeof Fa||"function"==typeof Fa)?Fa:Ea}function p(Ea,Fa){if("function"!=typeof Fa&&null!==Fa)throw new TypeError("Super expression must either be null or a function, not "+typeof Fa);Ea.prototype=Object.create(Fa&&Fa.prototype,{constructor:{value:Ea,enumerable:!1,writable:!0,configurable:!0}}),Fa&&(Object.setPrototypeOf?Object.setPrototypeOf(Ea,Fa):Ea.__proto__=Fa)}function q(Ea,Fa){if(!(Ea instanceof Fa))throw new TypeError("Cannot call a class as a function")}function v(Ea){return $(this).then(function(Fa){return ba(Fa,Ea)})}function w(Ea){return Ea&&this.then(function(Fa){return Ea(null,Fa)},function(Fa){return Ea(Fa)}).then(null,Q),this}function B(Ea){var Fa=this;return $(this).then(function(){return $(Ea()).then(function(){return Fa})},function(){return $(Ea()).then(function(){return Fa})})}function C(){return $(this).then(za,Aa)}Object.defineProperty(k,"__esModule",{value:!0}),k.try=k.attempt=k.TimeoutError=k.promisifyAll=k.promisify=k.finally=k.fromCallback=k.forOwn=k.forIterable=k.forIn=k.forEach=k.forArray=k.makeAsyncIterator=k.defer=k.cancelable=k.cancellable=k.CancelToken=k.Cancel=k.nodeify=k.isPromise=void 0;var I=function(){function Ea(Fa,Ga){for(var Ia,Ha=0;Ha<Ga.length;Ha++)Ia=Ga[Ha],Ia.enumerable=Ia.enumerable||!1,Ia.configurable=!0,"value"in Ia&&(Ia.writable=!0),Object.defineProperty(Fa,Ia.key,Ia)}return function(Fa,Ga,Ha){return Ga&&Ea(Fa.prototype,Ga),Ha&&Ea(Fa,Ha),Fa}}();k.all=v,k.asCallback=w,k.catchPlus=function(){var Ea=this,Fa=arguments.length-1,Ga;if(0>Fa||"function"!=typeof(Ga=arguments[Fa]))return this;var Ha;if(Fa){Ha=Array(Fa);for(var Ia=0;Ia<Fa;++Ia)Ha[Ia]=arguments[Ia]}return $(this).then(null,function(Ja){if(Ha){for(var Ka=0;Ka<Fa;++Ka)if(ka(Ha[Ka],Ja))return Ga(Ja);}else if(ha(Ja))return Ga(Ja);return Ea})},k.delay=function(Ea){var Fa=this;return Y(this)?this.then(function(Ga){return new Promise(function(Ha){setTimeout(function(){return Ha(Ga)},Ea)})}):new Promise(function(Ga){setTimeout(function(){return Ga(Fa)},Ea)})},k.join=function(){var Ea=arguments.length-1,Fa=arguments[Ea],Ga;if(0==Ea)return new Promise(function(Ia){return Ia(Fa())});if(1!=Ea){Ga=Array(Ea);for(var Ha=0;Ha<Ea;++Ha)Ga[Ha]=arguments[Ha]}else if(!N(Ga=arguments[0]))return $(Ga).then(function(Ia){return Fa(Ia)});return ba(Ga).then(function(Ia){return Fa.apply(null,Ia)})},k.lastly=B,k.reflect=C,k.settle=function(){return v.call(this,function(Ea){return C.call(Ea)})},k.some=function(Ea){return $(this).then(function(Fa){return Ba(Fa,Ea)})},k.tap=function(Ea,Fa){var Ga=this,Ha=function(){return Ga};return $(this).then(Ea&&function(Ia){return $(Ea(Ia)).then(Ha)},Fa&&function(Ia){return $(Fa(Ia)).then(Ha)})},k.timeout=function(Ea,Fa){var Ga=this;return new Promise(function(Ha,Ia){var Ja=setTimeout(function(){if(Ja=null,"function"==typeof Ga.cancel&&Ga.cancel(),Fa)try{Ha(Fa())}catch(Ka){Ia(Ka)}else Ia(new Ca)},Ea);$(Ga).then(function(Ka){null!==Ja&&clearTimeout(Ja),Ha(Ka)},function(Ka){null!==Ja&&clearTimeout(Ja),Ia(Ka)})})},k.unpromisify=function(){var Ea=this;return ta(function(){var Fa=arguments.length-1,Ga;if(0>Fa||"function"!=typeof(Ga=arguments[Fa]))throw new Error("missing callback");for(var Ha=Array(Fa),Ia=0;Ia<Fa;++Ia)Ha[Ia]=arguments[Ia];aa(Ea,Ha,this).then(function(Ja){return Ga(null,Ja)},function(Ja){return Ga(Ja)})},Ea.name,Ea.length+1)};var J=h("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 K=function(Fa,Ga){var Ha=2<arguments.length&&void 0!==arguments[2]?arguments[2]:Fa.length;return Ha-=Ga.length,0<=Ha&&Fa.indexOf(Ga,Ha)===Ha},L=Array.isArray||function(Ea){return Ea instanceof Array},M=function(Fa){return"number"==typeof Fa&&0<=Fa&&Fa<Infinity&&Math.floor(Fa)===Fa},N=function(Fa){return Fa&&"function"!=typeof Fa&&M(Fa.length)},O="function"==typeof Symbol&&Symbol.iterator||"@@iterator",P=function(Fa){return Fa&&"function"==typeof Fa[O]},Q=function(){},R=function(Fa,Ga){for(var Ha=Fa.length,Ia=0;Ia<Ha;++Ia)Ga(Fa[Ia],Ia,Fa)},S=function(Fa,Ga){for(var Ha in Fa)Ga(Fa[Ha],Ha,Fa)},T=function(Fa,Ga){for(var Ia,Ha=Fa[Symbol.iterator]();!(Ia=Ha.next()).done;)Ga(Ia.value,null,Fa)},U=Object.prototype.hasOwnProperty,V=function(Fa,Ga){for(var Ha in Fa)U.call(Fa,Ha)&&Ga(Fa[Ha],Ha,Fa)},W=function(Fa,Ga){return L(Fa)?R(Fa,Ga):P(Fa)?T(Fa,Ga):N(Fa)?R(Fa,Ga):V(Fa,Ga)},X=function(Fa,Ga){var Ha=N(Fa)?Array(Fa.length):{};return Ga&&W(Fa,function(Ia,Ja){Ha[Ja]=Ga(Ia,Ja,Fa)}),Ha},Y=k.isPromise=function(Fa){return null!=Fa&&"function"==typeof Fa.then},Z=function(Fa){return function(Ga,Ha){var Ia=Promise.resolve();return Fa(Ga,function(Ja,Ka){Ia=Y(Ja)?Ia.then(function(){return Ja.then(function(La){return Ha(La,Ka,Ga)})}):Ia.then(function(){return Ha(Ja,Ka,Ga)})}),Ia}},$=function(Fa){return Y(Fa)?Fa:Promise.resolve(Fa)},aa=function(Fa,Ga,Ha){try{return $(Fa.apply(Ha,Ga))}catch(Ia){return Promise.reject(Ia)}},ba=function(Fa,Ga){return new Promise(function(Ha,Ia){var Ja=X(Fa,Ga),Ka=1,La=function(){if(0==--Ka){var Pa=Ja;Ja=null,Ha(Pa)}},Ma=function(Pa,Qa){Ja&&(Ja[Qa]=Pa,La())},Na=function(Pa){Ja&&(Ja=null,Ia(Pa))};W(Ga?Ja:Fa,function(Oa,Pa){++Ka,Y(Oa)?Oa.then(function(Qa){return Ma(Qa,Pa)},Na):Ma(Oa,Pa)}),La()})};k.nodeify=w;var ca=k.Cancel=function(){function Ea(){var Fa=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"this action has been canceled";q(this,Ea),Object.defineProperty(this,"message",{enumerable:!0,value:Fa})}return I(Ea,[{key:"toString",value:function(){return`Cancel: ${this._message}`}}]),Ea}(),da="CancelToken",ea="function"==typeof Symbol&&Symbol.toStringTag!==void 0||"@@toStringTag",fa=k.CancelToken=function(){function Ea(Fa){var Ga=this;q(this,Ea),this._listeners=null,this._promise=null,this._reason=void 0,this._resolve=null;var Ha=function(Ka){Ha=Q;var La=Ga._reason=Ka instanceof ca?Ka:new ca(Ka),Ma=Ga._resolve;null!==Ma&&(Ga._resolve=null,Ma(La));var Na=Ga._listeners;null!==Na&&(Ga._listeners=null,R(Na,function(Oa){Oa(La)}))};Fa(function(Ka){return Ha(Ka)})}return I(Ea,null,[{key:"isCancelToken",value:function(Ga){return null!=Ga&&"function"==typeof Ga[ea]&&Ga[ea]()===da}},{key:"source",value:function(){var Ga,Ha=new Ea(function(Ia){Ga=Ia});return{cancel:Ga,token:Ha}}},{key:"race",value:function(Ga){var Ha=Ea.source(),Ia=Ha.cancel,Ja=Ha.token;return W(Ga,function(Ka){var La=Ka.reason;return La?(Ia(La),!1):void(Ka._listeners||(Ka._listeners=[])).push(Ia)}),Ja}}]),I(Ea,[{key:"throwIfRequested",value:function(){var Ga=this._reason;if(void 0!==Ga)throw Ga}},{key:ea,value:function(){return da}},{key:"promise",get:function(){var Ga=this,Ha=this._promise;if(null===Ha){var Ia=this._reason;Ha=this._promise=Ia?Promise.resolve(Ia):new Promise(function(Ja){Ga._resolve=Ja})}return Ha}},{key:"reason",get:function(){return this._reason}},{key:"requested",get:function(){return void 0!==this._reason}}]),Ea}(),ga=k.cancellable=function(Fa,Ga,Ha){function Ia(){var Ka=arguments.length;if(0!==Ka&&fa.isCancelToken(arguments[0]))return Ja.apply(this,arguments);var La=fa.source(),Ma=La.cancel,Na=La.token,Oa=Array(Ka+1);Oa[0]=Na;for(var Pa=0;Pa<Ka;++Pa)Oa[Pa+1]=arguments[Pa];var Qa=Ja.apply(this,Oa);return Qa.cancel=Ma,Qa}var Ja=Ha===void 0?Fa:Ha.value;return void 0===Ha?Ia:(Ha.value=Ia,Ha)};k.cancelable=ga;var ha=function(Fa){return!(Fa instanceof ReferenceError||Fa instanceof SyntaxError||Fa instanceof TypeError)},ia={error:null},ja=function(Fa){try{return Fa()}catch(Ga){return ia.error=Ga,ia}},ka=function(Fa,Ga){return"function"==typeof Fa?Fa.prototype instanceof Error?Ga instanceof Fa:Fa(Ga):"object"==typeof Fa?null!=Ga&&ja(function(){return V(Fa,function(Ha,Ia){if(Ga[Ia]!==Ha)throw null})})!==ia:void 0},la=k.defer=function(){var Fa,Ga,Ha=new Promise(function(Ia,Ja){Fa=Ia,Ga=Ja});return{promise:Ha,reject:Ga,resolve:Fa}},ma=k.makeAsyncIterator=function(Fa){var Ga=Z(Fa);return function(Ha){return $(this).then(function(Ia){return Ga(Ia,Ha)}).then(Q)}},na=k.forArray=ma(R),oa=k.forEach=ma(W),pa=k.forIn=ma(S),qa=k.forIterable=ma(T),ra=k.forOwn=ma(V),sa=k.fromCallback=function(Fa){return new Promise(function(Ga,Ha){Fa(function(Ia,Ja){return Ia?Ha(Ia):Ga(Ja)})})};k.finally=B;var ta=function(){var Ea=Object.defineProperties;try{var Fa=Ea(function(){},{length:{value:2},name:{value:"foo"}});if(2===Fa.length&&"foo"===Fa.name)return function(Ga,Ha,Ia){return Ea(Ga,{length:{configurable:!0,value:Ia},name:{configurable:!0,value:Ha}})}}catch(Ga){}return function(Ga){return Ga}}(),ua=k.promisify=function(Fa,Ga){return ta(function(){for(var Ha=this,Ia=arguments.length,Ja=Array(Ia+1),Ka=0;Ka<Ia;++Ka)Ja[Ka]=arguments[Ka];return new Promise(function(La,Ma){Ja[Ia]=function(Na,Oa){return Na?Ma(Na):La(Oa)},Fa.apply(Ga||Ha,Ja)})},Fa.name,Fa.length&&Fa.length-1)},va=function(Fa){return!(K(Fa,"Sync")||K(Fa,"Async"))&&Fa},wa=k.promisifyAll=function(Fa){var Ga=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},Ha=Ga.mapper,Ia=void 0===Ha?va:Ha,Ja=Ga.target,Ka=void 0===Ja?{}:Ja,La=Ga.context,Ma=void 0===La?Fa:La;return S(Fa,function(Na,Oa){var Pa;"function"==typeof Na&&(Pa=Ia(Oa,Na,Fa))&&(Ka[Pa]=ua(Na,Ma))}),Ka},xa=function(){return!1},ya=function(){return!0},za=function(Ea){return function(Fa){return{__proto__:Ea,value:function(){return Fa}}}}({isFulfilled:ya,isPending:xa,isRejected:xa,isResolved:ya,reason:function(){throw new Error("no reason, the promise has resolved")}}),Aa=function(Ea){return function(Fa){return{__proto__:Ea,reason:function(){return Fa}}}}({isFulfilled:xa,isPending:xa,isRejected:ya,isResolved:xa,value:function(){throw new Error("no value, the promise has rejected")}}),Ba=function(Fa,Ga){return new Promise(function(Ha,Ia){var Ja=[],Ka=[],La=function(Pa){Ja&&(Ja.push(Pa),0==--Ga&&(Ha(Ja),Ja=Ka=null))},Ma=-Ga,Na=function(Pa){Ja&&(Ka.push(Pa),0==--Ma&&(Ia(Ka),Ja=Ka=null))};W(Fa,function(Oa){++Ma,$(Oa).then(La,Na)})})},Ca=k.TimeoutError=function(Ea){function Fa(){return q(this,Fa),m(this,(Fa.__proto__||Object.getPrototypeOf(Fa)).call(this,"operation timed out"))}return p(Fa,Ea),Fa}(J.BaseError),Da=k.attempt=function(Fa){return new Promise(function(Ga){return Ga(Fa())})};k.try=Da},{"make-error":2}],2:[function(h,j,k){"use strict";function m(y){y&&q(this,"message",{configurable:!0,value:y,writable:!0});var z=this.constructor.name;z&&z!==this.name&&q(this,"name",{configurable:!0,value:z,writable:!0}),v(this,this.constructor)}var q=Object.defineProperty,v=Error.captureStackTrace;v||(v=function(z){var A=new Error;q(z,"stack",{configurable:!0,get:function(){var C=A.stack;return q(this,"stack",{value:C}),C},set:function(C){q(z,"stack",{configurable:!0,value:C,writable:!0})}})}),m.prototype=Object.create(Error.prototype,{constructor:{configurable:!0,value:m,writable:!0}});var w=function(){function y(A,B){return q(A,"name",{configurable:!0,value:B})}try{var z=function(){};if(y(z,"foo"),"foo"===z.name)return y}catch(A){}}();k=j.exports=function(y,z){if(null==z||z===Error)z=m;else if("function"!=typeof z)throw new TypeError("super_ should be a function");var A;if("string"==typeof y)A=y,y=function(){z.apply(this,arguments)},w&&(w(y,A),A=null);else if("function"!=typeof y)throw new TypeError("constructor should be either a string or a function");y.super_=y["super"]=z;var B={constructor:{configurable:!0,value:y,writable:!0}};return null!=A&&(B.name={configurable:!0,value:A,writable:!0}),y.prototype=Object.create(z.prototype,B),y},k.BaseError=m},{}]},{},[1])(1)}); | ||
(function(b){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=b();else if("function"==typeof define&&define.amd)define([],b);else{var c;c="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,c.promiseToolbox=b()}})(function(){return function j(k,m,q){function v(z,A){if(!m[z]){if(!k[z]){var B="function"==typeof require&&require;if(!A&&B)return B(z,!0);if(w)return w(z,!0);var C=new Error("Cannot find module '"+z+"'");throw C.code="MODULE_NOT_FOUND",C}var D=m[z]={exports:{}};k[z][0].call(D.exports,function(E){var F=k[z][1][E];return v(F?F:E)},D,D.exports,j,k,m,q)}return m[z].exports}for(var w="function"==typeof require&&require,y=0;y<q.length;y++)v(q[y]);return v}({1:[function(j,k,m){"use strict";function q(La,Ma){if(!La)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return Ma&&("object"==typeof Ma||"function"==typeof Ma)?Ma:La}function v(La,Ma){if("function"!=typeof Ma&&null!==Ma)throw new TypeError("Super expression must either be null or a function, not "+typeof Ma);La.prototype=Object.create(Ma&&Ma.prototype,{constructor:{value:La,enumerable:!1,writable:!0,configurable:!0}}),Ma&&(Object.setPrototypeOf?Object.setPrototypeOf(La,Ma):La.__proto__=Ma)}function w(La,Ma){if(!(La instanceof Ma))throw new TypeError("Cannot call a class as a function")}function y(La){return fa(this).then(function(Ma){return ia(Ma,La)})}function z(La){return La&&this.then(function(Ma){return La(null,Ma)},function(Ma){return La(Ma)}).then(null,W),this}function C(La,Ma){this.d=Ma,this.p=La}function G(La){var Ma=this;return fa(this).then(function(){return fa(La()).then(function(){return Ma})},function(){return fa(La()).then(function(){return Ma})})}function H(){return fa(this).then(Ga,Ha)}Object.defineProperty(m,"__esModule",{value:!0}),m.try=m.attempt=m.TimeoutError=m.promisifyAll=m.promisify=m.finally=m.fromCallback=m.forOwn=m.forIterable=m.forIn=m.forEach=m.forArray=m.makeAsyncIterator=m.defer=m.cancelable=m.cancellable=m.CancelToken=m.Cancel=m.nodeify=m.isPromise=void 0;var N=function(){function La(Ma,Na){for(var Pa,Oa=0;Oa<Na.length;Oa++)Pa=Na[Oa],Pa.enumerable=Pa.enumerable||!1,Pa.configurable=!0,"value"in Pa&&(Pa.writable=!0),Object.defineProperty(Ma,Pa.key,Pa)}return function(Ma,Na,Oa){return Na&&La(Ma.prototype,Na),Oa&&La(Ma,Oa),Ma}}();m.all=y,m.asCallback=z,m.catchPlus=function(){var La=this,Ma=arguments.length-1,Na;if(0>Ma||"function"!=typeof(Na=arguments[Ma]))return this;var Oa;if(Ma){Oa=Array(Ma);for(var Pa=0;Pa<Ma;++Pa)Oa[Pa]=arguments[Pa]}return fa(this).then(null,function(Qa){if(Oa){for(var Ra=0;Ra<Ma;++Ra)if(ra(Oa[Ra],Qa))return Na(Qa);}else if(oa(Qa))return Na(Qa);return La})},m.delay=function(La){var Ma=this;return da(this)?this.then(function(Na){return new Promise(function(Oa){setTimeout(function(){return Oa(Na)},La)})}):new Promise(function(Na){setTimeout(function(){return Na(Ma)},La)})},m.disposer=function(La){return new C(this,La)},m.using=function(){var La=this,Ma=arguments.length-1;if(1>Ma)throw new TypeError("using expects at least 2 arguments");var Na=arguments[Ma],Oa=arguments[0],Pa=1<Ma||!Q(Oa);if(Pa){Oa=Array(Ma);for(var Qa=0;Qa<Ma;++Qa)Oa[Qa]=arguments[Qa]}else Ma=Oa.length;var Ra=V(function(Sa,Ta){var Ua=Ma,Va=function(){0==--Ua&&Sa(Ta)},Wa=function(Ya){setTimeout(function(){throw Ya},0)};X(Oa,function(Xa){var Ya;null!=Xa&&"function"==typeof(Ya=Xa.d)?(Xa.p.then(function(Za){return ga(Ya,Za).then(Va,Wa)},Va),Xa.p=Xa.d=null):--Ua})});return new Promise(function(Sa,Ta){var Ua=Array(Ma),Va=Ma,Wa=function(_a){Wa=Ya,Za=function(){return Ra(Ta,_a)},Ya()},Xa=function(_a){return Wa(_a)},Ya=function(){0==--Va&&Za()},Za=function(){return(Pa?ha:ga)(Na,Ua,La).then(function(_a){return Ra(Sa,_a)},function(_a){return Ra(Ta,_a)})};X(Oa,function($a,_a){var ab=$a instanceof C?$a.p:$a;return null===ab?void Xa(new TypeError("resource has already been disposed of")):void ab.then(function(bb){Ua[_a]=bb,Ya()},Xa)})})},m.join=function(){var La=arguments.length-1,Ma=arguments[La],Na;if(0==La)return new Promise(function(Pa){return Pa(Ma())});if(1!=La){Na=Array(La);for(var Oa=0;Oa<La;++Oa)Na[Oa]=arguments[Oa]}else if(!S(Na=arguments[0]))return fa(Na).then(function(Pa){return Ma(Pa)});return ia(Na).then(function(Pa){return Ma.apply(null,Pa)})},m.lastly=G,m.reflect=H,m.settle=function(){return y.call(this,function(La){return H.call(La)})},m.some=function(La){return fa(this).then(function(Ma){return Ia(Ma,La)})},m.tap=function(La,Ma){var Na=this,Oa=function(){return Na};return fa(this).then(La&&function(Pa){return fa(La(Pa)).then(Oa)},Ma&&function(Pa){return fa(Ma(Pa)).then(Oa)})},m.timeout=function(La,Ma){var Na=this;return new Promise(function(Oa,Pa){var Qa=setTimeout(function(){if(Qa=null,"function"==typeof Na.cancel&&Na.cancel(),Ma)try{Oa(Ma())}catch(Ra){Pa(Ra)}else Pa(new Ja)},La);fa(Na).then(function(Ra){null!==Qa&&clearTimeout(Qa),Oa(Ra)},function(Ra){null!==Qa&&clearTimeout(Qa),Pa(Ra)})})},m.unpromisify=function(){var La=this;return Aa(function(){var Ma=arguments.length-1,Na;if(0>Ma||"function"!=typeof(Na=arguments[Ma]))throw new Error("missing callback");for(var Oa=Array(Ma),Pa=0;Pa<Ma;++Pa)Oa[Pa]=arguments[Pa];ha(La,Oa,this).then(function(Qa){return Na(null,Qa)},function(Qa){return Na(Qa)})},La.name,La.length+1)};var O=j("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 P=function(Ma,Na){var Oa=2<arguments.length&&void 0!==arguments[2]?arguments[2]:Ma.length;return Oa-=Na.length,0<=Oa&&Ma.indexOf(Na,Oa)===Oa},Q=Array.isArray||function(La){return La instanceof Array},R=function(Ma){return"number"==typeof Ma&&0<=Ma&&Ma<Infinity&&Math.floor(Ma)===Ma},S=function(Ma){return Ma&&"function"!=typeof Ma&&R(Ma.length)},T="function"==typeof Symbol&&Symbol.iterator||"@@iterator",U=function(Ma){return Ma&&"function"==typeof Ma[T]},V=function(Ma){var Na;return function(){return Ma&&(Na=Ma.apply(this,arguments),Ma=null),Na}},W=function(){},X=function(Ma,Na){for(var Oa=Ma.length,Pa=0;Pa<Oa;++Pa)Na(Ma[Pa],Pa,Ma)},Y=function(Ma,Na){for(var Oa in Ma)Na(Ma[Oa],Oa,Ma)},Z=function(Ma,Na){for(var Pa,Oa=Ma[Symbol.iterator]();!(Pa=Oa.next()).done;)Na(Pa.value,null,Ma)},$=Object.prototype.hasOwnProperty,aa=function(Ma,Na){for(var Oa in Ma)$.call(Ma,Oa)&&Na(Ma[Oa],Oa,Ma)},ba=function(Ma,Na){return Q(Ma)?X(Ma,Na):U(Ma)?Z(Ma,Na):S(Ma)?X(Ma,Na):aa(Ma,Na)},ca=function(Ma,Na){var Oa=S(Ma)?Array(Ma.length):{};return Na&&ba(Ma,function(Pa,Qa){Oa[Qa]=Na(Pa,Qa,Ma)}),Oa},da=m.isPromise=function(Ma){return null!=Ma&&"function"==typeof Ma.then},ea=function(Ma){return function(Na,Oa){var Pa=Promise.resolve();return Ma(Na,function(Qa,Ra){Pa=da(Qa)?Pa.then(function(){return Qa.then(function(Sa){return Oa(Sa,Ra,Na)})}):Pa.then(function(){return Oa(Qa,Ra,Na)})}),Pa}},fa=function(Ma){return da(Ma)?Ma:Promise.resolve(Ma)},ga=function(Ma,Na,Oa){try{return fa(Ma.call(Oa,Na))}catch(Pa){return Promise.reject(Pa)}},ha=function(Ma,Na,Oa){try{return fa(Ma.apply(Oa,Na))}catch(Pa){return Promise.reject(Pa)}},ia=function(Ma,Na){return new Promise(function(Oa,Pa){var Qa=ca(Ma,Na),Ra=1,Sa=function(){if(0==--Ra){var Wa=Qa;Qa=null,Oa(Wa)}},Ta=function(Wa,Xa){Qa&&(Qa[Xa]=Wa,Sa())},Ua=function(Wa){Qa&&(Qa=null,Pa(Wa))};ba(Na?Qa:Ma,function(Va,Wa){++Ra,da(Va)?Va.then(function(Xa){return Ta(Xa,Wa)},Ua):Ta(Va,Wa)}),Sa()})};m.nodeify=z;var ja=m.Cancel=function(){function La(){var Ma=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"this action has been canceled";w(this,La),Object.defineProperty(this,"message",{enumerable:!0,value:Ma})}return N(La,[{key:"toString",value:function(){return`Cancel: ${this._message}`}}]),La}(),ka="CancelToken",la="function"==typeof Symbol&&Symbol.toStringTag!==void 0||"@@toStringTag",ma=m.CancelToken=function(){function La(Ma){var Na=this;w(this,La),this._listeners=null,this._promise=null,this._reason=void 0,this._resolve=null;var Oa=function(Ra){Oa=W;var Sa=Na._reason=Ra instanceof ja?Ra:new ja(Ra),Ta=Na._resolve;null!==Ta&&(Na._resolve=null,Ta(Sa));var Ua=Na._listeners;null!==Ua&&(Na._listeners=null,X(Ua,function(Va){Va(Sa)}))};Ma(function(Ra){return Oa(Ra)})}return N(La,null,[{key:"isCancelToken",value:function(Na){return null!=Na&&"function"==typeof Na[la]&&Na[la]()===ka}},{key:"source",value:function(){var Na,Oa=new La(function(Pa){Na=Pa});return{cancel:Na,token:Oa}}},{key:"race",value:function(Na){var Oa=La.source(),Pa=Oa.cancel,Qa=Oa.token;return ba(Na,function(Ra){var Sa=Ra.reason;return Sa?(Pa(Sa),!1):void(Ra._listeners||(Ra._listeners=[])).push(Pa)}),Qa}}]),N(La,[{key:"fork",value:function(Na){if(void 0!==Na)return La.race([this,new La(Na)]);var Oa=La.source(),Pa=Oa.cancel,Qa=Oa.token;return{cancel:Pa,token:La.race([this,Qa])}}},{key:"throwIfRequested",value:function(){var Na=this._reason;if(void 0!==Na)throw Na}},{key:la,value:function(){return ka}},{key:"promise",get:function(){var Na=this,Oa=this._promise;if(null===Oa){var Pa=this._reason;Oa=this._promise=Pa?Promise.resolve(Pa):new Promise(function(Qa){Na._resolve=Qa})}return Oa}},{key:"reason",get:function(){return this._reason}},{key:"requested",get:function(){return void 0!==this._reason}}]),La}(),na=m.cancellable=function(Ma,Na,Oa){function Pa(){var Ra=arguments.length;if(0!==Ra&&ma.isCancelToken(arguments[0]))return Qa.apply(this,arguments);var Sa=ma.source(),Ta=Sa.cancel,Ua=Sa.token,Va=Array(Ra+1);Va[0]=Ua;for(var Wa=0;Wa<Ra;++Wa)Va[Wa+1]=arguments[Wa];var Xa=Qa.apply(this,Va);return Xa.cancel=Ta,Xa}var Qa=Oa===void 0?Ma:Oa.value;return void 0===Oa?Pa:(Oa.value=Pa,Oa)};m.cancelable=na;var oa=function(Ma){return!(Ma instanceof ReferenceError||Ma instanceof SyntaxError||Ma instanceof TypeError)},pa={error:null},qa=function(Ma){try{return Ma()}catch(Na){return pa.error=Na,pa}},ra=function(Ma,Na){return"function"==typeof Ma?Ma.prototype instanceof Error?Na instanceof Ma:Ma(Na):"object"==typeof Ma?null!=Na&&qa(function(){return aa(Ma,function(Oa,Pa){if(Na[Pa]!==Oa)throw null})})!==pa:void 0},sa=m.defer=function(){var Ma,Na,Oa=new Promise(function(Pa,Qa){Ma=Pa,Na=Qa});return{promise:Oa,reject:Na,resolve:Ma}},ta=m.makeAsyncIterator=function(Ma){var Na=ea(Ma);return function(Oa){return fa(this).then(function(Pa){return Na(Pa,Oa)}).then(W)}},ua=m.forArray=ta(X),va=m.forEach=ta(ba),wa=m.forIn=ta(Y),xa=m.forIterable=ta(Z),ya=m.forOwn=ta(aa),za=m.fromCallback=function(Ma){return new Promise(function(Na,Oa){Ma(function(Pa,Qa){return Pa?Oa(Pa):Na(Qa)})})};m.finally=G;var Aa=function(){var La=Object.defineProperties;try{var Ma=La(function(){},{length:{value:2},name:{value:"foo"}});if(2===Ma.length&&"foo"===Ma.name)return function(Na,Oa,Pa){return La(Na,{length:{configurable:!0,value:Pa},name:{configurable:!0,value:Oa}})}}catch(Na){}return function(Na){return Na}}(),Ba=m.promisify=function(Ma,Na){return Aa(function(){for(var Oa=this,Pa=arguments.length,Qa=Array(Pa+1),Ra=0;Ra<Pa;++Ra)Qa[Ra]=arguments[Ra];return new Promise(function(Sa,Ta){Qa[Pa]=function(Ua,Va){return Ua?Ta(Ua):Sa(Va)},Ma.apply(Na||Oa,Qa)})},Ma.name,Ma.length&&Ma.length-1)},Ca=function(Ma){return!(P(Ma,"Sync")||P(Ma,"Async"))&&Ma},Da=m.promisifyAll=function(Ma){var Na=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},Oa=Na.mapper,Pa=void 0===Oa?Ca:Oa,Qa=Na.target,Ra=void 0===Qa?{}:Qa,Sa=Na.context,Ta=void 0===Sa?Ma:Sa;return Y(Ma,function(Ua,Va){var Wa;"function"==typeof Ua&&(Wa=Pa(Va,Ua,Ma))&&(Ra[Wa]=Ba(Ua,Ta))}),Ra},Ea=function(){return!1},Fa=function(){return!0},Ga=function(La){return function(Ma){return{__proto__:La,value:function(){return Ma}}}}({isFulfilled:Fa,isPending:Ea,isRejected:Ea,isResolved:Fa,reason:function(){throw new Error("no reason, the promise has resolved")}}),Ha=function(La){return function(Ma){return{__proto__:La,reason:function(){return Ma}}}}({isFulfilled:Ea,isPending:Ea,isRejected:Fa,isResolved:Ea,value:function(){throw new Error("no value, the promise has rejected")}}),Ia=function(Ma,Na){return new Promise(function(Oa,Pa){var Qa=[],Ra=[],Sa=function(Wa){Qa&&(Qa.push(Wa),0==--Na&&(Oa(Qa),Qa=Ra=null))},Ta=-Na,Ua=function(Wa){Qa&&(Ra.push(Wa),0==--Ta&&(Pa(Ra),Qa=Ra=null))};ba(Ma,function(Va){++Ta,fa(Va).then(Sa,Ua)})})},Ja=m.TimeoutError=function(La){function Ma(){return w(this,Ma),q(this,(Ma.__proto__||Object.getPrototypeOf(Ma)).call(this,"operation timed out"))}return v(Ma,La),Ma}(O.BaseError),Ka=m.attempt=function(Ma){return new Promise(function(Na){return Na(Ma())})};m.try=Ka},{"make-error":2}],2:[function(j,k,m){"use strict";function q(A){A&&w(this,"message",{configurable:!0,value:A,writable:!0});var B=this.constructor.name;B&&B!==this.name&&w(this,"name",{configurable:!0,value:B,writable:!0}),y(this,this.constructor)}var w=Object.defineProperty,y=Error.captureStackTrace;y||(y=function(B){var C=new Error;w(B,"stack",{configurable:!0,get:function(){var E=C.stack;return w(this,"stack",{value:E}),E},set:function(E){w(B,"stack",{configurable:!0,value:E,writable:!0})}})}),q.prototype=Object.create(Error.prototype,{constructor:{configurable:!0,value:q,writable:!0}});var z=function(){function A(C,D){return w(C,"name",{configurable:!0,value:D})}try{var B=function(){};if(A(B,"foo"),"foo"===B.name)return A}catch(C){}}();m=k.exports=function(A,B){if(null==B||B===Error)B=q;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)},z&&(z(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 D={constructor:{configurable:!0,value:A,writable:!0}};return null!=C&&(D.name={configurable:!0,value:C,writable:!0}),A.prototype=Object.create(B.prototype,D),A},m.BaseError=q},{}]},{},[1])(1)}); | ||
{ | ||
"name": "promise-toolbox", | ||
"version": "0.8.3", | ||
"version": "0.9.0", | ||
"license": "ISC", | ||
@@ -5,0 +5,0 @@ "description": "Essential utils for promises", |
@@ -120,2 +120,22 @@ # promise-toolbox [![Build Status](https://travis-ci.org/JsCommunity/promise-toolbox.png?branch=master)](https://travis-ci.org/JsCommunity/promise-toolbox) | ||
#### Forking cancel tokens | ||
> Create a new token which is cancelled as soon as the original token | ||
> is or as soon as the executor decides. | ||
```js | ||
// token is cancelled as soon as otherToken is or when the button is | ||
// clicked. | ||
const token = otherToken.fork(cancel => { | ||
$('#some-button').on('click', () => cancel('button clicked')) | ||
}) | ||
``` | ||
If no executor is passed, `#fork()` works like `.source()` and returns | ||
an object with a cancel function and a token: | ||
```js | ||
const { cancel, token } = otherToken.fork() | ||
``` | ||
#### @cancellable decorator | ||
@@ -162,2 +182,24 @@ | ||
### Resource management | ||
> See [Bluebird documentation](http://bluebirdjs.com/docs/api/resource-management.html) for a good explanation. | ||
```js | ||
import { disposer, using } from 'promise-toolbox' | ||
const getConnection = () => | ||
// disposer() is used to associate a disposer to a resource | ||
// | ||
// The returned resource can only be used with using() | ||
db.connect()::disposer(connection => | ||
connection.close() | ||
) | ||
using(getConnection(), getConnection(), (connection1, connection2) => { | ||
// So something with connection1 and connection2 | ||
})).then(() => { | ||
// Both connections are now closed | ||
}) | ||
``` | ||
### Functions | ||
@@ -164,0 +206,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
103739
936
568