-
Notifications
You must be signed in to change notification settings - Fork 0
/
useTraceRoot.decorator.ts
32 lines (31 loc) · 1.06 KB
/
useTraceRoot.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
import { logObjectNicely } from './logObjectNicely';
import { renderRootTraceNode } from './renderTraceNode';
import { RootTraceNode } from './rootTraceNode';
import { trace } from './traceFunction';
import type { IPublicActionTraceNode } from './traceNode';
export function UseTraceRoot<const NormalArgs extends any[], const TReturn>(
functionToDecorate: (
currentTraceNode: IPublicActionTraceNode,
...args: NormalArgs
) => Promise<TReturn>,
expectedAmountOfChildren: number | null = null,
): (...args: NormalArgs) => Promise<TReturn> {
return async function decoratedFunc(...args: NormalArgs) {
const currentTraceNode = new RootTraceNode(expectedAmountOfChildren);
try {
const execResult = await trace(
currentTraceNode,
functionToDecorate,
args,
);
logObjectNicely({ execResult });
return execResult;
} catch (error) {
logObjectNicely({ error });
throw error;
} finally {
// logObjectNicely(currentTraceNode);
console.log(renderRootTraceNode(currentTraceNode));
}
};
}