UNPKG

@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`.

37 lines (33 loc) 852 B
/** * @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: (res) => { requests.delete(requestId); return res; }, // return true if an error should throw, return false if an error should be ignored handleError: ({name}) => name !== 'AbortError' } } export const abort = abortPlugin();