solid-awesome-hooks
Version:
A collection of awesome hooks for solid-js
20 lines (19 loc) • 633 B
JavaScript
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;
};