UNPKG

solid-awesome-hooks

Version:
20 lines (19 loc) 633 B
import { onCleanup, runWithOwner, getOwner } from "solid-js"; /** * Returns AbortController instance * Can be useful inside createResource * If there's no owner scope abort() won't be called */ export const useAbortController = ({ reason, fallbackOwner } = {}) => { const controller = new AbortController(); const owner = getOwner() ?? fallbackOwner; if (owner) { // register cleanup with owner runWithOwner(owner, () => { onCleanup(() => { runWithOwner(owner, () => controller.abort(reason)); }); }); } return controller; };