-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathabort.ts
150 lines (146 loc) · 4.2 KB
/
abort.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { Wretch, WretchAddon, WretchErrorCallback, WretchResponseChain } from "../types.js"
export interface AbortWretch {
/**
* Associates a custom controller with the request.
*
* Useful when you need to use
* your own AbortController, otherwise wretch will create a new controller itself.
*
* ```js
* const controller = new AbortController()
*
* // Associates the same controller with multiple requests
* wretch("url1")
* .addon(AbortAddon())
* .signal(controller)
* .get()
* .json()
* wretch("url2")
* .addon(AbortAddon())
* .signal(controller)
* .get()
* .json()
*
* // Aborts both requests
* controller.abort()
* ```
*
* @param controller - An instance of AbortController
*/
signal: <T extends AbortWretch, C, R>(this: T & Wretch<T, C, R>, controller: AbortController) => this
}
export interface AbortResolver {
/**
* Aborts the request after a fixed time.
*
* If you use a custom AbortController associated with the request, pass it as the second argument.
*
* ```js
* // 1 second timeout
* wretch("...").addon(AbortAddon()).get().setTimeout(1000).json(_ =>
* // will not be called if the request timeouts
* )
* ```
*
* @param time - Time in milliseconds
* @param controller - An instance of AbortController
*/
setTimeout: <T, C extends AbortResolver, R>(this: C & WretchResponseChain<T, C, R>, time: number, controller?: AbortController) => this
/**
* Returns the provided or generated AbortController plus the wretch response chain as a pair.
*
* ```js
* // We need the controller outside the chain
* const [c, w] = wretch("url")
* .addon(AbortAddon())
* .get()
* .controller()
*
* // Resume with the chain
* w.onAbort(_ => console.log("ouch")).json()
*
* // Later on…
* c.abort()
* ```
*/
controller: <T, C extends AbortResolver, R>(this: C & WretchResponseChain<T, C, R>) => [any, this]
/**
* Catches an AbortError and performs a callback.
*/
onAbort: <T, C extends AbortResolver, R>(this: C & WretchResponseChain<T, C, R>, cb: WretchErrorCallback<T, C, R>) => this
}
/**
* Adds the ability to abort requests using AbortController and signals under the hood.
*
*
* _Only compatible with browsers that support
* [AbortControllers](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
* Otherwise, you could use a (partial)
* [polyfill](https://www.npmjs.com/package/abortcontroller-polyfill)._
*
* ```js
* import AbortAddon from "wretch/addons/abort"
*
* const [c, w] = wretch("...")
* .addon(AbortAddon())
* .get()
* .onAbort((_) => console.log("Aborted !"))
* .controller();
*
* w.text((_) => console.log("should never be called"));
* c.abort();
*
* // Or :
*
* const controller = new AbortController();
*
* wretch("...")
* .addon(AbortAddon())
* .signal(controller)
* .get()
* .onAbort((_) => console.log("Aborted !"))
* .text((_) => console.log("should never be called"));
*
* controller.abort();
* ```
*/
const abort: () => WretchAddon<AbortWretch, AbortResolver> = () => {
return {
beforeRequest(wretch, options, state) {
const fetchController = wretch._config.polyfill("AbortController", false, true)
if (!options["signal"] && fetchController) {
options["signal"] = fetchController.signal
}
const timeout = {
ref: null,
clear() {
if (timeout.ref) {
clearTimeout(timeout.ref)
timeout.ref = null
}
}
}
state.abort = {
timeout,
fetchController
}
return wretch
},
wretch: {
signal(controller) {
return { ...this, _options: { ...this._options, signal: controller.signal } }
},
},
resolver: {
setTimeout(time, controller = this._sharedState.abort.fetchController) {
const { timeout } = this._sharedState.abort
timeout.clear()
timeout.ref = setTimeout(() => controller.abort(), time)
return this
},
controller() { return [this._sharedState.abort.fetchController, this] },
onAbort(cb) { return this.error("AbortError", cb) }
},
}
}
export default abort