-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
113 lines (99 loc) · 3.69 KB
/
test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { setTimeout } from 'timers/promises';
import { UsePromiseAllMappedTrace } from './usePromiseAllTrace.decorator';
import { UseTrace } from './useTrace.decorator';
import { UseTraceRoot } from './useTraceRoot.decorator';
// import { RootTraceNode } from './rootTraceNode';
// import { logObjectNicely } from 'src/tools/shared';
// import { renderRootTraceNode } from './renderTraceNode';
import type { IPublicActionTraceNode } from './traceNode';
export async function testUsualCase(): Promise<void> {
class LowLevelUseCase {
#lowLevelMethod = async (
currentTraceNode: IPublicActionTraceNode,
lowLevelArg: string,
): Promise<number> => {
currentTraceNode.saveLog('beforeTimeout', {});
await setTimeout(Math.random() * 1000);
currentTraceNode.saveLog('afterTimeout', {});
console.log('LowLevelUseCase', 'lowLevelMethod', lowLevelArg);
const num = parseInt(lowLevelArg, 10);
if (num > 1000) throw new RangeError('message');
return num;
};
lowLevelMethod = UseTrace(this.#lowLevelMethod, 2);
lowLevelMethod2 = UseTraceRoot(this.#lowLevelMethod, 2);
}
class HighLevelUseCase {
constructor(private readonly lowLevelUseCase: LowLevelUseCase) {}
#highLevelMethod = async (
currentTraceNode: IPublicActionTraceNode,
highLevelArg: string[],
): Promise<number[]> => {
const result = await UsePromiseAllMappedTrace(
highLevelArg,
async (currentTraceNode, e: string): Promise<number> => {
await this.lowLevelUseCase.lowLevelMethod(currentTraceNode, e);
return await this.lowLevelUseCase.lowLevelMethod(currentTraceNode, e);
},
)(currentTraceNode);
console.log('HighLevelUseCase', 'highLevelMethod', result);
return result;
};
highLevelMethod = UseTrace(this.#highLevelMethod, 1);
highLevelMethodRoot = UseTraceRoot(this.#highLevelMethod, 1);
}
class Controller {
constructor(private readonly highLevelUseCase: HighLevelUseCase) {}
controllerLevelMethod = UseTraceRoot(
async (currentTraceNode, firstArr: string[], secondArr: string[]) => {
let result: number[] = [];
try {
result = await this.highLevelUseCase.highLevelMethod(
currentTraceNode,
firstArr.concat(secondArr),
);
console.log('Controller', 'controllerLevelMethod', result);
} catch (error) {
currentTraceNode.saveMaybeError(error);
}
try {
const result2 = await this.highLevelUseCase.highLevelMethod(
currentTraceNode,
firstArr.concat(secondArr).reverse(),
);
console.log('Controller', 'controllerLevelMethod2', result2);
} catch (error) {
currentTraceNode.saveMaybeError(error);
}
return result;
},
);
}
const instanceOfLowLevelUseCase = new LowLevelUseCase();
console.log('instanceOfLowLevelUseCase: ', instanceOfLowLevelUseCase);
const instanceOfHighLevelUseCase = new HighLevelUseCase(
instanceOfLowLevelUseCase,
);
console.log('instanceOfHighLevelUseCase: ', instanceOfHighLevelUseCase);
try {
const asd = await instanceOfHighLevelUseCase.highLevelMethodRoot([
'12322',
'asdd',
]);
// console.log('traceNode: ', );
console.log('asd: ', asd);
} catch (error) {
console.log('error: ', error);
}
const instanceOfController = new Controller(instanceOfHighLevelUseCase);
try {
const result1 = await instanceOfController.controllerLevelMethod(
['123', '23400'],
['12333', '234'],
);
console.log('result1: ', result1);
} catch (error) {
console.log('error: ', error);
}
// console.log('result1: ', result1);
}