@ndriadev/futurable 2.0.2 → 2.0.3
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/futurable.cjs.js +1 -1
- package/dist/futurable.mjs.js +7 -3
- package/dist/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +0 -563
package/dist/futurable.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var T=Object.defineProperty;var C=(
|
|
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;
|
package/dist/futurable.mjs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var T = Object.defineProperty;
|
|
2
|
-
var C = (
|
|
3
|
-
var S = (
|
|
2
|
+
var C = (g, w, t) => w in g ? T(g, w, { enumerable: !0, configurable: !0, writable: !0, value: t }) : g[w] = t;
|
|
3
|
+
var S = (g, w, t) => (C(g, typeof w != "symbol" ? w + "" : w, t), t);
|
|
4
4
|
class l extends Promise {
|
|
5
5
|
constructor(t, e) {
|
|
6
6
|
const r = e ? null : new AbortController(), n = e || r.signal, o = [], c = () => {
|
|
@@ -119,7 +119,11 @@ class l extends Promise {
|
|
|
119
119
|
finally(t) {
|
|
120
120
|
return this.then(
|
|
121
121
|
(e) => (t(), e),
|
|
122
|
-
(e) =>
|
|
122
|
+
(e) => {
|
|
123
|
+
if (t(), e instanceof Error)
|
|
124
|
+
throw e;
|
|
125
|
+
return e;
|
|
126
|
+
}
|
|
123
127
|
);
|
|
124
128
|
}
|
|
125
129
|
/**
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;CACrD;AAED,MAAM,WAAW,eAAe;IAC/B,CAAC,MAAM,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,cAAc,CAAC,CAAC;IAChC;;OAEG;IACH,MAAM,EAAE,WAAW,CAAC;IACpB;;OAEG;IACH,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB;;;OAGG;IACH,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IACnC;;;;OAIG;IACH,KAAK,E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,CAAC;CAkB5G"}
|
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.
|
|
5
|
+
"version": "2.0.3",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist/",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
|
-
"main": "./
|
|
24
|
-
"types": "./
|
|
23
|
+
"main": "./dist/futurable.mjs.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
25
|
"//scripts": {
|
|
26
26
|
"preinstall": "node ./scripts/preinstall.js --foreground-script",
|
|
27
27
|
"postinstall": "echo 'postinstall executed'"
|
package/src/index.ts
DELETED
|
@@ -1,563 +0,0 @@
|
|
|
1
|
-
export interface FuturableLike<T> {
|
|
2
|
-
/**
|
|
3
|
-
* Attaches callbacks for the resolution and/or rejection of the Futurable.
|
|
4
|
-
* @param onfulfilled The callback to execute when the Futurable is resolved.
|
|
5
|
-
* @param onrejected The callback to execute when the Futurable is rejected.
|
|
6
|
-
* @returns A Futurable for the completion of which ever callback is executed.
|
|
7
|
-
*/
|
|
8
|
-
then<TResult1 = T, TResult2 = never>(
|
|
9
|
-
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1> | FuturableLike<TResult1>) | undefined | null,
|
|
10
|
-
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2> | FuturableLike<TResult2>) | undefined | null
|
|
11
|
-
): FuturableLike<TResult1 | TResult2>;
|
|
12
|
-
}
|
|
13
|
-
export interface FuturableResolve<T> {
|
|
14
|
-
(value: T | FuturableLike<T> | PromiseLike<T>): void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface FuturableReject {
|
|
18
|
-
(reason?: any): void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface FuturableUtils<T> {
|
|
22
|
-
/**
|
|
23
|
-
* Internal futurable signal
|
|
24
|
-
*/
|
|
25
|
-
signal: AbortSignal;
|
|
26
|
-
/**
|
|
27
|
-
* Cancel the futurable if it is to be executed or if it is still executing.
|
|
28
|
-
*/
|
|
29
|
-
cancel: () => void;
|
|
30
|
-
/**
|
|
31
|
-
* Executes the callback passed as a parameter when the futurable is cancelled.
|
|
32
|
-
* @param cb: callback
|
|
33
|
-
*/
|
|
34
|
-
onCancel: (cb: () => void) => void;
|
|
35
|
-
/**
|
|
36
|
-
* Waits for timer, then executes callback with the futurable value and returns the result obtained from the invocation.
|
|
37
|
-
* @param cb: callback executed after timer
|
|
38
|
-
* @param timer: timer to wait (in milliseconds)
|
|
39
|
-
*/
|
|
40
|
-
delay: <TResult=T, TResult2=never>(cb: () => TResult, timer: number) => FuturableLike<TResult | TResult2>;
|
|
41
|
-
/**
|
|
42
|
-
* Waits for timer parameter (in milliseconds) before returning the value.
|
|
43
|
-
* @param timer: timer to wait (in milliseconds)
|
|
44
|
-
*/
|
|
45
|
-
sleep: (timer: number) => FuturableLike<void>;
|
|
46
|
-
/**
|
|
47
|
-
* Extension of the fetch API with cancellation support. Url parameter can be a string or a function with receive value from futurable chaining as paremeter.
|
|
48
|
-
* @param url: url to fetch
|
|
49
|
-
* @param opts: fetch options
|
|
50
|
-
*/
|
|
51
|
-
fetch: (url: string, opts?: RequestInit) => Futurable<Response>;
|
|
52
|
-
/**
|
|
53
|
-
* Takes a promise and transforms it into a futurizable. Promise can be also a function that receives value from futurable chaining as parameter.
|
|
54
|
-
* @param promise: Promise to futurize
|
|
55
|
-
*/
|
|
56
|
-
futurizable: <TResult=any>(promise: Promise<TResult>) => Futurable<TResult>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export type FuturableExecutor<T> = (
|
|
60
|
-
resolve: FuturableResolve<T>,
|
|
61
|
-
reject: FuturableReject,
|
|
62
|
-
/**
|
|
63
|
-
* Object containing implemented functionalities.
|
|
64
|
-
*/
|
|
65
|
-
utils: FuturableUtils<T>
|
|
66
|
-
) => void;
|
|
67
|
-
|
|
68
|
-
export type FuturableIterable<T = any> = Iterable<FuturableLike<T> | PromiseLike<T> | T>;
|
|
69
|
-
|
|
70
|
-
enum FUTURABLE_STATUS {
|
|
71
|
-
PENDING = "pending",
|
|
72
|
-
FULFILLED = "fulfilled",
|
|
73
|
-
REJECTED = "rejected"
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export class Futurable<T> extends Promise<T> {
|
|
77
|
-
private controller;
|
|
78
|
-
private internalSignal;
|
|
79
|
-
private idsTimeout;
|
|
80
|
-
|
|
81
|
-
constructor(executor: FuturableExecutor<T>, signal?: AbortSignal) {
|
|
82
|
-
const controller: AbortController | null = signal ? null : new AbortController();
|
|
83
|
-
const sign = signal || controller!.signal;
|
|
84
|
-
const idsTimeout: ReturnType<typeof setTimeout>[] = [];
|
|
85
|
-
|
|
86
|
-
const abortTimeout = () => {
|
|
87
|
-
for (const timeout of idsTimeout) {
|
|
88
|
-
clearTimeout(timeout);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
let abort: () => void;
|
|
93
|
-
|
|
94
|
-
const onCancel = (cb: () => void): void => {
|
|
95
|
-
abort = cb;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const utils: FuturableUtils<T> = {
|
|
99
|
-
signal: sign,
|
|
100
|
-
cancel: (): void => this.controller?.abort(),
|
|
101
|
-
onCancel,
|
|
102
|
-
delay: (cb, timer) => {
|
|
103
|
-
return new Futurable(res => {
|
|
104
|
-
idsTimeout.push(setTimeout(() => {
|
|
105
|
-
res(cb());
|
|
106
|
-
}, timer));
|
|
107
|
-
}, sign);
|
|
108
|
-
},
|
|
109
|
-
sleep: (timer) => {
|
|
110
|
-
return utils.delay(() => { }, timer);
|
|
111
|
-
},
|
|
112
|
-
fetch: (url: string, opts?: RequestInit): Futurable<Response> => {
|
|
113
|
-
return new Futurable<Response>((res, rej) => {
|
|
114
|
-
fetch(url, { ...(opts || {}), signal: sign })
|
|
115
|
-
.then(val => res(val))
|
|
116
|
-
.catch(err => {
|
|
117
|
-
if (err.name === "AbortError") {
|
|
118
|
-
return;
|
|
119
|
-
} else {
|
|
120
|
-
rej(err);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
}, sign);
|
|
124
|
-
},
|
|
125
|
-
futurizable: (promise) => {
|
|
126
|
-
return new Futurable((res, rej) => {
|
|
127
|
-
promise
|
|
128
|
-
.then(res)
|
|
129
|
-
.catch(rej);
|
|
130
|
-
}, sign);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
let status = FUTURABLE_STATUS.PENDING;
|
|
135
|
-
|
|
136
|
-
const p = new Promise<T>((resolve, reject) => {
|
|
137
|
-
if (!sign.aborted) {
|
|
138
|
-
const func: (() => void) = typeof sign.onabort === "function" ? sign.onabort as () => void : () => { };
|
|
139
|
-
sign.onabort = () => {
|
|
140
|
-
func();
|
|
141
|
-
abortTimeout();
|
|
142
|
-
if (status === FUTURABLE_STATUS.PENDING) {
|
|
143
|
-
abort && abort();
|
|
144
|
-
}
|
|
145
|
-
return;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const res: FuturableResolve<T> = (val) => {
|
|
149
|
-
status = FUTURABLE_STATUS.FULFILLED;
|
|
150
|
-
resolve(val as T | PromiseLike<T>);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const rej: FuturableReject = (reason) => {
|
|
154
|
-
status = FUTURABLE_STATUS.REJECTED;
|
|
155
|
-
reject(reason);
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
executor(res, rej, utils);
|
|
159
|
-
} else {
|
|
160
|
-
abortTimeout();
|
|
161
|
-
status === FUTURABLE_STATUS.PENDING && abort && abort();
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
super((resolve, reject) => {
|
|
166
|
-
p.then(val => resolve(val)).catch(reject);
|
|
167
|
-
});
|
|
168
|
-
this.controller = controller;
|
|
169
|
-
this.internalSignal = sign;
|
|
170
|
-
this.idsTimeout = idsTimeout;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
static get [Symbol.species]() {
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
get [Symbol.toStringTag]() {
|
|
178
|
-
return 'Futurable';
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Return internal futurable signal
|
|
183
|
-
*/
|
|
184
|
-
get signal() {
|
|
185
|
-
return this.internalSignal;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private clearTimeout() {
|
|
189
|
-
for (const timeout of this.idsTimeout) {
|
|
190
|
-
clearTimeout(timeout);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Attaches callbacks for the resolution and/or rejection of the Futurable.
|
|
196
|
-
*/
|
|
197
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1> | FuturableLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2> | FuturableLike<TResult2>) | undefined | null): Futurable<TResult1 | TResult2> {
|
|
198
|
-
let resolve: FuturableResolve<TResult1|TResult2>, reject: FuturableReject;
|
|
199
|
-
const p = new Futurable<TResult1|TResult2>((res, rej) => {
|
|
200
|
-
resolve = res;
|
|
201
|
-
reject = rej;
|
|
202
|
-
}, this.internalSignal);
|
|
203
|
-
p.controller = this.controller;
|
|
204
|
-
super.then(val => {
|
|
205
|
-
if (this.internalSignal?.aborted) {
|
|
206
|
-
this.clearTimeout();
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
try {
|
|
210
|
-
if (onfulfilled) {
|
|
211
|
-
resolve(onfulfilled(val));
|
|
212
|
-
} else {
|
|
213
|
-
resolve(val as unknown as TResult1);
|
|
214
|
-
}
|
|
215
|
-
} catch (error) {
|
|
216
|
-
reject(error);
|
|
217
|
-
}
|
|
218
|
-
}, reason => {
|
|
219
|
-
if (this.internalSignal?.aborted) {
|
|
220
|
-
this.clearTimeout();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
if (onrejected) {
|
|
225
|
-
resolve(onrejected(reason));
|
|
226
|
-
} else {
|
|
227
|
-
reject(reason);
|
|
228
|
-
}
|
|
229
|
-
} catch (error) {
|
|
230
|
-
reject(error);
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
return p;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Attaches a callback for only the rejection of the Futurable.
|
|
238
|
-
*/
|
|
239
|
-
catch<TResult2 = never>(onRejected: ((reason: any) => TResult2 | PromiseLike<TResult2> | FuturableLike<TResult2>) | undefined | null): Futurable<T | TResult2> {
|
|
240
|
-
return this.then(null, onRejected);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Attaches a callback that is invoked when the Futurable is settled (fulfilled or rejected).
|
|
245
|
-
* The resolved value cannot be modified from the callback.
|
|
246
|
-
*/
|
|
247
|
-
finally(onfinally: () => void | undefined | null): Futurable<T> {
|
|
248
|
-
return this.then(
|
|
249
|
-
(val) => {
|
|
250
|
-
onfinally();
|
|
251
|
-
return val;
|
|
252
|
-
},
|
|
253
|
-
(reason) => {
|
|
254
|
-
onfinally();
|
|
255
|
-
return reason;
|
|
256
|
-
}
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Cancel the futurable if it is to be executed or if it is still executing.
|
|
262
|
-
*/
|
|
263
|
-
cancel(): void {
|
|
264
|
-
!this.internalSignal?.aborted && this.controller?.abort();
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Waits for timer, then executes callback with the futurable value and returns the result obtained from the invocation.
|
|
269
|
-
* @param cb: callback executed after timer with futurable chain value as parameter
|
|
270
|
-
* @param timer: timer to wait (in milliseconds)
|
|
271
|
-
*/
|
|
272
|
-
delay<TResult1 = T, TResult2 = never>(cb: (val: T) => TResult1 | PromiseLike<TResult1> | FuturableLike<TResult1>, timer: number): Futurable<TResult1 | TResult2> {
|
|
273
|
-
let resolve: FuturableResolve<TResult1 | TResult2>, reject: FuturableReject;
|
|
274
|
-
const p = new Futurable<TResult1 | TResult2>((res, rej) => {
|
|
275
|
-
resolve = res;
|
|
276
|
-
reject = rej;
|
|
277
|
-
}, this.internalSignal);
|
|
278
|
-
p.controller = this.controller;
|
|
279
|
-
this.then(
|
|
280
|
-
val => {
|
|
281
|
-
this.idsTimeout.push(setTimeout(() => resolve(cb(val)), timer));
|
|
282
|
-
},
|
|
283
|
-
reason => {
|
|
284
|
-
reject(reason);
|
|
285
|
-
}
|
|
286
|
-
);
|
|
287
|
-
return p;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Waits for timer parameter (in milliseconds) before returning the value.
|
|
292
|
-
* @param timer: timer to wait (in milliseconds)
|
|
293
|
-
*/
|
|
294
|
-
sleep(timer: number): Futurable<T> {
|
|
295
|
-
return this.delay(val => val, timer);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Extension of the fetch API with cancellation support. Url parameter can be a string or a function with receive value from futurable chaining as paremeter.
|
|
300
|
-
* @param url: url to fetch or function with futurable chaining value that returns url to fetch
|
|
301
|
-
* @param opts: fetch options or function with futurable chaining value that return fetch options
|
|
302
|
-
*/
|
|
303
|
-
fetch(url: string | ((val?: T) => string), opts?: object | RequestInit | ((val?: T) => RequestInit)): Futurable<Response> {
|
|
304
|
-
let resolve: FuturableResolve<Response>, reject: FuturableReject;
|
|
305
|
-
const p = new Futurable<Response>((res, rej) => {
|
|
306
|
-
resolve = res;
|
|
307
|
-
reject = rej;
|
|
308
|
-
}, this.internalSignal);
|
|
309
|
-
p.controller = this.controller;
|
|
310
|
-
this.then(val => {
|
|
311
|
-
const urlFetch = typeof url === "function" ? url(val) : url,
|
|
312
|
-
optsFetch = { ...(typeof opts === "function" ? opts(val) : opts), signal: this.internalSignal };
|
|
313
|
-
|
|
314
|
-
fetch(urlFetch, optsFetch).then(val => resolve(val)).catch(err => {
|
|
315
|
-
if (err.name === "AbortError") {
|
|
316
|
-
return;
|
|
317
|
-
} else {
|
|
318
|
-
reject(err);
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
return p;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Executes the callback passed as a parameter when the futurable is cancelled.
|
|
327
|
-
* @param cb: callback
|
|
328
|
-
*/
|
|
329
|
-
onCancel<TResult1 = void, TResult2 = never>(cb: () => void): Futurable<TResult1 | TResult2> {
|
|
330
|
-
let resolve: FuturableResolve<TResult1 | TResult2>, reject: FuturableReject;
|
|
331
|
-
const f = new Futurable<TResult1 | TResult2>((res, rej, utils) => {
|
|
332
|
-
utils.onCancel(cb);
|
|
333
|
-
resolve = res;
|
|
334
|
-
reject = rej;
|
|
335
|
-
}, this.internalSignal);
|
|
336
|
-
f.controller = this.controller;
|
|
337
|
-
|
|
338
|
-
this.then(
|
|
339
|
-
val => resolve(val as unknown as TResult1),
|
|
340
|
-
reason => reject(reason)
|
|
341
|
-
);
|
|
342
|
-
return f;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// promisify<TResult1 = T, TResult2 = never>(): Promise<TResult1 | TResult2> {
|
|
346
|
-
// return new Promise((res, rej) => {
|
|
347
|
-
// if (this.#signal.aborted) {
|
|
348
|
-
// this.#clearTimeout();
|
|
349
|
-
// return;
|
|
350
|
-
// } else {
|
|
351
|
-
// this.then(
|
|
352
|
-
// val => res(val),
|
|
353
|
-
// reason => rej(reason)
|
|
354
|
-
// );
|
|
355
|
-
// }
|
|
356
|
-
// });
|
|
357
|
-
// }
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* Takes a promise and transforms it into a futurizable. Promise can be also a function that receives value from futurable chaining as parameter.
|
|
361
|
-
* @param promise: Promise to futurize or function that return promise with futurable chaining value as parameter
|
|
362
|
-
*/
|
|
363
|
-
futurizable<TResult1 = T, TResult2 = never>(promise: Promise<TResult1> | ((val?: T) => Promise<TResult1>)): Futurable<TResult1 | TResult2> {
|
|
364
|
-
let resolve: FuturableResolve<TResult1 | TResult2>, reject: FuturableReject;
|
|
365
|
-
const f = new Futurable<TResult1 | TResult2>((res, rej) => {
|
|
366
|
-
resolve = res;
|
|
367
|
-
reject = rej;
|
|
368
|
-
}, this.internalSignal);
|
|
369
|
-
f.controller = this.controller;
|
|
370
|
-
this.then(val => {
|
|
371
|
-
const p = typeof promise === "function" ? promise(val) : promise;
|
|
372
|
-
p
|
|
373
|
-
.then(resolve)
|
|
374
|
-
.catch(reject);
|
|
375
|
-
});
|
|
376
|
-
return f;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
static resolve(): Futurable<void>;
|
|
380
|
-
static resolve<T=any>(value: T | PromiseLike<T> | FuturableLike<T>, signal?: AbortSignal): Futurable<T>;
|
|
381
|
-
static resolve<T=any>(value?: T | PromiseLike<T> | FuturableLike<T>, signal?: AbortSignal): Futurable<T|void> {
|
|
382
|
-
return value
|
|
383
|
-
? new Futurable(res => res(value), signal)
|
|
384
|
-
|
|
385
|
-
: new Futurable<void>(res=> res(), signal);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
static reject<T = never>(reason?: any, signal?: AbortSignal): Futurable<T> {
|
|
389
|
-
return new Futurable((res, rej) => rej(reason), signal);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* OnCancel static method. It accepts a callback or a object with cb property and an optional signal.
|
|
394
|
-
*/
|
|
395
|
-
static onCancel<T=void>({ cb, signal }: {cb: () => T, signal?: AbortSignal}): Futurable<T> {
|
|
396
|
-
return new Futurable((res, rej, utils) => {
|
|
397
|
-
utils.onCancel(() => res(cb()));
|
|
398
|
-
}, signal);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Delay static method. It accepts a object with timer and cb properties and an optional signal property.
|
|
403
|
-
*/
|
|
404
|
-
static delay<T = any, TResult2 = never>({ cb, timer, signal }: { cb: () => any, timer: number, signal?: AbortSignal }): Futurable<T | TResult2> {
|
|
405
|
-
return new Futurable((res, rej, utils) => {
|
|
406
|
-
utils.delay(cb, timer).then(res, rej);
|
|
407
|
-
}, signal)
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* Sleep static method. It accepts a timer or a object with timer property and an optional signal.
|
|
412
|
-
*/
|
|
413
|
-
static sleep({ timer, signal }: { timer: number, signal?: AbortSignal }): Futurable<void> {
|
|
414
|
-
return Futurable.delay<void>({
|
|
415
|
-
cb: () => { },
|
|
416
|
-
timer,
|
|
417
|
-
signal
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* Fetch static method.
|
|
423
|
-
*/
|
|
424
|
-
static fetch(url: string, opts?: RequestInit): Futurable<Response> {
|
|
425
|
-
const signal = opts?.signal || undefined;
|
|
426
|
-
opts?.signal && delete opts.signal;
|
|
427
|
-
return new Futurable((res, rej, utils) => {
|
|
428
|
-
utils.fetch(url, opts).then(res);
|
|
429
|
-
}, signal)
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Futurizable static method.
|
|
434
|
-
*/
|
|
435
|
-
static futurizable<TResult1=any, TResult2=never>({ promise, signal }: { promise: Promise<TResult1>, signal?: AbortSignal }): Futurable<TResult1 | TResult2> {
|
|
436
|
-
return new Futurable((res, rej) => {
|
|
437
|
-
promise
|
|
438
|
-
.then(res)
|
|
439
|
-
.catch(rej);
|
|
440
|
-
}, signal);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private static handleValues<T extends readonly unknown[] | []>(values: T, signal?: AbortSignal): Futurable<T>[] {
|
|
444
|
-
const array: Futurable<T>[] = [];
|
|
445
|
-
|
|
446
|
-
for (const i in values) {
|
|
447
|
-
if ((values[i] instanceof Futurable)) {
|
|
448
|
-
array.push(values[i] as Futurable<{ -readonly [P in keyof T]: T[P] }>);
|
|
449
|
-
}
|
|
450
|
-
else if ((values[i] instanceof Promise)) {
|
|
451
|
-
array.push(
|
|
452
|
-
new Futurable<{ - readonly [P in keyof T]: T[P] }>(
|
|
453
|
-
(res, rej) => {
|
|
454
|
-
(values[i] as Promise<{ -readonly [P in keyof T]: T[P] }>)
|
|
455
|
-
.then((val) => res(val))
|
|
456
|
-
.catch(rej);
|
|
457
|
-
},
|
|
458
|
-
signal
|
|
459
|
-
)
|
|
460
|
-
);
|
|
461
|
-
} else {
|
|
462
|
-
array.push(
|
|
463
|
-
new Futurable<{ - readonly [P in keyof T]: T[P] }>(
|
|
464
|
-
res => res(values[i] as { -readonly [P in keyof T]: T[P] | FuturableLike<T[P]> | PromiseLike<T[P]> }),
|
|
465
|
-
signal
|
|
466
|
-
)
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
return array;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* Creates a Futurable with cancellation support that is resolved with an array of results when all of the provided Futurables resolve, or rejected when any Futurable is rejected.
|
|
476
|
-
*/
|
|
477
|
-
static all<T extends readonly unknown[] | []>(values: T, signal?: AbortSignal): Futurable<{ -readonly [P in keyof T]: Awaited<T[P]>; }> {
|
|
478
|
-
let resolve: FuturableResolve<{ -readonly [P in keyof T]: Awaited<T[P]> }>, reject: FuturableReject;
|
|
479
|
-
const f = new Futurable<{ -readonly [P in keyof T]: Awaited<T[P]> }>((res, rej, utils) => {
|
|
480
|
-
resolve = res;
|
|
481
|
-
reject = rej;
|
|
482
|
-
utils.onCancel(() => {
|
|
483
|
-
for (const futurable of array) {
|
|
484
|
-
futurable.cancel();
|
|
485
|
-
}
|
|
486
|
-
})
|
|
487
|
-
}, signal);
|
|
488
|
-
signal ||= f.internalSignal;
|
|
489
|
-
const array = Futurable.handleValues(values, signal);
|
|
490
|
-
|
|
491
|
-
super.all(array).then(val => resolve(val as { -readonly [P in keyof T]: Awaited<T[P]>; })).catch(reason => reject(reason));
|
|
492
|
-
|
|
493
|
-
return f;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
/**
|
|
497
|
-
* Creates a Futurable with cancellation support that is resolved with an array of results when all of the provided Futurables resolve or reject.
|
|
498
|
-
*/
|
|
499
|
-
static allSettled<T extends readonly unknown[] | []>(values: T, signal?: AbortSignal): Futurable<{ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>> }> {
|
|
500
|
-
let resolve: FuturableResolve<{ -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>; }>;
|
|
501
|
-
const f = new Futurable<{ - readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>> }>((res, rej, utils) => {
|
|
502
|
-
resolve = res;
|
|
503
|
-
utils.onCancel(() => {
|
|
504
|
-
for (const futurable of array) {
|
|
505
|
-
futurable.cancel();
|
|
506
|
-
}
|
|
507
|
-
})
|
|
508
|
-
}, signal);
|
|
509
|
-
signal ||= f.internalSignal;
|
|
510
|
-
const array = Futurable.handleValues(values, signal);
|
|
511
|
-
|
|
512
|
-
super.allSettled(array).then(val => resolve(val as { -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>; }));
|
|
513
|
-
|
|
514
|
-
return f;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Creates a Futurable with cancellation support that is resolved or rejected when any of the provided Futurables are resolved or rejected.
|
|
519
|
-
*/
|
|
520
|
-
static race<T extends readonly unknown[] | []>(values: T, signal?: AbortSignal): Futurable<Awaited<T[number]>> {
|
|
521
|
-
let resolve: FuturableResolve<Awaited<T[number]>>, reject: FuturableReject;
|
|
522
|
-
const f = new Futurable<Awaited<T[number]>>((res, rej, utils) => {
|
|
523
|
-
resolve = res;
|
|
524
|
-
reject = rej;
|
|
525
|
-
utils.onCancel(() => {
|
|
526
|
-
for (const futurable of array) {
|
|
527
|
-
futurable.cancel();
|
|
528
|
-
}
|
|
529
|
-
})
|
|
530
|
-
}, signal);
|
|
531
|
-
signal ||= f.internalSignal;
|
|
532
|
-
const array = Futurable.handleValues(values, signal);
|
|
533
|
-
|
|
534
|
-
super.race(array).then(val => resolve(val)).catch(reason => reject(reason));
|
|
535
|
-
|
|
536
|
-
return f;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* The any function returns a futurable with cancellation support that is fulfilled by the first given futurable to be fulfilled,
|
|
541
|
-
* or rejected with an AggregateError containing an array of rejection reasons if all of the
|
|
542
|
-
* given futurables are rejected. It resolves all elements of the passed iterable to futurables as
|
|
543
|
-
* it runs this algorithm.
|
|
544
|
-
*/
|
|
545
|
-
static any<T extends readonly unknown[] | []>(value: T, signal?: AbortSignal): Futurable<Awaited<T[number]>> {
|
|
546
|
-
let resolve: FuturableResolve<Awaited<T[number]>>, reject: FuturableReject;
|
|
547
|
-
const f = new Futurable<Awaited<T[number]>>((res, rej, utils) => {
|
|
548
|
-
resolve = res;
|
|
549
|
-
reject = rej;
|
|
550
|
-
utils.onCancel(() => {
|
|
551
|
-
for (const futurable of array) {
|
|
552
|
-
futurable.cancel();
|
|
553
|
-
}
|
|
554
|
-
})
|
|
555
|
-
}, signal);
|
|
556
|
-
signal ||= f.internalSignal;
|
|
557
|
-
const array = Futurable.handleValues(value, signal);
|
|
558
|
-
|
|
559
|
-
super.any(array).then(val => resolve(val)).catch(reason => reject(reason));
|
|
560
|
-
|
|
561
|
-
return f;
|
|
562
|
-
}
|
|
563
|
-
}
|