more-proms 1.8.0 → 1.8.2
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/app/dist/cjs/moreProms.d.ts +34 -39
- package/app/dist/cjs/moreProms.js +44 -19
- package/app/dist/esm/moreProms.d.ts +34 -39
- package/app/dist/esm/moreProms.mjs +44 -19
- package/package.json +2 -1
|
@@ -3,7 +3,10 @@ type P<Args extends unknown[], Ret> = {
|
|
|
3
3
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): P<Args, Ret | TResult>;
|
|
4
4
|
} & ((...a: Args) => (CancelAblePromise<Ret> | Promise<Ret> | undefined));
|
|
5
5
|
export declare function latestLatent<Args extends unknown[], Ret>(cb: (...args: Args) => (CancelAblePromise<Ret> | Promise<Ret> | undefined)): P<Args, Ret>;
|
|
6
|
-
export declare function execQueue(
|
|
6
|
+
export declare function execQueue(defaultOptions?: {
|
|
7
|
+
skipAble: boolean;
|
|
8
|
+
cancelVal: string;
|
|
9
|
+
}): <T, FR extends Promise<T> | CancelAblePromise<T, string, string | void | Promise<any>>>(f: () => FR, options?: typeof defaultOptions | boolean, cancelPrevIfPossible?: boolean) => FR;
|
|
7
10
|
export declare class SyncPromise<T = unknown> {
|
|
8
11
|
private thenListener;
|
|
9
12
|
private catchListener;
|
|
@@ -22,66 +25,58 @@ export declare class SyncPromise<T = unknown> {
|
|
|
22
25
|
static race(proms: SyncPromise[]): SyncPromise<unknown>;
|
|
23
26
|
static allSettled(proms: SyncPromise[]): SyncPromise<unknown>;
|
|
24
27
|
}
|
|
25
|
-
|
|
26
|
-
export type SettledPromise<T = void> = Promise<T> & {
|
|
28
|
+
type SettledPromProps<T> = {
|
|
27
29
|
settled: boolean;
|
|
28
30
|
onSettled: Promise<void>;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledPromise<T | TResult>;
|
|
32
|
-
finally(onfinally?: (() => void) | undefined | null): SettledPromise<T>;
|
|
31
|
+
res: (t: T) => void;
|
|
32
|
+
rej: (err: any) => void;
|
|
33
33
|
};
|
|
34
|
-
|
|
35
|
-
export type ResablePromise<T = void> = SettledPromise<T> & {
|
|
34
|
+
type ResablePromProps<T> = {
|
|
36
35
|
res: (t: T) => void;
|
|
37
36
|
rej: (err: any) => void;
|
|
38
|
-
} & {
|
|
39
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ResablePromise<TResult1 | TResult2>;
|
|
40
|
-
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ResablePromise<T | TResult>;
|
|
41
|
-
finally(onfinally?: (() => void) | undefined | null): ResablePromise<T>;
|
|
42
37
|
};
|
|
43
|
-
|
|
44
|
-
export type CancelAblePromise<T = unknown, C = void, CT = C> = SettledPromise<T> & {
|
|
38
|
+
type CancelAblePromProps<T, C, CT> = {
|
|
45
39
|
cancel: (reason: C) => CT;
|
|
46
40
|
cancelled: boolean;
|
|
47
41
|
onCancel: Promise<{
|
|
48
42
|
reason: C;
|
|
49
43
|
cancelResult: CT;
|
|
50
44
|
}>;
|
|
51
|
-
}
|
|
45
|
+
};
|
|
46
|
+
export declare const SettledPromise: PromiseConstructor & (new <T = void>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => SettledPromise<T>);
|
|
47
|
+
export type SettledPromise<T = unknown> = SettledPromProps<T> & {
|
|
48
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): SettledPromise<TResult1 | TResult2>;
|
|
49
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledPromise<T | TResult>;
|
|
50
|
+
finally(onfinally?: (() => void) | undefined | null): SettledPromise<T>;
|
|
51
|
+
} & Promise<T>;
|
|
52
|
+
export declare const ResablePromise: PromiseConstructor & (new <T = void>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => ResablePromise<T>);
|
|
53
|
+
export type ResablePromise<T = unknown> = SettledPromProps<T> & ResablePromProps<T> & {
|
|
54
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ResablePromise<TResult1 | TResult2>;
|
|
55
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ResablePromise<T | TResult>;
|
|
56
|
+
finally(onfinally?: (() => void) | undefined | null): ResablePromise<T>;
|
|
57
|
+
} & Promise<T>;
|
|
58
|
+
export declare const CancelAblePromise: PromiseConstructor & (new <T = void, C = void, CT = C>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel?: (reason: C) => void) => CancelAblePromise<T, C, CT>);
|
|
59
|
+
export type CancelAblePromise<T = unknown, C = void, CT = C> = SettledPromProps<T> & ResablePromProps<T> & CancelAblePromProps<T, C, CT> & {
|
|
52
60
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): CancelAblePromise<TResult1 | TResult2>;
|
|
53
61
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): CancelAblePromise<T | TResult>;
|
|
54
62
|
finally(onfinally?: (() => void) | undefined | null): CancelAblePromise<T>;
|
|
55
|
-
}
|
|
56
|
-
export declare const
|
|
57
|
-
export type SettledSyncPromise<T =
|
|
58
|
-
settled: boolean;
|
|
59
|
-
onSettled: Promise<void>;
|
|
60
|
-
} & {
|
|
63
|
+
} & Promise<T>;
|
|
64
|
+
export declare const SettledSyncPromise: typeof SyncPromise & (new <T = void>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => SettledSyncPromise<T>);
|
|
65
|
+
export type SettledSyncPromise<T = unknown> = SettledPromProps<T> & {
|
|
61
66
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): SettledSyncPromise<TResult1 | TResult2>;
|
|
62
67
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledSyncPromise<T | TResult>;
|
|
63
68
|
finally(onfinally?: (() => void) | undefined | null): SettledSyncPromise<T>;
|
|
64
|
-
}
|
|
65
|
-
export declare const
|
|
66
|
-
export type ResableSyncPromise<T =
|
|
67
|
-
res: (t: T) => void;
|
|
68
|
-
rej: (err: any) => void;
|
|
69
|
-
} & {
|
|
69
|
+
} & SyncPromise<T>;
|
|
70
|
+
export declare const ResableSyncPromise: typeof SyncPromise & (new <T = void>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => ResableSyncPromise<T>);
|
|
71
|
+
export type ResableSyncPromise<T = unknown> = SettledPromProps<T> & ResablePromProps<T> & {
|
|
70
72
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ResableSyncPromise<TResult1 | TResult2>;
|
|
71
73
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ResableSyncPromise<T | TResult>;
|
|
72
74
|
finally(onfinally?: (() => void) | undefined | null): ResableSyncPromise<T>;
|
|
73
|
-
}
|
|
74
|
-
export declare const
|
|
75
|
-
export type CancelAbleSyncPromise<T = unknown, C = void, CT = C> =
|
|
76
|
-
cancel: (reason: C) => CT;
|
|
77
|
-
cancelled: boolean;
|
|
78
|
-
onCancel: SyncPromise<{
|
|
79
|
-
reason: C;
|
|
80
|
-
cancelResult: CT;
|
|
81
|
-
}>;
|
|
82
|
-
} & {
|
|
75
|
+
} & SyncPromise<T>;
|
|
76
|
+
export declare const CancelAbleSyncPromise: typeof SyncPromise & (new <T = unknown, C = void, CT = C>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel?: (reason: C) => void) => CancelAbleSyncPromise<T, C, CT>);
|
|
77
|
+
export type CancelAbleSyncPromise<T = unknown, C = void, CT = C> = SettledPromProps<T> & ResablePromProps<T> & CancelAblePromProps<T, C, CT> & {
|
|
83
78
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): CancelAbleSyncPromise<TResult1 | TResult2>;
|
|
84
79
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): CancelAbleSyncPromise<T | TResult>;
|
|
85
80
|
finally(onfinally?: (() => void) | undefined | null): CancelAbleSyncPromise<T>;
|
|
86
|
-
}
|
|
81
|
+
} & SyncPromise<T>;
|
|
87
82
|
export {};
|
|
@@ -43,41 +43,67 @@ function latestLatent(cb) {
|
|
|
43
43
|
propagateFuture(request, futures, "catch", prom);
|
|
44
44
|
return request;
|
|
45
45
|
}
|
|
46
|
-
function execQueue(
|
|
46
|
+
function execQueue(defaultOptions = {
|
|
47
|
+
skipAble: false,
|
|
48
|
+
cancelVal: "cancelled by execQueue"
|
|
49
|
+
}) {
|
|
47
50
|
const queue = [];
|
|
48
51
|
let curFP;
|
|
52
|
+
let curCancelVal;
|
|
49
53
|
let running = false;
|
|
50
54
|
let wantToCancelUntil;
|
|
55
|
+
let curOb;
|
|
56
|
+
let cancelDataStore;
|
|
51
57
|
async function makeSureQueueIsStarted() {
|
|
52
58
|
if (running)
|
|
53
59
|
return;
|
|
54
60
|
running = true;
|
|
55
61
|
while (queue.length !== 0) {
|
|
56
|
-
const ob = queue.shift();
|
|
57
|
-
const { p, f, skipAble } = ob;
|
|
58
|
-
|
|
62
|
+
const ob = curOb = queue.shift();
|
|
63
|
+
const { p, f, skipAble, cancelVal } = ob;
|
|
64
|
+
if (wantToCancelUntil === ob)
|
|
65
|
+
wantToCancelUntil = undefined;
|
|
66
|
+
const wantToCancelThis = wantToCancelUntil !== undefined;
|
|
59
67
|
if (!(wantToCancelThis && skipAble)) {
|
|
60
68
|
const prom = curFP = f();
|
|
61
|
-
|
|
62
|
-
prom.cancel("cancelled by execQueue");
|
|
63
|
-
p.res(prom);
|
|
69
|
+
curCancelVal = cancelVal;
|
|
64
70
|
const localPromsToContinue = [];
|
|
65
|
-
if ("cancel" in prom)
|
|
71
|
+
if ("cancel" in prom) {
|
|
66
72
|
localPromsToContinue.push(prom.onCancel.then(({ reason, cancelResult }) => cancelResult));
|
|
67
|
-
|
|
73
|
+
prom.onCancel.then((r) => {
|
|
74
|
+
cancelDataStore = r;
|
|
75
|
+
p.cancel(r.reason);
|
|
76
|
+
cancelDataStore = undefined;
|
|
77
|
+
});
|
|
78
|
+
if (wantToCancelThis)
|
|
79
|
+
prom.cancel(cancelVal);
|
|
80
|
+
}
|
|
81
|
+
p.res(prom);
|
|
82
|
+
const promSettled = "onSettled" in prom ? prom.onSettled : new Promise((res) => { prom.then(res, res); });
|
|
83
|
+
localPromsToContinue.push(promSettled);
|
|
68
84
|
await Promise.race(localPromsToContinue);
|
|
69
85
|
}
|
|
70
86
|
}
|
|
71
87
|
running = false;
|
|
72
88
|
}
|
|
73
|
-
return (f,
|
|
74
|
-
|
|
75
|
-
const
|
|
89
|
+
return (f, options = defaultOptions, cancelPrevIfPossible = false) => {
|
|
90
|
+
options = typeof options === "boolean" ? { ...defaultOptions, ...{ skipAble: options } } : { ...defaultOptions, ...options };
|
|
91
|
+
const p = new CancelAblePromise(() => { }, (cVal) => {
|
|
92
|
+
// on cancel
|
|
93
|
+
if (cancelDataStore !== undefined)
|
|
94
|
+
return cancelDataStore.cancelResult;
|
|
95
|
+
// if ("cancel" in curFP): we can assume this as the types restrict it.
|
|
96
|
+
if (curOb === ob)
|
|
97
|
+
return curFP.cancel(cVal);
|
|
98
|
+
// we can assume that it is still in the list, as it has to be non-resolved to be cancelled (as ensured by CancelAblePromise)
|
|
99
|
+
queue.splice(queue.indexOf(ob), 1);
|
|
100
|
+
});
|
|
101
|
+
const ob = { f, p, skipAble: options.skipAble, cancelVal: options.cancelVal };
|
|
76
102
|
queue.push(ob);
|
|
77
103
|
if (cancelPrevIfPossible && running) {
|
|
78
104
|
wantToCancelUntil = ob;
|
|
79
105
|
if ("cancel" in curFP)
|
|
80
|
-
curFP.cancel(
|
|
106
|
+
curFP.cancel(curCancelVal);
|
|
81
107
|
}
|
|
82
108
|
makeSureQueueIsStarted();
|
|
83
109
|
return p;
|
|
@@ -203,19 +229,14 @@ class SyncPromise {
|
|
|
203
229
|
});
|
|
204
230
|
}
|
|
205
231
|
}
|
|
206
|
-
// type PromInterface<T> = {
|
|
207
|
-
// then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): SettledPromise<TResult1 | TResult2>;
|
|
208
|
-
// catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledPromise<T | TResult>;
|
|
209
|
-
// finally(onfinally?: (() => void) | undefined | null): SettledPromise<T>;
|
|
210
|
-
// }
|
|
211
232
|
const { SettledPromise: _SettledPromise, ResablePromise: _ResablePromise, CancelAblePromise: _CancelAblePromise } = mkExt(Promise);
|
|
212
233
|
const SettledPromise = _SettledPromise;
|
|
213
234
|
const ResablePromise = _ResablePromise;
|
|
214
235
|
const CancelAblePromise = _CancelAblePromise;
|
|
215
236
|
const { ResablePromise: _ResableSyncPromise, CancelAblePromise: _CancelAbleSyncPromise, SettledPromise: _SettledSyncPromise } = mkExt(SyncPromise);
|
|
237
|
+
const SettledSyncPromise = _SettledSyncPromise;
|
|
216
238
|
const ResableSyncPromise = _ResableSyncPromise;
|
|
217
239
|
const CancelAbleSyncPromise = _CancelAbleSyncPromise;
|
|
218
|
-
const SettledSyncPromise = _SettledSyncPromise;
|
|
219
240
|
function mkExt(Prom) {
|
|
220
241
|
class SettledPromise extends Prom {
|
|
221
242
|
constructor(executor) {
|
|
@@ -251,6 +272,10 @@ function mkExt(Prom) {
|
|
|
251
272
|
}
|
|
252
273
|
}
|
|
253
274
|
class ResablePromise extends SettledPromise {
|
|
275
|
+
constructor(a) {
|
|
276
|
+
super(a);
|
|
277
|
+
// todo: deprecate
|
|
278
|
+
}
|
|
254
279
|
}
|
|
255
280
|
class CancelAblePromise extends SettledPromise {
|
|
256
281
|
constructor(executor, cancel) {
|
|
@@ -3,7 +3,10 @@ type P<Args extends unknown[], Ret> = {
|
|
|
3
3
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): P<Args, Ret | TResult>;
|
|
4
4
|
} & ((...a: Args) => (CancelAblePromise<Ret> | Promise<Ret> | undefined));
|
|
5
5
|
export declare function latestLatent<Args extends unknown[], Ret>(cb: (...args: Args) => (CancelAblePromise<Ret> | Promise<Ret> | undefined)): P<Args, Ret>;
|
|
6
|
-
export declare function execQueue(
|
|
6
|
+
export declare function execQueue(defaultOptions?: {
|
|
7
|
+
skipAble: boolean;
|
|
8
|
+
cancelVal: string;
|
|
9
|
+
}): <T, FR extends Promise<T> | CancelAblePromise<T, string, string | void | Promise<any>>>(f: () => FR, options?: typeof defaultOptions | boolean, cancelPrevIfPossible?: boolean) => FR;
|
|
7
10
|
export declare class SyncPromise<T = unknown> {
|
|
8
11
|
private thenListener;
|
|
9
12
|
private catchListener;
|
|
@@ -22,66 +25,58 @@ export declare class SyncPromise<T = unknown> {
|
|
|
22
25
|
static race(proms: SyncPromise[]): SyncPromise<unknown>;
|
|
23
26
|
static allSettled(proms: SyncPromise[]): SyncPromise<unknown>;
|
|
24
27
|
}
|
|
25
|
-
|
|
26
|
-
export type SettledPromise<T = void> = Promise<T> & {
|
|
28
|
+
type SettledPromProps<T> = {
|
|
27
29
|
settled: boolean;
|
|
28
30
|
onSettled: Promise<void>;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledPromise<T | TResult>;
|
|
32
|
-
finally(onfinally?: (() => void) | undefined | null): SettledPromise<T>;
|
|
31
|
+
res: (t: T) => void;
|
|
32
|
+
rej: (err: any) => void;
|
|
33
33
|
};
|
|
34
|
-
|
|
35
|
-
export type ResablePromise<T = void> = SettledPromise<T> & {
|
|
34
|
+
type ResablePromProps<T> = {
|
|
36
35
|
res: (t: T) => void;
|
|
37
36
|
rej: (err: any) => void;
|
|
38
|
-
} & {
|
|
39
|
-
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ResablePromise<TResult1 | TResult2>;
|
|
40
|
-
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ResablePromise<T | TResult>;
|
|
41
|
-
finally(onfinally?: (() => void) | undefined | null): ResablePromise<T>;
|
|
42
37
|
};
|
|
43
|
-
|
|
44
|
-
export type CancelAblePromise<T = unknown, C = void, CT = C> = SettledPromise<T> & {
|
|
38
|
+
type CancelAblePromProps<T, C, CT> = {
|
|
45
39
|
cancel: (reason: C) => CT;
|
|
46
40
|
cancelled: boolean;
|
|
47
41
|
onCancel: Promise<{
|
|
48
42
|
reason: C;
|
|
49
43
|
cancelResult: CT;
|
|
50
44
|
}>;
|
|
51
|
-
}
|
|
45
|
+
};
|
|
46
|
+
export declare const SettledPromise: PromiseConstructor & (new <T = void>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => SettledPromise<T>);
|
|
47
|
+
export type SettledPromise<T = unknown> = SettledPromProps<T> & {
|
|
48
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): SettledPromise<TResult1 | TResult2>;
|
|
49
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledPromise<T | TResult>;
|
|
50
|
+
finally(onfinally?: (() => void) | undefined | null): SettledPromise<T>;
|
|
51
|
+
} & Promise<T>;
|
|
52
|
+
export declare const ResablePromise: PromiseConstructor & (new <T = void>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => ResablePromise<T>);
|
|
53
|
+
export type ResablePromise<T = unknown> = SettledPromProps<T> & ResablePromProps<T> & {
|
|
54
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ResablePromise<TResult1 | TResult2>;
|
|
55
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ResablePromise<T | TResult>;
|
|
56
|
+
finally(onfinally?: (() => void) | undefined | null): ResablePromise<T>;
|
|
57
|
+
} & Promise<T>;
|
|
58
|
+
export declare const CancelAblePromise: PromiseConstructor & (new <T = void, C = void, CT = C>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel?: (reason: C) => void) => CancelAblePromise<T, C, CT>);
|
|
59
|
+
export type CancelAblePromise<T = unknown, C = void, CT = C> = SettledPromProps<T> & ResablePromProps<T> & CancelAblePromProps<T, C, CT> & {
|
|
52
60
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): CancelAblePromise<TResult1 | TResult2>;
|
|
53
61
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): CancelAblePromise<T | TResult>;
|
|
54
62
|
finally(onfinally?: (() => void) | undefined | null): CancelAblePromise<T>;
|
|
55
|
-
}
|
|
56
|
-
export declare const
|
|
57
|
-
export type SettledSyncPromise<T =
|
|
58
|
-
settled: boolean;
|
|
59
|
-
onSettled: Promise<void>;
|
|
60
|
-
} & {
|
|
63
|
+
} & Promise<T>;
|
|
64
|
+
export declare const SettledSyncPromise: typeof SyncPromise & (new <T = void>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => SettledSyncPromise<T>);
|
|
65
|
+
export type SettledSyncPromise<T = unknown> = SettledPromProps<T> & {
|
|
61
66
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): SettledSyncPromise<TResult1 | TResult2>;
|
|
62
67
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledSyncPromise<T | TResult>;
|
|
63
68
|
finally(onfinally?: (() => void) | undefined | null): SettledSyncPromise<T>;
|
|
64
|
-
}
|
|
65
|
-
export declare const
|
|
66
|
-
export type ResableSyncPromise<T =
|
|
67
|
-
res: (t: T) => void;
|
|
68
|
-
rej: (err: any) => void;
|
|
69
|
-
} & {
|
|
69
|
+
} & SyncPromise<T>;
|
|
70
|
+
export declare const ResableSyncPromise: typeof SyncPromise & (new <T = void>(executor?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => ResableSyncPromise<T>);
|
|
71
|
+
export type ResableSyncPromise<T = unknown> = SettledPromProps<T> & ResablePromProps<T> & {
|
|
70
72
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ResableSyncPromise<TResult1 | TResult2>;
|
|
71
73
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ResableSyncPromise<T | TResult>;
|
|
72
74
|
finally(onfinally?: (() => void) | undefined | null): ResableSyncPromise<T>;
|
|
73
|
-
}
|
|
74
|
-
export declare const
|
|
75
|
-
export type CancelAbleSyncPromise<T = unknown, C = void, CT = C> =
|
|
76
|
-
cancel: (reason: C) => CT;
|
|
77
|
-
cancelled: boolean;
|
|
78
|
-
onCancel: SyncPromise<{
|
|
79
|
-
reason: C;
|
|
80
|
-
cancelResult: CT;
|
|
81
|
-
}>;
|
|
82
|
-
} & {
|
|
75
|
+
} & SyncPromise<T>;
|
|
76
|
+
export declare const CancelAbleSyncPromise: typeof SyncPromise & (new <T = unknown, C = void, CT = C>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel?: (reason: C) => void) => CancelAbleSyncPromise<T, C, CT>);
|
|
77
|
+
export type CancelAbleSyncPromise<T = unknown, C = void, CT = C> = SettledPromProps<T> & ResablePromProps<T> & CancelAblePromProps<T, C, CT> & {
|
|
83
78
|
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): CancelAbleSyncPromise<TResult1 | TResult2>;
|
|
84
79
|
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): CancelAbleSyncPromise<T | TResult>;
|
|
85
80
|
finally(onfinally?: (() => void) | undefined | null): CancelAbleSyncPromise<T>;
|
|
86
|
-
}
|
|
81
|
+
} & SyncPromise<T>;
|
|
87
82
|
export {};
|
|
@@ -40,41 +40,67 @@ export function latestLatent(cb) {
|
|
|
40
40
|
propagateFuture(request, futures, "catch", prom);
|
|
41
41
|
return request;
|
|
42
42
|
}
|
|
43
|
-
export function execQueue(
|
|
43
|
+
export function execQueue(defaultOptions = {
|
|
44
|
+
skipAble: false,
|
|
45
|
+
cancelVal: "cancelled by execQueue"
|
|
46
|
+
}) {
|
|
44
47
|
const queue = [];
|
|
45
48
|
let curFP;
|
|
49
|
+
let curCancelVal;
|
|
46
50
|
let running = false;
|
|
47
51
|
let wantToCancelUntil;
|
|
52
|
+
let curOb;
|
|
53
|
+
let cancelDataStore;
|
|
48
54
|
async function makeSureQueueIsStarted() {
|
|
49
55
|
if (running)
|
|
50
56
|
return;
|
|
51
57
|
running = true;
|
|
52
58
|
while (queue.length !== 0) {
|
|
53
|
-
const ob = queue.shift();
|
|
54
|
-
const { p, f, skipAble } = ob;
|
|
55
|
-
|
|
59
|
+
const ob = curOb = queue.shift();
|
|
60
|
+
const { p, f, skipAble, cancelVal } = ob;
|
|
61
|
+
if (wantToCancelUntil === ob)
|
|
62
|
+
wantToCancelUntil = undefined;
|
|
63
|
+
const wantToCancelThis = wantToCancelUntil !== undefined;
|
|
56
64
|
if (!(wantToCancelThis && skipAble)) {
|
|
57
65
|
const prom = curFP = f();
|
|
58
|
-
|
|
59
|
-
prom.cancel("cancelled by execQueue");
|
|
60
|
-
p.res(prom);
|
|
66
|
+
curCancelVal = cancelVal;
|
|
61
67
|
const localPromsToContinue = [];
|
|
62
|
-
if ("cancel" in prom)
|
|
68
|
+
if ("cancel" in prom) {
|
|
63
69
|
localPromsToContinue.push(prom.onCancel.then(({ reason, cancelResult }) => cancelResult));
|
|
64
|
-
|
|
70
|
+
prom.onCancel.then((r) => {
|
|
71
|
+
cancelDataStore = r;
|
|
72
|
+
p.cancel(r.reason);
|
|
73
|
+
cancelDataStore = undefined;
|
|
74
|
+
});
|
|
75
|
+
if (wantToCancelThis)
|
|
76
|
+
prom.cancel(cancelVal);
|
|
77
|
+
}
|
|
78
|
+
p.res(prom);
|
|
79
|
+
const promSettled = "onSettled" in prom ? prom.onSettled : new Promise((res) => { prom.then(res, res); });
|
|
80
|
+
localPromsToContinue.push(promSettled);
|
|
65
81
|
await Promise.race(localPromsToContinue);
|
|
66
82
|
}
|
|
67
83
|
}
|
|
68
84
|
running = false;
|
|
69
85
|
}
|
|
70
|
-
return (f,
|
|
71
|
-
|
|
72
|
-
const
|
|
86
|
+
return (f, options = defaultOptions, cancelPrevIfPossible = false) => {
|
|
87
|
+
options = typeof options === "boolean" ? { ...defaultOptions, ...{ skipAble: options } } : { ...defaultOptions, ...options };
|
|
88
|
+
const p = new CancelAblePromise(() => { }, (cVal) => {
|
|
89
|
+
// on cancel
|
|
90
|
+
if (cancelDataStore !== undefined)
|
|
91
|
+
return cancelDataStore.cancelResult;
|
|
92
|
+
// if ("cancel" in curFP): we can assume this as the types restrict it.
|
|
93
|
+
if (curOb === ob)
|
|
94
|
+
return curFP.cancel(cVal);
|
|
95
|
+
// we can assume that it is still in the list, as it has to be non-resolved to be cancelled (as ensured by CancelAblePromise)
|
|
96
|
+
queue.splice(queue.indexOf(ob), 1);
|
|
97
|
+
});
|
|
98
|
+
const ob = { f, p, skipAble: options.skipAble, cancelVal: options.cancelVal };
|
|
73
99
|
queue.push(ob);
|
|
74
100
|
if (cancelPrevIfPossible && running) {
|
|
75
101
|
wantToCancelUntil = ob;
|
|
76
102
|
if ("cancel" in curFP)
|
|
77
|
-
curFP.cancel(
|
|
103
|
+
curFP.cancel(curCancelVal);
|
|
78
104
|
}
|
|
79
105
|
makeSureQueueIsStarted();
|
|
80
106
|
return p;
|
|
@@ -200,19 +226,14 @@ export class SyncPromise {
|
|
|
200
226
|
});
|
|
201
227
|
}
|
|
202
228
|
}
|
|
203
|
-
// type PromInterface<T> = {
|
|
204
|
-
// then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): SettledPromise<TResult1 | TResult2>;
|
|
205
|
-
// catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): SettledPromise<T | TResult>;
|
|
206
|
-
// finally(onfinally?: (() => void) | undefined | null): SettledPromise<T>;
|
|
207
|
-
// }
|
|
208
229
|
const { SettledPromise: _SettledPromise, ResablePromise: _ResablePromise, CancelAblePromise: _CancelAblePromise } = mkExt(Promise);
|
|
209
230
|
export const SettledPromise = _SettledPromise;
|
|
210
231
|
export const ResablePromise = _ResablePromise;
|
|
211
232
|
export const CancelAblePromise = _CancelAblePromise;
|
|
212
233
|
const { ResablePromise: _ResableSyncPromise, CancelAblePromise: _CancelAbleSyncPromise, SettledPromise: _SettledSyncPromise } = mkExt(SyncPromise);
|
|
234
|
+
export const SettledSyncPromise = _SettledSyncPromise;
|
|
213
235
|
export const ResableSyncPromise = _ResableSyncPromise;
|
|
214
236
|
export const CancelAbleSyncPromise = _CancelAbleSyncPromise;
|
|
215
|
-
export const SettledSyncPromise = _SettledSyncPromise;
|
|
216
237
|
function mkExt(Prom) {
|
|
217
238
|
let finallyInit = false;
|
|
218
239
|
class SettledPromise extends Prom {
|
|
@@ -249,6 +270,10 @@ function mkExt(Prom) {
|
|
|
249
270
|
}
|
|
250
271
|
}
|
|
251
272
|
class ResablePromise extends SettledPromise {
|
|
273
|
+
constructor(a) {
|
|
274
|
+
super(a);
|
|
275
|
+
// todo: deprecate
|
|
276
|
+
}
|
|
252
277
|
}
|
|
253
278
|
class CancelAblePromise extends SettledPromise {
|
|
254
279
|
constructor(executor, cancel) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "more-proms",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "A collection of additional promise extending classes. Including a (from the outside) ResablePromise, CancelAblePromise and a latestLatent utility function.",
|
|
5
5
|
"main": "./app/dist/esm/moreProms.mjs",
|
|
6
6
|
"types": "./app/dist/esm/moreProms.d.ts",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"mjsify": "^2.0.6",
|
|
67
67
|
"open": "^9.1.0",
|
|
68
68
|
"rollup": "^3.21.7",
|
|
69
|
+
"timoi": "^1.1.12",
|
|
69
70
|
"tiny-delay": "^1.2.0",
|
|
70
71
|
"tslib": "2.0.0",
|
|
71
72
|
"typescript": "^5.0.4",
|