@ndriadev/futurable 2.0.10 → 2.1.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/README.md CHANGED
@@ -18,7 +18,6 @@
18
18
 
19
19
 
20
20
  # Summary
21
- - [ToDo](#TODO)
22
21
  - [Introduction](#introduction)
23
22
  - [Installation](#Installation)
24
23
  - [Usage](#Usage)
@@ -41,14 +40,10 @@
41
40
  - [Futurable.allSettled](#futurableallsettledvalues-t-signal-abortsignal)
42
41
  - [Futurable.any](#futurableanyvalues-t-signal-abortsignal)
43
42
  - [Futurable.race](#futurableracevalues-t-signal-abortsignal)
43
+ - [ToDo](#TODO)
44
44
  - [License](#License)
45
45
 
46
46
 
47
- # ToDo
48
- - Think about the possibility of making a static method that returns an object with the futurable, resolve, reject, utils properties inside to be used as done for usePromiser.
49
- - Extends fetch api support to third library like axios.
50
-
51
-
52
47
  # Introduction
53
48
  Futurable is a library that extends Javascript's Promise and Fetch APIs, adding a number of useful features and with support for Typescirpt. It can be used on both browser and node.
54
49
 
@@ -593,6 +588,12 @@ f.cancel();
593
588
  ```
594
589
 
595
590
 
591
+ # ToDo
592
+ - Think about the possibility of making a static method that returns an object with the futurable, resolve, reject, utils properties inside to be used as done for usePromiser.
593
+ - Extends fetch api support to third library like axios.
594
+ - Implement promise cache.
595
+
596
+
596
597
  ## License
597
598
 
598
599
 
@@ -1 +1 @@
1
- "use strict";var T=Object.defineProperty;var C=(w,g,t)=>g in w?T(w,g,{enumerable:!0,configurable:!0,writable:!0,value:t}):w[g]=t;var S=(w,g,t)=>(C(w,typeof g!="symbol"?g+"":g,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class l extends Promise{constructor(t,e){const r=e?null:new AbortController,n=e||r.signal,o=[],c=()=>{for(const s of o)clearTimeout(s)};let i;const a={signal:n,cancel:()=>{var s;return(s=this.controller)==null?void 0:s.abort()},onCancel:s=>{i=s},delay:(s,u)=>new l(d=>{o.push(setTimeout(()=>{d(s())},u))},n),sleep:s=>a.delay(()=>{},s),fetch:(s,u)=>new l((d,j)=>{fetch(s,{...u||{},signal:n}).then(y=>d(y)).catch(y=>{y.name!=="AbortError"&&j(y)})},n),futurizable:s=>new l((u,d)=>{s.then(u).catch(d)},n)};let f="pending";const p=new Promise((s,u)=>{if(n.aborted){c(),f==="pending"&&i&&i();return}else{const d=typeof n.onabort=="function"?n.onabort:()=>{};n.onabort=()=>{d(),c(),f==="pending"&&i&&i()},t(m=>{f="fulfilled",s(m)},m=>{f="rejected",u(m)},a)}});super((s,u)=>{p.then(d=>s(d)).catch(u)});S(this,"controller");S(this,"internalSignal");S(this,"idsTimeout");this.controller=r,this.internalSignal=n,this.idsTimeout=o}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)}then(t,e){let r,n;const o=new l((c,i)=>{r=c,n=i},this.internalSignal);return o.controller=this.controller,super.then(c=>{var i;if((i=this.internalSignal)!=null&&i.aborted){this.clearTimeout();return}try{r(t?t(c):c)}catch(h){n(h)}},c=>{var i;if((i=this.internalSignal)!=null&&i.aborted){this.clearTimeout();return}try{e?r(e(c)):n(c)}catch(h){n(h)}}),o}catch(t){return this.then(null,t)}finally(t){return this.then(e=>(t(),e),e=>{if(t(),e instanceof Error)throw e;return e})}cancel(){var t,e;!((t=this.internalSignal)!=null&&t.aborted)&&((e=this.controller)==null||e.abort())}delay(t,e){let r,n;const o=new l((c,i)=>{r=c,n=i},this.internalSignal);return o.controller=this.controller,this.then(c=>{this.idsTimeout.push(setTimeout(()=>r(t(c)),e))},c=>{n(c)}),o}sleep(t){return this.delay(e=>e,t)}fetch(t,e){let r,n;const o=new l((c,i)=>{r=c,n=i},this.internalSignal);return o.controller=this.controller,this.then(c=>{const i=typeof t=="function"?t(c):t,h={...typeof e=="function"?e(c):e,signal:this.internalSignal};fetch(i,h).then(a=>r(a)).catch(a=>{a.name!=="AbortError"&&n(a)})}),o}onCancel(t){let e,r;const n=new l((o,c,i)=>{i.onCancel(t),e=o,r=c},this.internalSignal);return n.controller=this.controller,this.then(o=>e(o),o=>r(o)),n}futurizable(t){let e,r;const n=new l((o,c)=>{e=o,r=c},this.internalSignal);return n.controller=this.controller,this.then(o=>{(typeof t=="function"?t(o):t).then(e).catch(r)}),n}static resolve(t,e){return t?new l(r=>r(t),e):new l(r=>r(),e)}static reject(t,e){return new l((r,n)=>n(t),e)}static onCancel({cb:t,signal:e}){return new l((r,n,o)=>{o.onCancel(()=>r(t()))},e)}static delay({cb:t,timer:e,signal:r}){return new l((n,o,c)=>{c.delay(t,e).then(n,o)},r)}static sleep({timer:t,signal:e}){return l.delay({cb:()=>{},timer:t,signal:e})}static fetch(t,e){const r=(e==null?void 0:e.signal)||void 0;return e!=null&&e.signal&&delete e.signal,new l((n,o,c)=>{c.fetch(t,e).then(n)},r)}static futurizable({promise:t,signal:e}){return new l((r,n)=>{t.then(r).catch(n)},e)}static handleValues(t,e){const r=[];for(const n in t)t[n]instanceof l?r.push(t[n]):t[n]instanceof Promise?r.push(new l((o,c)=>{t[n].then(i=>o(i)).catch(c)},e)):r.push(new l(o=>o(t[n]),e));return r}static all(t,e){let r,n;const o=new l((i,h,a)=>{r=i,n=h,a.onCancel(()=>{for(const f of c)f.cancel()})},e);e||(e=o.internalSignal);const c=l.handleValues(t,e);return super.all(c).then(i=>r(i)).catch(i=>n(i)),o}static allSettled(t,e){let r;const n=new l((c,i,h)=>{r=c,h.onCancel(()=>{for(const a of o)a.cancel()})},e);e||(e=n.internalSignal);const o=l.handleValues(t,e);return super.allSettled(o).then(c=>r(c)),n}static race(t,e){let r,n;const o=new l((i,h,a)=>{r=i,n=h,a.onCancel(()=>{for(const f of c)f.cancel()})},e);e||(e=o.internalSignal);const c=l.handleValues(t,e);return super.race(c).then(i=>r(i)).catch(i=>n(i)),o}static any(t,e){let r,n;const o=new l((i,h,a)=>{r=i,n=h,a.onCancel(()=>{for(const f of c)f.cancel()})},e);e||(e=o.internalSignal);const c=l.handleValues(t,e);return super.any(c).then(i=>r(i)).catch(i=>n(i)),o}}exports.Futurable=l;
1
+ "use strict";var T=Object.defineProperty;var C=(w,g,t)=>g in w?T(w,g,{enumerable:!0,configurable:!0,writable:!0,value:t}):w[g]=t;var S=(w,g,t)=>(C(w,typeof g!="symbol"?g+"":g,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class i extends Promise{constructor(t,e){const r=e?null:new AbortController,n=e||r.signal,o=[],c=()=>{for(const a of o)clearTimeout(a)};let l;const h={signal:n,cancel:()=>{var a;return(a=this.controller)==null?void 0:a.abort()},onCancel:a=>{l=a},delay:(a,u)=>new i(d=>{o.push(setTimeout(()=>{d(a())},u))},n),sleep:a=>h.delay(()=>{},a),fetch:(a,u)=>new i((d,p)=>{fetch(a,{...u||{},signal:n}).then(y=>d(y)).catch(y=>{y.name!=="AbortError"&&p(y)})},n),futurizable:a=>new i((u,d)=>{a.then(u).catch(d)},n)};let f="pending";const m=new Promise((a,u)=>{if(n.aborted){c(),f==="pending"&&l&&l();return}else{const d=typeof n.onabort=="function"?n.onabort:()=>{};n.onabort=()=>{d(),c(),f==="pending"&&l&&l()},t(j=>{f="fulfilled",a(j)},j=>{f="rejected",u(j)},h)}});super((a,u)=>{m.then(d=>a(d)).catch(u)});S(this,"controller");S(this,"internalSignal");S(this,"idsTimeout");this.controller=r,this.internalSignal=n,this.idsTimeout=o}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)}then(t,e){let r,n;const o=new i((c,l)=>{r=c,n=l},this.internalSignal);return o.controller=this.controller,super.then(c=>{var l;if((l=this.internalSignal)!=null&&l.aborted){this.clearTimeout();return}try{r(t?t(c):c)}catch(s){n(s)}},c=>{var l;if((l=this.internalSignal)!=null&&l.aborted){this.clearTimeout();return}try{e?r(e(c)):n(c)}catch(s){n(s)}}),o}catch(t){return this.then(null,t)}finally(t){return this.then(e=>(t(),e),e=>{if(t(),e instanceof Error)throw e;return e})}cancel(){var t,e;!((t=this.internalSignal)!=null&&t.aborted)&&((e=this.controller)==null||e.abort())}delay(t,e){let r,n;const o=new i((c,l)=>{r=c,n=l},this.internalSignal);return o.controller=this.controller,this.then(c=>{this.idsTimeout.push(setTimeout(()=>r(t(c)),e))},c=>{n(c)}),o}sleep(t){return this.delay(e=>e,t)}fetch(t,e){let r,n;const o=new i((c,l)=>{r=c,n=l},this.internalSignal);return o.controller=this.controller,this.then(c=>{const l=typeof t=="function"?t(c):t,s={...typeof e=="function"?e(c):e,signal:this.internalSignal};fetch(l,s).then(h=>r(h)).catch(h=>{h.name!=="AbortError"&&n(h)})}),o}onCancel(t){let e,r;const n=new i((o,c,l)=>{l.onCancel(t),e=o,r=c},this.internalSignal);return n.controller=this.controller,this.then(o=>e(o),o=>r(o)),n}futurizable(t){let e,r;const n=new i((o,c)=>{e=o,r=c},this.internalSignal);return n.controller=this.controller,this.then(o=>{(typeof t=="function"?t(o):t).then(e).catch(r)}),n}static resolve(t,e){return t?new i(r=>r(t),e):new i(r=>r(),e)}static reject(t,e){return new i((r,n)=>n(t),e)}static onCancel({cb:t,signal:e}){return new i((r,n,o)=>{o.onCancel(()=>r(t()))},e)}static delay({cb:t,timer:e,signal:r}){return new i((n,o,c)=>{c.delay(t,e).then(n,o)},r)}static sleep({timer:t,signal:e}){return i.delay({cb:()=>{},timer:t,signal:e})}static fetch(t,e){const r=(e==null?void 0:e.signal)||void 0;return e!=null&&e.signal&&delete e.signal,new i((n,o,c)=>{c.fetch(t,e).then(n)},r)}static futurizable({promise:t,signal:e}){return new i((r,n)=>{t.then(r).catch(n)},e)}static handleValues(t,e){const r=[];for(const n in t)t[n]instanceof i?r.push(t[n]):t[n]instanceof Promise?r.push(new i((o,c)=>{t[n].then(l=>o(l)).catch(c)},e)):r.push(new i(o=>o(t[n]),e));return r}static all(t,e){let r,n;const o=new i((l,s,h)=>{r=l,n=s,h.onCancel(()=>{for(const f of c)f.cancel()})},e);e||(e=o.internalSignal);const c=i.handleValues(t,e);return super.all(c).then(l=>r(l)).catch(l=>n(l)),o}static allSettled(t,e){let r;const n=new i((c,l,s)=>{r=c,s.onCancel(()=>{for(const h of o)h.cancel()})},e);e||(e=n.internalSignal);const o=i.handleValues(t,e);return super.allSettled(o).then(c=>r(c)),n}static race(t,e){let r,n;const o=new i((l,s,h)=>{r=l,n=s,h.onCancel(()=>{for(const f of c)f.cancel()})},e);e||(e=o.internalSignal);const c=i.handleValues(t,e);return super.race(c).then(l=>r(l)).catch(l=>n(l)),o}static any(t,e){let r,n;const o=new i((l,s,h)=>{r=l,n=s,h.onCancel(()=>{for(const f of c)f.cancel()})},e);e||(e=o.internalSignal);const c=i.handleValues(t,e);return super.any(c).then(l=>r(l)).catch(l=>n(l)),o}static polling(t,{interval:e,signal:r}){let n,o,c;const l=setInterval(()=>{n&&n.cancel(),n=new i((s,h,f)=>{f.onCancel(()=>{o&&o.cancel()}),o=t().then(()=>s()).catch(m=>c&&c(m))},r)},e);return{cancel:()=>{l&&clearInterval(l),n&&n.cancel()},catch:s=>{c=s}}}}exports.Futurable=i;
@@ -1,57 +1,57 @@
1
1
  var T = Object.defineProperty;
2
2
  var C = (g, w, t) => w in g ? T(g, w, { enumerable: !0, configurable: !0, writable: !0, value: t }) : g[w] = t;
3
3
  var S = (g, w, t) => (C(g, typeof w != "symbol" ? w + "" : w, t), t);
4
- class l extends Promise {
4
+ class i extends Promise {
5
5
  constructor(t, e) {
6
6
  const r = e ? null : new AbortController(), n = e || r.signal, o = [], c = () => {
7
- for (const s of o)
8
- clearTimeout(s);
7
+ for (const a of o)
8
+ clearTimeout(a);
9
9
  };
10
- let i;
11
- const a = {
10
+ let l;
11
+ const h = {
12
12
  signal: n,
13
13
  cancel: () => {
14
- var s;
15
- return (s = this.controller) == null ? void 0 : s.abort();
14
+ var a;
15
+ return (a = this.controller) == null ? void 0 : a.abort();
16
16
  },
17
- onCancel: (s) => {
18
- i = s;
17
+ onCancel: (a) => {
18
+ l = a;
19
19
  },
20
- delay: (s, u) => new l((d) => {
20
+ delay: (a, u) => new i((d) => {
21
21
  o.push(setTimeout(() => {
22
- d(s());
22
+ d(a());
23
23
  }, u));
24
24
  }, n),
25
- sleep: (s) => a.delay(() => {
26
- }, s),
27
- fetch: (s, u) => new l((d, p) => {
28
- fetch(s, { ...u || {}, signal: n }).then((y) => d(y)).catch((y) => {
29
- y.name !== "AbortError" && p(y);
25
+ sleep: (a) => h.delay(() => {
26
+ }, a),
27
+ fetch: (a, u) => new i((d, j) => {
28
+ fetch(a, { ...u || {}, signal: n }).then((y) => d(y)).catch((y) => {
29
+ y.name !== "AbortError" && j(y);
30
30
  });
31
31
  }, n),
32
- futurizable: (s) => new l((u, d) => {
33
- s.then(u).catch(d);
32
+ futurizable: (a) => new i((u, d) => {
33
+ a.then(u).catch(d);
34
34
  }, n)
35
35
  };
36
36
  let f = "pending";
37
- const j = new Promise((s, u) => {
37
+ const m = new Promise((a, u) => {
38
38
  if (n.aborted) {
39
- c(), f === "pending" && i && i();
39
+ c(), f === "pending" && l && l();
40
40
  return;
41
41
  } else {
42
42
  const d = typeof n.onabort == "function" ? n.onabort : () => {
43
43
  };
44
44
  n.onabort = () => {
45
- d(), c(), f === "pending" && i && i();
46
- }, t((m) => {
47
- f = "fulfilled", s(m);
48
- }, (m) => {
49
- f = "rejected", u(m);
50
- }, a);
45
+ d(), c(), f === "pending" && l && l();
46
+ }, t((p) => {
47
+ f = "fulfilled", a(p);
48
+ }, (p) => {
49
+ f = "rejected", u(p);
50
+ }, h);
51
51
  }
52
52
  });
53
- super((s, u) => {
54
- j.then((d) => s(d)).catch(u);
53
+ super((a, u) => {
54
+ m.then((d) => a(d)).catch(u);
55
55
  });
56
56
  S(this, "controller");
57
57
  S(this, "internalSignal");
@@ -79,30 +79,30 @@ class l extends Promise {
79
79
  */
80
80
  then(t, e) {
81
81
  let r, n;
82
- const o = new l((c, i) => {
83
- r = c, n = i;
82
+ const o = new i((c, l) => {
83
+ r = c, n = l;
84
84
  }, this.internalSignal);
85
85
  return o.controller = this.controller, super.then((c) => {
86
- var i;
87
- if ((i = this.internalSignal) != null && i.aborted) {
86
+ var l;
87
+ if ((l = this.internalSignal) != null && l.aborted) {
88
88
  this.clearTimeout();
89
89
  return;
90
90
  }
91
91
  try {
92
92
  r(t ? t(c) : c);
93
- } catch (h) {
94
- n(h);
93
+ } catch (s) {
94
+ n(s);
95
95
  }
96
96
  }, (c) => {
97
- var i;
98
- if ((i = this.internalSignal) != null && i.aborted) {
97
+ var l;
98
+ if ((l = this.internalSignal) != null && l.aborted) {
99
99
  this.clearTimeout();
100
100
  return;
101
101
  }
102
102
  try {
103
103
  e ? r(e(c)) : n(c);
104
- } catch (h) {
105
- n(h);
104
+ } catch (s) {
105
+ n(s);
106
106
  }
107
107
  }), o;
108
108
  }
@@ -140,8 +140,8 @@ class l extends Promise {
140
140
  */
141
141
  delay(t, e) {
142
142
  let r, n;
143
- const o = new l((c, i) => {
144
- r = c, n = i;
143
+ const o = new i((c, l) => {
144
+ r = c, n = l;
145
145
  }, this.internalSignal);
146
146
  return o.controller = this.controller, this.then(
147
147
  (c) => {
@@ -166,13 +166,13 @@ class l extends Promise {
166
166
  */
167
167
  fetch(t, e) {
168
168
  let r, n;
169
- const o = new l((c, i) => {
170
- r = c, n = i;
169
+ const o = new i((c, l) => {
170
+ r = c, n = l;
171
171
  }, this.internalSignal);
172
172
  return o.controller = this.controller, this.then((c) => {
173
- const i = typeof t == "function" ? t(c) : t, h = { ...typeof e == "function" ? e(c) : e, signal: this.internalSignal };
174
- fetch(i, h).then((a) => r(a)).catch((a) => {
175
- a.name !== "AbortError" && n(a);
173
+ const l = typeof t == "function" ? t(c) : t, s = { ...typeof e == "function" ? e(c) : e, signal: this.internalSignal };
174
+ fetch(l, s).then((h) => r(h)).catch((h) => {
175
+ h.name !== "AbortError" && n(h);
176
176
  });
177
177
  }), o;
178
178
  }
@@ -182,8 +182,8 @@ class l extends Promise {
182
182
  */
183
183
  onCancel(t) {
184
184
  let e, r;
185
- const n = new l((o, c, i) => {
186
- i.onCancel(t), e = o, r = c;
185
+ const n = new i((o, c, l) => {
186
+ l.onCancel(t), e = o, r = c;
187
187
  }, this.internalSignal);
188
188
  return n.controller = this.controller, this.then(
189
189
  (o) => e(o),
@@ -209,7 +209,7 @@ class l extends Promise {
209
209
  */
210
210
  futurizable(t) {
211
211
  let e, r;
212
- const n = new l((o, c) => {
212
+ const n = new i((o, c) => {
213
213
  e = o, r = c;
214
214
  }, this.internalSignal);
215
215
  return n.controller = this.controller, this.then((o) => {
@@ -217,16 +217,16 @@ class l extends Promise {
217
217
  }), n;
218
218
  }
219
219
  static resolve(t, e) {
220
- return t ? new l((r) => r(t), e) : new l((r) => r(), e);
220
+ return t ? new i((r) => r(t), e) : new i((r) => r(), e);
221
221
  }
222
222
  static reject(t, e) {
223
- return new l((r, n) => n(t), e);
223
+ return new i((r, n) => n(t), e);
224
224
  }
225
225
  /**
226
226
  * OnCancel static method. It accepts a callback or a object with cb property and an optional signal.
227
227
  */
228
228
  static onCancel({ cb: t, signal: e }) {
229
- return new l((r, n, o) => {
229
+ return new i((r, n, o) => {
230
230
  o.onCancel(() => r(t()));
231
231
  }, e);
232
232
  }
@@ -234,7 +234,7 @@ class l extends Promise {
234
234
  * Delay static method. It accepts a object with timer and cb properties and an optional signal property.
235
235
  */
236
236
  static delay({ cb: t, timer: e, signal: r }) {
237
- return new l((n, o, c) => {
237
+ return new i((n, o, c) => {
238
238
  c.delay(t, e).then(n, o);
239
239
  }, r);
240
240
  }
@@ -242,7 +242,7 @@ class l extends Promise {
242
242
  * Sleep static method. It accepts a timer or a object with timer property and an optional signal.
243
243
  */
244
244
  static sleep({ timer: t, signal: e }) {
245
- return l.delay({
245
+ return i.delay({
246
246
  cb: () => {
247
247
  },
248
248
  timer: t,
@@ -254,7 +254,7 @@ class l extends Promise {
254
254
  */
255
255
  static fetch(t, e) {
256
256
  const r = (e == null ? void 0 : e.signal) || void 0;
257
- return e != null && e.signal && delete e.signal, new l((n, o, c) => {
257
+ return e != null && e.signal && delete e.signal, new i((n, o, c) => {
258
258
  c.fetch(t, e).then(n);
259
259
  }, r);
260
260
  }
@@ -262,22 +262,22 @@ class l extends Promise {
262
262
  * Futurizable static method.
263
263
  */
264
264
  static futurizable({ promise: t, signal: e }) {
265
- return new l((r, n) => {
265
+ return new i((r, n) => {
266
266
  t.then(r).catch(n);
267
267
  }, e);
268
268
  }
269
269
  static handleValues(t, e) {
270
270
  const r = [];
271
271
  for (const n in t)
272
- t[n] instanceof l ? r.push(t[n]) : t[n] instanceof Promise ? r.push(
273
- new l(
272
+ t[n] instanceof i ? r.push(t[n]) : t[n] instanceof Promise ? r.push(
273
+ new i(
274
274
  (o, c) => {
275
- t[n].then((i) => o(i)).catch(c);
275
+ t[n].then((l) => o(l)).catch(c);
276
276
  },
277
277
  e
278
278
  )
279
279
  ) : r.push(
280
- new l(
280
+ new i(
281
281
  (o) => o(t[n]),
282
282
  e
283
283
  )
@@ -289,29 +289,29 @@ class l extends Promise {
289
289
  */
290
290
  static all(t, e) {
291
291
  let r, n;
292
- const o = new l((i, h, a) => {
293
- r = i, n = h, a.onCancel(() => {
292
+ const o = new i((l, s, h) => {
293
+ r = l, n = s, h.onCancel(() => {
294
294
  for (const f of c)
295
295
  f.cancel();
296
296
  });
297
297
  }, e);
298
298
  e || (e = o.internalSignal);
299
- const c = l.handleValues(t, e);
300
- return super.all(c).then((i) => r(i)).catch((i) => n(i)), o;
299
+ const c = i.handleValues(t, e);
300
+ return super.all(c).then((l) => r(l)).catch((l) => n(l)), o;
301
301
  }
302
302
  /**
303
303
  * Creates a Futurable with cancellation support that is resolved with an array of results when all of the provided Futurables resolve or reject.
304
304
  */
305
305
  static allSettled(t, e) {
306
306
  let r;
307
- const n = new l((c, i, h) => {
308
- r = c, h.onCancel(() => {
309
- for (const a of o)
310
- a.cancel();
307
+ const n = new i((c, l, s) => {
308
+ r = c, s.onCancel(() => {
309
+ for (const h of o)
310
+ h.cancel();
311
311
  });
312
312
  }, e);
313
313
  e || (e = n.internalSignal);
314
- const o = l.handleValues(t, e);
314
+ const o = i.handleValues(t, e);
315
315
  return super.allSettled(o).then((c) => r(c)), n;
316
316
  }
317
317
  /**
@@ -319,15 +319,15 @@ class l extends Promise {
319
319
  */
320
320
  static race(t, e) {
321
321
  let r, n;
322
- const o = new l((i, h, a) => {
323
- r = i, n = h, a.onCancel(() => {
322
+ const o = new i((l, s, h) => {
323
+ r = l, n = s, h.onCancel(() => {
324
324
  for (const f of c)
325
325
  f.cancel();
326
326
  });
327
327
  }, e);
328
328
  e || (e = o.internalSignal);
329
- const c = l.handleValues(t, e);
330
- return super.race(c).then((i) => r(i)).catch((i) => n(i)), o;
329
+ const c = i.handleValues(t, e);
330
+ return super.race(c).then((l) => r(l)).catch((l) => n(l)), o;
331
331
  }
332
332
  /**
333
333
  * The any function returns a futurable with cancellation support that is fulfilled by the first given futurable to be fulfilled,
@@ -337,17 +337,38 @@ class l extends Promise {
337
337
  */
338
338
  static any(t, e) {
339
339
  let r, n;
340
- const o = new l((i, h, a) => {
341
- r = i, n = h, a.onCancel(() => {
340
+ const o = new i((l, s, h) => {
341
+ r = l, n = s, h.onCancel(() => {
342
342
  for (const f of c)
343
343
  f.cancel();
344
344
  });
345
345
  }, e);
346
346
  e || (e = o.internalSignal);
347
- const c = l.handleValues(t, e);
348
- return super.any(c).then((i) => r(i)).catch((i) => n(i)), o;
347
+ const c = i.handleValues(t, e);
348
+ return super.any(c).then((l) => r(l)).catch((l) => n(l)), o;
349
+ }
350
+ /**
351
+ * Creates a polling service with cancellation support and possibility to handle error.
352
+ */
353
+ static polling(t, { interval: e, signal: r }) {
354
+ let n, o, c;
355
+ const l = setInterval(() => {
356
+ n && n.cancel(), n = new i((s, h, f) => {
357
+ f.onCancel(() => {
358
+ o && o.cancel();
359
+ }), o = t().then(() => s()).catch((m) => c && c(m));
360
+ }, r);
361
+ }, e);
362
+ return {
363
+ cancel: () => {
364
+ l && clearInterval(l), n && n.cancel();
365
+ },
366
+ catch: (s) => {
367
+ c = s;
368
+ }
369
+ };
349
370
  }
350
371
  }
351
372
  export {
352
- l as Futurable
373
+ i as Futurable
353
374
  };
package/dist/index.d.ts CHANGED
@@ -172,4 +172,15 @@ export declare class Futurable<T> extends Promise<T> {
172
172
  * it runs this algorithm.
173
173
  */
174
174
  static any<T extends readonly unknown[] | []>(values: T, signal?: AbortSignal): Futurable<Awaited<T[number]>>;
175
+ /**
176
+ * Creates a polling service with cancellation support and possibility to handle error.
177
+ */
178
+ static polling<T>(value: () => Futurable<T>, { interval, signal }: {
179
+ interval: number;
180
+ signal?: AbortSignal;
181
+ }): {
182
+ cancel: () => void;
183
+ catch: (onrejected: (reason: unknown) => void) => void;
184
+ };
175
185
  }
186
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa,CAAC,CAAC;IAC/B;;;;;OAKG;IACH,IAAI,CAAC,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,KAAK,EAClC,WAAW,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,EAC3G,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,GAC3G,aAAa,CAAC,QAAQ,GAAG,QAAQ,CAAC,CAAC;CACtC;AACD,MAAM,WAAW,gBAAgB,CAAC,CAAC;IAClC,CAAC,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC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IAAI,CAAC;QAAC,KAAK,EAAE,CAAC,UAAU,EAAC,CAAC,MAAM,EAAE,OAAO,KAAG,IAAI,KAAG,IAAI,CAAA;KAAE;CAqBpL"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@ndriadev/futurable",
3
3
  "description": "Extension Javascript's Promise API with more functionalities",
4
4
  "private": false,
5
- "version": "2.0.10",
5
+ "version": "2.1.0",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "dist/",
@@ -23,25 +23,25 @@
23
23
  "postinstall": "echo 'postinstall executed'"
24
24
  },
25
25
  "devDependencies": {
26
- "@babel/preset-typescript": "^7.22.5",
27
- "@jest/globals": "^29.6.2",
28
- "@types/node": "^20.4.7",
29
- "@typescript-eslint/eslint-plugin": "^6.2.1",
30
- "@typescript-eslint/parser": "^6.2.1",
31
- "eslint": "^8.46.0",
32
- "eslint-config-prettier": "^8.10.0",
33
- "eslint-plugin-import": "^2.28.0",
34
- "eslint-plugin-prettier": "^5.0.0",
35
- "jest": "^29.6.2",
36
- "prettier": "^3.0.1",
37
- "ts-jest": "^29.1.1",
38
- "ts-node": "^10.9.1",
39
- "typedoc": "^0.24.8",
40
- "typescript": "^5.1.6",
41
- "vite": "^4.4.8",
42
- "vite-plugin-dts": "^3.5.0",
43
- "vite-plugin-linter": "^2.0.2",
44
- "vite-tsconfig-paths": "^4.2.0"
26
+ "@babel/preset-typescript": "^7.23.3",
27
+ "@jest/globals": "^29.7.0",
28
+ "@types/node": "^20.11.16",
29
+ "@typescript-eslint/eslint-plugin": "^6.21.0",
30
+ "@typescript-eslint/parser": "^6.21.0",
31
+ "eslint": "^8.56.0",
32
+ "eslint-config-prettier": "^9.1.0",
33
+ "eslint-plugin-import": "^2.29.1",
34
+ "eslint-plugin-prettier": "^5.1.3",
35
+ "jest": "^29.7.0",
36
+ "prettier": "^3.2.5",
37
+ "ts-jest": "^29.1.2",
38
+ "ts-node": "^10.9.2",
39
+ "typedoc": "^0.25.7",
40
+ "typescript": "^5.3.3",
41
+ "vite": "^5.0.12",
42
+ "vite-plugin-dts": "^3.7.2",
43
+ "vite-plugin-linter": "^2.1.1",
44
+ "vite-tsconfig-paths": "^4.3.1"
45
45
  },
46
46
  "keywords": [
47
47
  "promise",