@abortor/core
v0.1.4
Published
Hierarchical cancellation scopes for modern JavaScript runtimes.
Downloads
15
Maintainers
Readme
@abortor/core
Hierarchical cancellation scopes for modern JavaScript runtimes.
Built on top of the platform's native AbortController / AbortSignal, @abortor/core makes cancellation composable, structured, and developer‑friendly.
Features
- Hierarchical cancellation: disposing a parent scope cancels all of its descendants.
- Scoped async: run work inside a
scope.run(fn)and it automatically tracks lifetime. - Fetch integration:
scope.fetch()threads the scope's signal intofetchcalls. - Timeouts & composition: use
scope.timeout(ms)or combine signals withcomposeAny(). - Diagnostics: dev‑mode warnings, strict mode checks, and perf marks for profiling.
- Fallbacks: uses
AbortSignal.any()andAbortSignal.timeout()if available; provides shims otherwise.
Installation
npm install @abortor/core
# or
pnpm add @abortor/core
yarn add @abortor/coreRequires Node.js 18+ or modern browsers with Abort APIs.
Quick Start
import { scope } from "@abortor/core";
// Create a new scope (optionally label it)
const s = scope({ label: "SearchBox" });
// Run async work inside the scope
const result = await s.run(async (sc) => {
const res = await sc.fetch("https://jsonplaceholder.typicode.com/users");
return res.json();
});
// Later, cancel everything under this scope
s.dispose("navigated away");API Overview
scope(opts?)
Creates a new cancellation scope.
const s = scope({ label: string, parent: Scope });A Scope exposes:
signal: AbortSignal— propagate into APIs likefetchrun(fn)— run async work bound to the scopechild(opts?)— create a nested scopedispose(reason?)— cancel this scope and all childrenfetch(input, init?)— likefetch, but auto‑uses the scope’s signaltimeout(ms, reason?)— create a timeout signal combined with this scoperace(promises)/all(promises)— helpers that cancel correctly
withScope(fn, opts?)
Utility to create a scope, run work, and auto‑dispose.
import { withScope } from "@abortor/core";
await withScope(async (sc) => {
await sc.fetch("/api/data");
}); // auto‑disposed hereDiagnostics
import { configureDiagnostics } from "@abortor/core";
configureDiagnostics({
warnings: true,
strict: false,
perf: false,
});- warnings: console.warn on leaks
- strict: throw if missing signal usage
- perf: mark/measure scopes in PerformanceTimeline
Utilities
composeAny(signals: AbortSignal[]): AbortSignal
Combine multiple signals; aborts when any does.
withTimeout(ms: number, parent?: AbortSignal, reason?: string): AbortSignal
Create a timeout signal, optionally combined with a parent.
Related packages
Also see:
@abortor/angular – Angular-specific integration with component lifecycle-aware cancellation.
@abortor/react — React hooks and utilities for cancellation-aware components (requires React 18+).
