mentie 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -1
- package/modules/network.js +38 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal helper for fetch requests
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} options - Options for the signal
|
|
5
|
+
* @param {number} [options.timeout_ms] - Timeout in milliseconds
|
|
6
|
+
* @returns {Object} signal_data - Object containing fetch options and abort signal
|
|
7
|
+
* @returns {Object} signal_data.fetch_options - Options for usage with fetch requests
|
|
8
|
+
* @returns {AbortController} signal_data.controller - Raw abort controller
|
|
9
|
+
* @returns {Function} signal_data.abort_signal - Function to abort the request
|
|
10
|
+
* @returns {Function} signal_data.abort - Alias for abort_signal
|
|
11
|
+
* @returns {number} signal_data.timeout_id - Timeout ID
|
|
12
|
+
*/
|
|
13
|
+
export const timeout_signal = ( { timeout_ms }={} ) => {
|
|
14
|
+
|
|
15
|
+
// Request with timeout
|
|
16
|
+
const controller = new AbortController()
|
|
17
|
+
const timeout_id = timeout_ms && setTimeout( () => {
|
|
18
|
+
controller.abort()
|
|
19
|
+
}, timeout_ms )
|
|
20
|
+
|
|
21
|
+
const fetch_options = {
|
|
22
|
+
signal: controller.signal
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const abort = () => {
|
|
26
|
+
if( timeout_ms ) clearTimeout( timeout_id )
|
|
27
|
+
controller.abort()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
fetch_options,
|
|
32
|
+
controller,
|
|
33
|
+
abort_signal: abort,
|
|
34
|
+
abort,
|
|
35
|
+
timeout_id
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
}
|