@ndriadev/futurable 2.3.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";class Futurable extends Promise{controller;internalSignal;idsTimeout;constructor(t,n){const e=new AbortController,i=e.signal,s=[];n&&(n.aborted?e.abort():n.addEventListener("abort",()=>e.abort(),{once:!0}));let c=[];const r=o=>{c.push(o)},a=()=>{for(const o of s)clearTimeout(o);s.length=0},l={signal:i,cancel:()=>e.abort(),onCancel:r,delay:(o,f)=>new Futurable(h=>{s.push(setTimeout(()=>{h(o())},f))},i),sleep:o=>l.delay(()=>{},o),fetch:(o,f)=>new Futurable((h,u)=>{fetch(o,{...f||{},signal:i}).then(g=>h(g)).catch(g=>{g.name!=="AbortError"&&u(g)})},i),futurizable:o=>new Futurable((f,h)=>{o.then(f).catch(h)},i)},d=new Promise((o,f)=>{if(i.aborted){a();return}else{const h=()=>{a(),c.forEach(g=>g()),c=[]},u=()=>{i.removeEventListener("abort",h)};i.addEventListener("abort",h,{once:!0}),t(g=>{u(),o(g)},g=>{u(),f(g)},l)}});super((o,f)=>{d.then(h=>o(h)).catch(f)}),this.controller=e,this.internalSignal=i,this.idsTimeout=s}static get[Symbol.species](){return this}get[Symbol.toStringTag](){return"Futurable"}get signal(){return this.internalSignal}clearTimeout(){for(const t of this.idsTimeout)clearTimeout(t);this.idsTimeout.length=0}then(t,n){let e,i;const s=new Futurable((c,r)=>{e=c,i=r},this.internalSignal);return s.controller=this.controller,super.then(c=>{var r;if((r=this.internalSignal)!=null&&r.aborted){this.clearTimeout();return}try{e(t?t(c):c)}catch(a){i(a)}},c=>{var r;if((r=this.internalSignal)!=null&&r.aborted){this.clearTimeout();return}try{n?e(n(c)):i(c)}catch(a){i(a)}}),s}catch(t){return this.then(null,t)}finally(t){return this.then(n=>(t(),n),n=>{if(t(),n instanceof Error)throw n;return n})}cancel(){var t;!((t=this.internalSignal)!=null&&t.aborted)&&this.controller.abort()}delay(t,n){return this.then(e=>new Futurable(i=>{this.idsTimeout.push(setTimeout(()=>i(t(e)),n))},this.internalSignal))}sleep(t){return this.delay(n=>n,t)}fetch(t,n){return this.then(e=>{const i=typeof t=="function"?t(e):t,s={...typeof n=="function"?n(e):n,signal:this.internalSignal};return new Futurable((c,r)=>{fetch(i,s).then(c).catch(a=>{a.name!=="AbortError"&&r(a)})})})}onCancel(t){const n=new Futurable((e,i,s)=>{s.onCancel(t),this.then(c=>e(c),c=>i(c))},this.internalSignal);return n.controller=this.controller,n}futurizable(t){return this.then(n=>{const e=typeof t=="function"?t(n):t;return new Futurable((i,s)=>{e.then(i).catch(s)},this.internalSignal)})}safe(){return this.then(t=>({success:!0,data:t,error:null}),t=>({success:!1,data:null,error:t}))}static resolve(t,n){return t?new Futurable(e=>e(t),n):new Futurable(e=>e(),n)}static reject(t,n){return new Futurable((e,i)=>i(t),n)}static onCancel({cb:t,signal:n}){return new Futurable((e,i,s)=>{s.onCancel(()=>e(t()))},n)}static delay({cb:t,timer:n,signal:e}){return new Futurable((i,s,c)=>{c.delay(t,n).then(i,s)},e)}static sleep({timer:t,signal:n}){return Futurable.delay({cb:()=>{},timer:t,signal:n})}static fetch(t,n){const e=(n==null?void 0:n.signal)||void 0;return n!=null&&n.signal&&delete n.signal,new Futurable((i,s,c)=>{c.fetch(t,n).then(i).catch(s)},e)}static futurizable({promise:t,signal:n}){return new Futurable((e,i)=>{t.then(e).catch(i)},n)}static handleValues(t,n){const e=[];for(const i of t)i instanceof Futurable?e.push(i):i instanceof Promise?e.push(new Futurable((s,c)=>{i.then(r=>s(r)).catch(c)},n)):e.push(new Futurable(s=>s(i),n));return e}static all(t,n){let e,i;const s=new Futurable((r,a,l)=>{e=r,i=a,l.onCancel(()=>{for(const d of c)d.cancel()})},n);n||=s.internalSignal;const c=Futurable.handleValues(t,n);return super.all(c).then(r=>e(r)).catch(r=>i(r)),s}static allSettled(t,n){let e;const i=new Futurable((c,r,a)=>{e=c,a.onCancel(()=>{for(const l of s)l.cancel()})},n);n||=i.internalSignal;const s=Futurable.handleValues(t,n);return super.allSettled(s).then(c=>e(c)),i}static race(t,n){let e,i;const s=new Futurable((r,a,l)=>{e=r,i=a,l.onCancel(()=>{for(const d of c)d.cancel()})},n);n||=s.internalSignal;const c=Futurable.handleValues(t,n);return super.race(c).then(r=>e(r)).catch(r=>i(r)),s}static any(t,n){let e,i;const s=new Futurable((r,a,l)=>{e=r,i=a,l.onCancel(()=>{for(const d of c)d.cancel()})},n);n||=s.internalSignal;const c=Futurable.handleValues(t,n);return super.any(c).then(r=>e(r)).catch(r=>i(r)),s}static polling(t,{interval:n,signal:e,immediate:i}){let s,c;const r=[];let a=o=>{r.push(o)};const l=()=>{s&&s.cancel(),s=new Futurable((o,f,h)=>{h.onCancel(()=>{c&&c instanceof Futurable&&c.cancel()});try{const u=t();u instanceof Futurable||u instanceof Promise?c=u.then(()=>o()).catch(g=>{a(g)}):o()}catch(u){a(u)}},e)};i&&l();const d=setInterval(l,n);return{cancel:()=>{d&&clearInterval(d),s&&s.cancel(),c&&c instanceof Futurable&&c.cancel()},catch:o=>{a=o,r.length>0&&(r.forEach(f=>o(f)),r.length=0)}}}static withResolvers(t){let n,e,i;const s=new Futurable((r,a,l)=>{n=r,e=a,i=l},t),c=s.cancel.bind(s);return{resolve:n,reject:e,utils:i,cancel:c,promise:s}}static safe(t,n){return new Futurable(t,n).safe()}}class FuturableTask{constructor(t,n){this.executor=t,this.controller=new AbortController,this.controller.signal.addEventListener("abort",()=>{this.cancelCallbacks.forEach(e=>e())},{once:!0}),n&&(n.aborted?this.controller.abort():n.addEventListener("abort",()=>this.controller.abort(),{once:!0}))}controller;cancelCallbacks=[];memoizeOptions={enabled:!1,catchErrors:!1};sourceTask;get signal(){return this.controller.signal}cancel(){this.controller.signal.aborted||this.controller.abort()}onCancel(t){return this.cancelCallbacks.push(t),this}run(t){if(this.memoizeOptions.enabled&&this.memoizeOptions.instance)if(this.memoizeOptions.instance.signal.aborted)delete this.memoizeOptions.instance;else return this.memoizeOptions.instance;const n=new AbortController;this.signal.aborted?n.abort():this.signal.addEventListener("abort",()=>n.abort(),{once:!0}),t&&(t.aborted?n.abort():t.addEventListener("abort",()=>n.abort(),{once:!0}));const e=new Futurable(this.executor,n.signal);return this.memoizeOptions.enabled&&(this.memoizeOptions.instance=e,this.memoizeOptions.catchErrors||e.catch(()=>{delete this.memoizeOptions.instance})),e}runSafe(t){return this.run(t).safe()}memoize(t){const n=new FuturableTask(this.executor,this.signal);return n.memoizeOptions.enabled=!0,n.memoizeOptions.catchErrors=t,n}map(t){return new FuturableTask((n,e,i)=>{this.run(i.signal).then(s=>n(t(s,i.signal))).catch(e)},this.signal)}flatMap(t){return new FuturableTask((n,e,i)=>{this.run(i.signal).then(s=>{t(s).run(i.signal).then(n).catch(e)}).catch(e)},this.signal)}andThen(t){return this.flatMap(()=>t)}tap(t){return this.map(async n=>(await t(n),n))}tapError(t){return new FuturableTask((n,e,i)=>{this.run(i.signal).then(n).catch(async s=>{try{await t(s)}catch(c){console.error("tapError callback failed: ",c)}e(s)})},this.signal)}catchError(t){return new FuturableTask((n,e,i)=>{this.run(i.signal).then(n).catch(s=>{t(s).run(i.signal).then(n).catch(e)})},this.signal)}orElse(t){return new FuturableTask((n,e,i)=>{this.run(i.signal).then(n).catch(s=>{t(s).run(i.signal).then(n).catch(e)})},this.signal)}fallbackTo(t){return this.orElse(()=>FuturableTask.resolve(t))}ifElse(t,n,e){return new FuturableTask((i,s,c)=>{this.run(c.signal).then(async r=>{(await t(r)?n(r):e(r)).run(c.signal).then(i).catch(s)}).catch(s)},this.signal)}fold(t,n){return new FuturableTask((e,i,s)=>{this.run(s.signal).then(c=>{n(c).run(s.signal).then(e).catch(i)}).catch(c=>{t(c).run(s.signal).then(e).catch(i)})},this.signal)}finally(t){return new FuturableTask((n,e,i)=>{this.run(i.signal).then(async s=>{await t(),n(s)}).catch(async s=>{await t(),e(s)})},this.signal)}timeout(t,n="TimeoutExceeded"){return new FuturableTask((e,i,s)=>{const c=setTimeout(()=>{r.cancel(),i(n)},t),r=this.run(s.signal);s.onCancel(()=>{clearTimeout(c)}),r.then(a=>{clearTimeout(c),e(a)}).catch(a=>{clearTimeout(c),i(a)})},this.signal)}delay(t){return new FuturableTask(async(n,e,i)=>{await i.sleep(t),this.run(i.signal).then(n).catch(e)},this.signal)}retry(t,n=0){return new FuturableTask(async(e,i,s)=>{let c;for(let r=0;r<=t;r++){if(s.signal.aborted)return;try{return e(await this.run(s.signal))}catch(a){c=a,r<t&&!s.signal.aborted&&n>0&&await s.sleep(n)}}i(c)},this.signal)}debounce(t){const n=this.sourceTask??this;let e=null;const i=new FuturableTask((s,c,r)=>{r.onCancel(()=>{e&&clearTimeout(e),e=null}),e&&clearTimeout(e),e=setTimeout(()=>{n.run(r.signal).then(s).catch(c).finally(()=>{e=null})},t)},this.signal);return i.sourceTask=n,i}throttle(t){let n=0,e;return new FuturableTask((i,s,c)=>{const r=Date.now();r-n>=t||!e?(n=r,e=this.run(c.signal),e.then(i).catch(s)):e&&e.then(i).catch(s)},this.signal)}zip(t){return new FuturableTask((n,e,i)=>{Futurable.all([this.run(i.signal),t.run(i.signal)],i.signal).then(([s,c])=>n([s,c])).catch(e)},this.signal)}zipWith(t,n){return this.zip(t).map(([e,i])=>n(e,i))}bimap(t,n){return new FuturableTask((e,i,s)=>{this.run(s.signal).then(c=>e(t(c))).catch(c=>i(n(c)))},this.signal)}repeat(t){return FuturableTask.times(t,()=>this)}pipe(...t){return t.reduce((n,e)=>e(n),this)}fetch(t,n){return new FuturableTask((e,i,s)=>{this.run(s.signal).then(c=>{const r=typeof t=="function"?t(c):t,a={...typeof n=="function"?n(c):n,signal:s.signal};s.fetch(r,a).then(e).catch(i)}).catch(i)},this.signal)}static of(t,n){return typeof t=="function"?new FuturableTask(async(e,i,s)=>{try{e(await t(s))}catch(c){i(c)}},n):new FuturableTask(e=>e(t),n)}static resolve(t,n){return FuturableTask.of(t,n)}static reject(t,n){return new FuturableTask((e,i)=>i(t),n)}static all(t,n){return new FuturableTask((e,i,s)=>{Futurable.all(t.map(c=>c.run(s.signal)),s.signal).then(e).catch(i)},n)}static allSettled(t,n){return new FuturableTask((e,i,s)=>{Futurable.allSettled(t.map(c=>c.run(s.signal)),s.signal).then(e).catch(i)},n)}static race(t,n){return new FuturableTask((e,i,s)=>{Futurable.race(t.map(c=>c.run(s.signal)),s.signal).then(e).catch(i)},n)}static any(t,n){return new FuturableTask((e,i,s)=>{Futurable.any(t.map(c=>c.run(s.signal)),s.signal).then(e).catch(i)},n)}static delay(t,n){return new FuturableTask((e,i,s)=>{s.sleep(t).then(e).catch(i)},n)}static fromEvent(t,n,e,i){return new FuturableTask((s,c,r)=>{r.onCancel(()=>t.removeEventListener(n,a));const a=l=>{!(e!=null&&e.once)&&t.removeEventListener(n,a),s(l)};t.addEventListener(n,a,e)},i)}static sequence(t,n){return new FuturableTask(async(e,i,s)=>{const c=[];try{for(const r of t){if(s.signal.aborted)return;c.push(await r.run(s.signal))}e(c)}catch(r){i(r)}},n)}static parallel(t,n=5,e){return new FuturableTask(async(i,s,c)=>{const r=new Array(t.length),a=[];let l=0,d=0,o=0;c.onCancel(()=>{a.forEach(h=>h.cancel())});const f=(h,u)=>{if(d===t.length)return h(r);for(;l<n&&o<t.length;){const g=o++;l++;const w=t[g].run(c.signal);a.push(w),w.then(p=>{r[g]=p,l--,d++;const m=a.indexOf(w);m!==-1&&a.splice(m,1),f(h,u)}).catch(p=>{a.forEach(m=>m.cancel()),u(p)})}};f(i,s)},e)}static createLimiter(t,n={},e){let i=0;const s=[],c=()=>{for(var a;i<t&&s.length>0;){const l=s.shift();l&&l()}i===0&&s.length===0&&((a=n.onIdle)==null||a.call(n))},r=a=>new FuturableTask((l,d,o)=>{let f=!1;const h=()=>{var u;f=!1,i++,(u=n.onActive)==null||u.call(n,a),a.run(o.signal).then(g=>{var w;(w=n.onCompleted)==null||w.call(n,g),l(g)}).catch(g=>{var w;(w=n.onError)==null||w.call(n,g),d(g)}).finally(()=>{i--,c()})};o.onCancel(()=>{if(f){const u=s.indexOf(h);u!==-1&&s.splice(u,1),c()}}),i<t?h():(f=!0,s.push(h))},e);return Object.defineProperties(r,{activeCount:{get:()=>i},pendingCount:{get:()=>s.length},concurrency:{get:()=>t}}),r}static compose(t,...n){return n.reduce((e,i)=>i(e),t)}static filter(t,n,e){return new FuturableTask(async(i,s,c)=>{const r=[];try{for(const a of t){if(c.signal.aborted)return;const l=await a.run(c.signal);await n(l)&&r.push(l)}i(r)}catch(a){s(a)}},e)}static reduce(t,n,e,i){return new FuturableTask(async(s,c,r)=>{let a=e;try{for(let l=0;l<t.length;l++){if(r.signal.aborted)return;const d=await t[l].run(r.signal);a=await n(a,d,l)}s(a)}catch(l){c(l)}},i)}static whilst(t,n,e){return new FuturableTask(async(i,s,c)=>{const r=[];try{for(;await t();){if(c.signal.aborted)return;r.push(await n.run(c.signal))}i(r)}catch(a){s(a)}},e)}static until(t,n,e){return FuturableTask.whilst(async()=>!await t(),n,e)}static times(t,n,e){return new FuturableTask(async(i,s,c)=>{const r=[];try{for(let a=0;a<t;a++){if(c.signal.aborted)return;r.push(await n(a).run(c.signal))}i(r)}catch(a){s(a)}},e)}static traverse(t,n,e){return FuturableTask.sequence(t.map((i,s)=>n(i,s)),e)}static fetch(t,n,e){return new FuturableTask((i,s,c)=>{const r={...n,signal:c.signal};c.fetch(t,r).then(i).catch(s)},e)}}exports.Futurable=Futurable;exports.FuturableTask=FuturableTask;