Skip to content

Commit

Permalink
avoid MaxListenersExceededWarning on AbortSignal objects
Browse files Browse the repository at this point in the history
Set the max to infinity, avoid cases where it might fire more than once
  • Loading branch information
isaacs committed Jun 1, 2024
1 parent 58f5fd5 commit 24e7b47
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setMaxListeners } from 'node:events'

/**
* Options that define the graph and how to traverse it
*/
Expand Down Expand Up @@ -59,7 +61,6 @@ export type DepResults<Node, Result> = Map<Node, Result | undefined>
*/
export interface RunnerOptionsSync<Node, Result = void>
extends RunnerOptions<Node, Result> {

/** Get a set of dependency nodes synchronously */
getDeps: (node: Node) => Node[]

Expand Down Expand Up @@ -145,6 +146,7 @@ export abstract class RunnerBase<
const ac = new AbortController()
this.from = from ?? this.constructor
this.abortController = ac
setMaxListeners(Infinity, ac.signal)
this.options = options
if (!options.graph.length) {
const er = new Error('no nodes provided to graph traversal', {
Expand All @@ -159,7 +161,10 @@ export abstract class RunnerBase<
this.failFast = options.failFast !== false
const { signal } = options
if (signal !== undefined) {
signal.addEventListener('abort', reason => ac.abort(reason))
signal.addEventListener('abort', reason => ac.abort(reason), {
once: true,
signal: ac.signal
})
}
}

Expand Down Expand Up @@ -199,10 +204,7 @@ export abstract class RunnerBase<
* Note that self-referential links are never considered, since they're
* by definition cyclical.
*/
route(
n: Node,
d: Node,
): undefined | [n: Node, ...path: Node[]] {
route(n: Node, d: Node): undefined | [n: Node, ...path: Node[]] {
const dependents = this.dependents.get(d)
if (!dependents?.has(n)) return undefined
const directDependents = this.directDependents.get(d)
Expand Down

0 comments on commit 24e7b47

Please sign in to comment.