-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.source.js
83 lines (53 loc) · 2.19 KB
/
index.source.js
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
(function(win) {
let oldFetch = fetch;
class ModificationFetch {
constructor(...opt) {
this.opt = opt;
this.init()
}
init() {
this.oldFetchPromise = oldFetch(...this.opt);
this.oldFetchPromise.abort = this.abort.bind(this.oldFetchPromise);
this.oldThen = this.oldFetchPromise.then;
this.oldFetchPromise.then = this.then.bind(this.oldFetchPromise, this.oldFetchPromise, this.oldFetchPromise, this.then, this.abort,this.oldThen);
}
then(oldFetchPromise, curFetchPromise, then, abort, oldThen, resFn = () => {}, rejFn = () => {}) {
let afterPromise = oldThen.call(curFetchPromise,(...arg) => {
oldFetchPromise.abort = abort.bind(afterPromise); //把第一个promise的abort上下文指向下一个promise
if (this.__abort) afterPromise.__abort = this.__abort; // 传递 abort
if (!this.__abort) return resFn(...arg); //没阻断
}, (...arg) => {
oldFetchPromise.abort = abort.bind(afterPromise); //把第一个promise的abort上下文指向下一个promise
if (this.__abort) afterPromise.__abort = this.__abort; // 传递 abort
if (!this.__abort) return rejFn(...arg);
});
afterPromise.abort = abort.bind(afterPromise);
afterPromise.then = then.bind(afterPromise, oldFetchPromise, afterPromise, then, abort, oldThen);
return afterPromise
}
abort() {
this.__abort = true;
}
getFetch() {
return this.oldFetchPromise
}
}
let cacheFetch = [];
win.fetch = (...opt) => {
let modificationFetch = new ModificationFetch(...opt);
let curFetchPromise = modificationFetch.getFetch();
cacheFetch.push(curFetchPromise);
return curFetchPromise;
};
win.fetch.abort = (abortNum = 10) => {
cacheFetch
.splice(-abortNum)
.forEach((item) => {
item.abort();
});
cacheFetch = [];
}
for (let s in oldFetch) {
fetch[s] = oldFetch[s];
}
})(window);