@thepassle/app-tools
Version:
Collection of tools I regularly use to build apps. Maybe they're useful to somebody else. Maybe not. Most of these are thin wrappers around native API's, like the native `<dialog>` element, `fetch` API, and `URLPattern`.
38 lines (33 loc) • 874 B
JavaScript
/**
* @returns {import('../index.js').Plugin}
*/
export function abortPlugin() {
let requestId;
const requests = new Map();
return {
name: "abort",
beforeFetch: (meta) => {
const { method, url } = meta;
requestId = `${method}:${url}`;
if (requests.has(requestId)) {
const request = requests.get(requestId);
request.abort();
}
requests.set(requestId, new AbortController());
return {
...meta,
opts: {
...meta.opts,
signal: requests.get(requestId).signal,
},
};
},
afterFetch: ({ response }) => {
requests.delete(requestId);
return response;
},
// return true if an error should throw, return false if an error should be ignored
handleError: ({ name }) => name !== "AbortError",
};
}
export const abort = abortPlugin();