@@ -29,7 +29,7 @@ export class ChainFn {
29
29
executor && this .executorList .push ([executor , args ]);
30
30
}
31
31
32
- public add(onResFn : ExecutorFn , args ? : any ) {
32
+ public add(onResFn : ExecutorFn , ... args : any [] ) {
33
33
args = flat ([args ]);
34
34
return new ChainFn (onResFn , args , this .resList , this .executorList , this .errorList );
35
35
}
@@ -38,7 +38,7 @@ export class ChainFn {
38
38
return executor instanceof Promise ? await (executor as any ).apply (this , args ) : executor .apply (this , args );
39
39
}
40
40
41
- public async run(onResFn ? : Function , args ? : any ): Promise <ChainFn > {
41
+ public async run(onResFn ? : Function , ... args : any [] ): Promise <ChainFn > {
42
42
await this .getValue ();
43
43
args = flat ([args ]);
44
44
@@ -60,7 +60,6 @@ export class ChainFn {
60
60
61
61
public async get(allRes = false ): Promise <any > {
62
62
await this .getValue ();
63
- this .resList = this .resList .filter (v => v );
64
63
return allRes ? this .resList : this .resList [this .resList .length - 1 ];
65
64
}
66
65
@@ -113,4 +112,56 @@ console.log(await new ChainFn()
113
112
* 7
114
113
* 7 最后的结果
115
114
*/
115
+ ```
116
+
117
+ ### mini-chainFn (only sync)
118
+ ``` ts
119
+ type ExecutorFn = (... args : any ) => any ;
120
+
121
+ export const flat = (arr : any [], num = 1 ) => {
122
+ let i = 0 ;
123
+ while (arr .some ((item ) => Array .isArray (item ))) {
124
+ arr = [].concat (... arr );
125
+ i ++ ;
126
+ if (i >= num ) break ;
127
+ }
128
+ return arr ;
129
+ };
130
+
131
+ export class ChainFn {
132
+ resList! : any [];
133
+
134
+ constructor (executor ? : ExecutorFn , args ? : any , resList : any [] = []) {
135
+ this .initValue (resList );
136
+ args = flat ([args ]);
137
+ // 执行对应方法
138
+ const returnValue = executor ?.apply (this , args ! );
139
+
140
+ returnValue && this .resList .push (returnValue );
141
+ }
142
+
143
+ public initValue(resList : any []) {
144
+ this .resList = resList ;
145
+ }
146
+
147
+ public add(onResFn : ExecutorFn , args : any ) {
148
+ args = flat ([args ]);
149
+ return new ChainFn (onResFn , args , this .resList );
150
+ }
151
+
152
+ // eslint-disable-next-line @typescript-eslint/ban-types
153
+ public run(onResFn : Function , args : any ) {
154
+ try {
155
+ args = flat ([args ]);
156
+ const returnValue = onResFn .apply (this , [this .resList , ... args ]);
157
+ if (returnValue ) return returnValue ;
158
+ } catch (error ) {
159
+ console .warn (error );
160
+ }
161
+ }
162
+
163
+ public remove() {
164
+ return new ChainFn (undefined , undefined , []);
165
+ }
166
+ }
116
167
```
0 commit comments