Skip to content

Commit b2c5cd1

Browse files
committed
docs: init utils md
1 parent 28e057f commit b2c5cd1

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

docs/docs/utils.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
## chainFn 函数
2+
3+
### source code
4+
```ts
5+
type ExecutorFn = (...args: any) => any;
6+
7+
const flat = (arr: any[], num = 1) => {
8+
let i = 0;
9+
while (arr.some((item) => Array.isArray(item))) {
10+
arr = [].concat(...arr);
11+
i++;
12+
if (i >= num) break;
13+
}
14+
return arr;
15+
};
16+
17+
export class ChainFn {
18+
resList: any[];
19+
executorList: any[];
20+
errorList: any[];
21+
22+
constructor(executor?: ExecutorFn, args?: any, resList: any[] = [], executorList: any[] = [], errorList: any[] = []) {
23+
this.resList = resList;
24+
this.executorList = executorList;
25+
this.errorList = errorList;
26+
args = flat([args]);
27+
28+
// 添加执行函数
29+
executor && this.executorList.push([executor, args]);
30+
}
31+
32+
public add(onResFn: ExecutorFn, args?: any) {
33+
args = flat([args]);
34+
return new ChainFn(onResFn, args, this.resList, this.executorList, this.errorList);
35+
}
36+
37+
private async executeSync(executor: Function, args: any[]) {
38+
return executor instanceof Promise ? await (executor as any).apply(this, args) : executor.apply(this, args);
39+
}
40+
41+
public async run(onResFn?: Function, args?: any): Promise<ChainFn> {
42+
await this.getValue();
43+
args = flat([args]);
44+
45+
if (onResFn) {
46+
try {
47+
const returnValue = await this.executeSync(onResFn, [this.resList, ...args]);
48+
returnValue && this.resList.push(returnValue);
49+
} catch (error) {
50+
this.errorList.push(error);
51+
}
52+
}
53+
54+
return new ChainFn(undefined, undefined, this.resList, this.executorList, this.errorList);
55+
}
56+
57+
public clear() {
58+
return new ChainFn(undefined, undefined, [], [], []);
59+
}
60+
61+
public async get(allRes = false): Promise<any> {
62+
await this.getValue();
63+
this.resList = this.resList.filter(v => v);
64+
return allRes ? this.resList : this.resList[this.resList.length-1];
65+
}
66+
67+
private async getValue() {
68+
for (let index = 0; index < this.executorList.length; index++) {
69+
const [executor, args, isDo = false] = this.executorList[index];
70+
const useRes = executor.name === '' ? true : false;
71+
72+
if (isDo)
73+
continue;
74+
try {
75+
const value = useRes ? await this.executeSync(executor, [this.resList]) : await this.executeSync(executor, args);
76+
this.resList.push(value);
77+
this.executorList[index].push(true);
78+
} catch (error) {
79+
this.errorList.push(error);
80+
}
81+
}
82+
}
83+
84+
public catch(callBack: Function) {
85+
if (this.errorList.length) {
86+
return callBack.apply(this, this.errorList);
87+
}
88+
}
89+
}
90+
```
91+
92+
### template
93+
```ts
94+
console.log(await new ChainFn()
95+
.add(say, ['2', 2])
96+
.add(say, '3')
97+
.add(say, '4')
98+
.add(say, '5')
99+
.add(say, '6')
100+
.add((res) => {
101+
console.log(res, '获取前方的结果');
102+
103+
return say('7');
104+
})
105+
.get(), '最后的结果');
106+
/**
107+
* 2
108+
* 3
109+
* 4
110+
* 5
111+
* 6
112+
* [ '2', '3', '4', '5', '6' ] 获取前方的结果
113+
* 7
114+
* 7 最后的结果
115+
*/
116+
```

0 commit comments

Comments
 (0)