UNPKG

@nextgis/cancelable-promise

Version:
3 lines (2 loc) 4.04 kB
"use strict";var e=Object.defineProperty,t=(t,s,i)=>((t,s,i)=>s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[s]=i)(t,s+"",i);class s extends Error{constructor(){super(),t(this,"name","CancelError"),Object.setPrototypeOf(this,s.prototype)}}var i=Object.defineProperty,r=(e,t,s)=>((e,t,s)=>t in e?i(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s)(e,t+"",s);class n{constructor(e={}){this.options=e,r(this,"_promises",new Map)}get isLoaded(){return this._promises.size>0}remove(e){this._promises.has(e)&&(this._promises.delete(e),this._onStop())}get(e){return this._promises.get(e)}add(e,t){const s=t||e,i=this._promises.get(s);return this.options.onStart&&!this.isLoaded&&this.options.onStart(),i||(this._promises.set(s,e),e.finally(()=>{this.remove(s)}),e)}abort(){this.isLoaded&&(this._promises.forEach(e=>{e.cancel&&e.cancel()}),this._promises.clear(),this._onStop())}waitFunc(e,t=""){const s=this.get(t=t||e.name);return s||this.add(e(),t)}WaitForMe(e=""){const t=this.get.bind(this),s=this.add.bind(this);return function(i,r,n){const o=n.value;return e=e||r,n.value=function(...i){const r=t(e);if(r)return r;return s(o.apply(this,i),e)},n}}GetOrCreateDecorator(e=""){return this.WaitForMe(e)}_onStop(){this.options.onStop&&!this.isLoaded&&this.options.onStop()}}var o=Object.defineProperty,a=(e,t,s)=>((e,t,s)=>t in e?o(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s)(e,t+"",s);class c extends Error{constructor(){super(),a(this,"name","TimeoutError"),Object.setPrototypeOf(this,c.prototype)}}var h,l=Object.defineProperty,d=(e,t,s)=>((e,t,s)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s)(e,"symbol"!=typeof t?t+"":t,s);const p=(e,t,s,i)=>{try{e(s(i))}catch(r){t(r)}};let _=0;h=Symbol.toStringTag;const u=class e{constructor(t,i){d(this,h),d(this,"id",_++),d(this,"_isCanceled",!1),d(this,"_isPending",!0),d(this,"_promise"),d(this,"_cancelPromise"),d(this,"_cancelHandlers",[]),d(this,"_setCanceledCallback"),d(this,"_parentPromise"),d(this,"_children",[]),this._cancelPromise=new Promise((e,t)=>{this._setCanceledCallback=t=>e(t||new s)});const r=[this._cancelPromise,new Promise((s,i)=>t(t=>{t instanceof e?this.attach(t):this._isPending=!1,s(t)},e=>{this._isPending=!1,i(e)},e=>{if(!this._isPending)throw new Error("The `onCancel` handler was attached after the promise settled.");this._cancelHandlers.push(e)}))];i&&r.push(new Promise((e,t)=>{setTimeout(()=>{if(this._isPending)try{this.cancel()}finally{t(new c)}},i)})),this._promise=Promise.race(r)}static createControl(e){return new n(e)}static resolve(t){return new e(e=>e(t))}static reject(t){return new e((e,s)=>s(t))}static all(t){return new e((e,s)=>{Promise.all(t).then(e).catch(s)}).catch(e=>{if(e instanceof this.CancelError)for(const s of t){"cancel"in s&&s.cancel()}throw e})}attach(e){this._isCanceled?e.cancel():this._children.push(e)}then(t,s){const i=new e((e,i)=>{if(this._promise){const r=t=>{s?p(e,i,s,t):i(t)};this._promise.then(s=>{this._isCanceled?r(s):t?p(e,i,t,s):e(s)},r)}});return i._parentPromise=this,this._children.push(i),i}catch(e){return this._isCanceled&&e&&e(new s),this.then(void 0,e)}finally(e){return this._promise?this._promise.finally(e):Promise.reject(this._isCanceled?new s:e)}cancel(){if(this._isCanceled||!this._isPending)return this;this._isCanceled=!0;const e=this._getTopParent();if(e&&e.cancel(),this._children&&this._children.forEach(e=>e.cancel()),this._isPending){if(this._cancelHandlers.length)try{for(const e of this._cancelHandlers)e()}catch{}this._setCanceledCallback&&this._setCanceledCallback()}return this._destroy(),this}_getTopParent(){let e=this._parentPromise,t=!!e;for(;t;)e&&e._parentPromise?(e=e._parentPromise,t=!!e):t=!1;return e}_destroy(){this._setCanceledCallback=void 0,this._cancelPromise=void 0,this._promise=void 0}};d(u,"CancelError",s),d(u,"TimeoutError",c),d(u,"PromiseControl",n);let m=u;Object.setPrototypeOf(m.prototype,Promise.prototype),module.exports=m; //# sourceMappingURL=cancelable-promise.cjs.prod.js.map