@@ -34,7 +34,14 @@ export class CacheOperator<
34
34
35
35
private readonly op : ( option : TOptions ) => TTask ;
36
36
private readonly cache : Cache < TRes & CacheRes > = new Cache ( ) ;
37
+ /**
38
+ * 正在处理的回调
39
+ */
37
40
private readonly callbackMapList : { [ key : string ] : { success : Function [ ] ; fail : Function [ ] ; complete : Function [ ] } } = { } ;
41
+ /**
42
+ * 处理完的回调,待删除
43
+ */
44
+ private readonly completeMapList : { [ key : string ] : Function [ ] } = { } ;
38
45
39
46
constructor ( operator : ( option : TOptions ) => TTask , config ?: Configuration < TRes , TOptions > ) {
40
47
this . op = operator ;
@@ -92,15 +99,17 @@ export class CacheOperator<
92
99
if ( this . config . resultCondition ( res ) ) {
93
100
this . cache . set ( key , res , this . config . expire ) ;
94
101
}
95
- this . callbackMapList [ key ] . success . forEach ( ( v ) => { v ( res ) ; } ) ;
102
+ this . _getMapBeforeComplete ( key ) . success
103
+ . forEach ( ( v ) => { v ( res ) ; } ) ;
96
104
} ,
97
105
fail : ( res : { errMsg : string } ) => {
98
- this . callbackMapList [ key ] . fail . forEach ( ( v ) => { v ( res ) ; } ) ;
106
+ this . _getMapBeforeComplete ( key ) . fail
107
+ . forEach ( ( v ) => { v ( res ) ; } ) ;
99
108
} ,
100
109
complete : ( res : TRes ) => {
101
- this . callbackMapList [ key ] . complete . forEach ( ( v ) => { v ( res ) ; } ) ;
110
+ this . completeMapList [ key ] . forEach ( ( v ) => { v ( res ) ; } ) ;
102
111
// tslint:disable-next-line: no-dynamic-delete
103
- delete this . callbackMapList [ key ] ;
112
+ delete this . completeMapList [ key ] ;
104
113
}
105
114
} ;
106
115
return this . op ( data ) ;
@@ -112,20 +121,37 @@ export class CacheOperator<
112
121
if ( options . success ) {
113
122
arrayRemove ( this . callbackMapList [ key ] . success , options . success ) ;
114
123
}
124
+ const callbackList = [ ] ;
115
125
if ( options . fail ) {
116
126
arrayRemove ( this . callbackMapList [ key ] . fail , options . fail ) ;
117
- options . fail ( { errMsg : 'request:fail abort' , cancel : true , source : CacheOperator . name } ) ;
127
+ callbackList . push ( options . fail ) ;
118
128
}
119
129
if ( options . complete ) {
120
- options . complete ( { errMsg : 'request:fail abort' , cancel : true , source : CacheOperator . name } ) ;
121
130
arrayRemove ( this . callbackMapList [ key ] . complete , options . complete ) ;
131
+ callbackList . push ( options . complete ) ;
122
132
}
133
+ const res = { errMsg : 'request:fail abort' , cancel : true , source : CacheOperator . name } ;
134
+ callbackList . forEach ( f => { f ( res ) ; } ) ;
123
135
}
124
136
} ,
125
137
onHeadersReceived : doNothing as TTask [ 'onHeadersReceived' ] ,
126
138
onProgressUpdate : doNothing as TTask [ 'onProgressUpdate' ]
127
139
} as TTask ;
128
140
}
141
+
142
+ /**
143
+ * fixed #10
144
+ * 在回调中再次发起操作前清除任务
145
+ * @param key cacheKey
146
+ */
147
+ private _getMapBeforeComplete ( key : string ) : { success : Function [ ] ; fail : Function [ ] ; complete : Function [ ] } {
148
+ // remove the MapList from the `callbackMapList`
149
+ const list = this . callbackMapList [ key ] ;
150
+ // tslint:disable-next-line: no-dynamic-delete
151
+ delete this . callbackMapList [ key ] ;
152
+ this . completeMapList [ key ] = list . complete ;
153
+ return list ;
154
+ }
129
155
}
130
156
131
157
interface CacheRes {
0 commit comments