UNPKG

thorish

Version:

This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.

46 lines (45 loc) 1.57 kB
type SignalFunc = (signal: AbortSignal) => void | Promise<void>; type HaltFunc = (signal: AbortSignal, resume: AbortSignal) => void | Promise<void>; /** * Manages a group of signals to create a unified signal that has the super lifecycle of all of them. */ export declare class CGroup { private shutdownCause; private controller?; private active; private tasks; private readonly halts; private resumeController?; private resumeStart?; /** * Adds a signal to this group. * Returns `true` if the signal was not already aborted and was added to the active set. */ add(signal: AbortSignal): boolean; /** * Starts the group and returns its {@link AbortSignal}. * If no valid signals were added, this will be immediately aborted. */ start(): AbortSignal; /** * Ensures the group has started, then waits until the group's signal is aborted. */ wait(): Promise<void>; /** * Runs the given function as part of this group. * It will only start after `start()` has been called. * Any returned error will cancel the group's signal. */ go(fn: SignalFunc): boolean; /** * Registers a function to run when the group is about to shut down. * It is passed the group's signal and a "resume" signal which is aborted if the group restarts. * Any returned error will cancel the group's signal. */ halt(fn: HaltFunc): boolean; /** * Helper to execute a task and handle its potential error. */ private runTask; } export {};