-
Notifications
You must be signed in to change notification settings - Fork 0
/
usePromiseAllTrace.decorator.ts
60 lines (57 loc) · 1.89 KB
/
usePromiseAllTrace.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
PromiseAllMappedTraceNode,
PromiseAllTraceNode,
} from './PromiseAllTraceNode';
import { trace } from './traceFunction';
import type { IPublicActionTraceNode } from './traceNode';
import type { TraceNode } from './traceNode';
import { UseTrace } from './useTrace.decorator';
export function UsePromiseAllTrace<T extends readonly Promise<any>[]>(
arrGetter: (currentTraceNode: IPublicActionTraceNode) => T,
TraceClass: new (
expectedAmountOfChildren: number | null,
parentTraceNode: TraceNode,
) => TraceNode = PromiseAllTraceNode,
): (parentTraceNode: IPublicActionTraceNode) => PromiseAllDone<T> {
return {
async 'Promise.all'(
parentTraceNode: IPublicActionTraceNode,
): PromiseAllDone<T> {
// this.name = 'Promise.all';
const currentTraceNode = new TraceClass(
null,
parentTraceNode as TraceNode,
);
return await trace(
currentTraceNode,
(current: TraceNode): PromiseAllDone<T> => {
const promiseArr = arrGetter(current);
current.expectedAmountOfChildren = promiseArr.length;
return Promise.all(promiseArr);
},
[],
);
},
}['Promise.all']; // hack to give custom name to function
}
export function UsePromiseAllMappedTrace<
TInputArrayElement,
TReturnArrayElement,
>(
arrayToMap: TInputArrayElement[],
elementGetter: (
currentTraceNode: IPublicActionTraceNode,
arrayElement: TInputArrayElement,
index: number,
) => Promise<TReturnArrayElement>,
): (parentTraceNode: IPublicActionTraceNode) => Promise<TReturnArrayElement[]> {
const tracedElementGetter = UseTrace(elementGetter);
return UsePromiseAllTrace(
(current) =>
arrayToMap.map((e, index) => tracedElementGetter(current, e, index)),
PromiseAllMappedTraceNode,
);
}
type PromiseAllDone<T extends readonly Promise<any>[]> = Promise<{
-readonly [P in keyof T]: Awaited<T[P]>;
}>;