Skip to content

Commit f4e465a

Browse files
committed
fix(cache): retry
resolve #10
1 parent f6bb23b commit f4e465a

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

cache/src/cache-operator.ts

+32-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ export class CacheOperator<
3434

3535
private readonly op: (option: TOptions) => TTask;
3636
private readonly cache: Cache<TRes & CacheRes> = new Cache();
37+
/**
38+
* 正在处理的回调
39+
*/
3740
private readonly callbackMapList: { [key: string]: { success: Function[]; fail: Function[]; complete: Function[] } } = {};
41+
/**
42+
* 处理完的回调,待删除
43+
*/
44+
private readonly completeMapList: { [key: string]: Function[] } = {};
3845

3946
constructor(operator: (option: TOptions) => TTask, config?: Configuration<TRes, TOptions>) {
4047
this.op = operator;
@@ -92,15 +99,17 @@ export class CacheOperator<
9299
if (this.config.resultCondition(res)) {
93100
this.cache.set(key, res, this.config.expire);
94101
}
95-
this.callbackMapList[key].success.forEach((v) => { v(res); });
102+
this._getMapBeforeComplete(key).success
103+
.forEach((v) => { v(res); });
96104
},
97105
fail: (res: { errMsg: string }) => {
98-
this.callbackMapList[key].fail.forEach((v) => { v(res); });
106+
this._getMapBeforeComplete(key).fail
107+
.forEach((v) => { v(res); });
99108
},
100109
complete: (res: TRes) => {
101-
this.callbackMapList[key].complete.forEach((v) => { v(res); });
110+
this.completeMapList[key].forEach((v) => { v(res); });
102111
// tslint:disable-next-line: no-dynamic-delete
103-
delete this.callbackMapList[key];
112+
delete this.completeMapList[key];
104113
}
105114
};
106115
return this.op(data);
@@ -112,20 +121,37 @@ export class CacheOperator<
112121
if (options.success) {
113122
arrayRemove(this.callbackMapList[key].success, options.success);
114123
}
124+
const callbackList = [];
115125
if (options.fail) {
116126
arrayRemove(this.callbackMapList[key].fail, options.fail);
117-
options.fail({ errMsg: 'request:fail abort', cancel: true, source: CacheOperator.name });
127+
callbackList.push(options.fail);
118128
}
119129
if (options.complete) {
120-
options.complete({ errMsg: 'request:fail abort', cancel: true, source: CacheOperator.name });
121130
arrayRemove(this.callbackMapList[key].complete, options.complete);
131+
callbackList.push(options.complete);
122132
}
133+
const res = { errMsg: 'request:fail abort', cancel: true, source: CacheOperator.name };
134+
callbackList.forEach(f => { f(res); });
123135
}
124136
},
125137
onHeadersReceived: doNothing as TTask['onHeadersReceived'],
126138
onProgressUpdate: doNothing as TTask['onProgressUpdate']
127139
} as TTask;
128140
}
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+
}
129155
}
130156

131157
interface CacheRes {

0 commit comments

Comments
 (0)