Skip to content

Commit

Permalink
fix: update abortcontroller/abortSignal for use in Reactive Native
Browse files Browse the repository at this point in the history
no access to React Native, instead tested for existence of AbortSignal.timeout,
if there, use it; otherwise, use a general timeout on the AbortController

should fix #92
  • Loading branch information
courcelan committed Aug 12, 2024
1 parent d91d47f commit b111808
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
19 changes: 17 additions & 2 deletions lib/useButter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function useButter(type, butterConfig) {
const friendlyType = type.replace(type[0], type[0].toUpperCase());

const {
abortOnTimeout,
applyRequestUrlForErrorMessages,
cancelRequest,
cleanup,
Expand Down Expand Up @@ -77,7 +78,13 @@ export default function useButter(type, butterConfig) {
try {
// use static abortSignal timeout functionality to relay a cancelation
// when timeout is reached
const timeoutSignal = AbortSignal.timeout(config.timeout);
// if AbortSignal.timeout doestn't exist (React Native), use a regular timeotu
const timeoutSignal = AbortSignal.timeout
? AbortSignal.timeout(config.timeout)
: setTimeout(
() => abortOnTimeout(config.timeout),
config.timeout
)

const response = await fetch(
`${apiEndpoint}?${new URLSearchParams(params)}`,
Expand All @@ -87,10 +94,18 @@ export default function useButter(type, butterConfig) {
headers,
// use either the above timeout or
// the explicit AbortController cancelRequest to cancel request
signal: AbortSignal.any([signal, timeoutSignal])
signal: AbortSignal.timeout
? AbortSignal.any([signal, timeoutSignal])
: signal
}
);

if (!AbortSignal.timeout) {
// if we are running on a regular timout,
// clear it after its use
clearTimeout(timeoutSignal)
}

cleanup();

if (response.status !== 200) {
Expand Down
19 changes: 17 additions & 2 deletions lib/utils/useFetchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,28 @@ export default function useFetchController(type) {
* @function cancelRequest
* @returns {Promise<void>} A promise that resolves when the request has been aborted.
*/
async function cancelRequest() {
await controller.abort(
function cancelRequest() {
controller.abort(
generateErrorMessage("Request cancelled")
);

return;
}

/**
* Aborts the current request due to a timeout.
*
* @param {number} timeout - The timeout duration in milliseconds after which the request was aborted.
* @returns {void}
*/
function abortOnTimeout(timeout) {
controller.abort(
generateErrorMessage(`Request timed out after ${timeout}ms.`)
);

return;
}

/**
* Cleans up resources by nullifying the AbortController to prevent memory leaks.
*/
Expand Down Expand Up @@ -88,6 +102,7 @@ export default function useFetchController(type) {
}

return {
abortOnTimeout,
applyRequestUrlForErrorMessages,
cancelRequest,
cleanup,
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/butter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b111808

Please sign in to comment.