fix: improve aborted handling (#416) · tinylibs/tinybench@3a0e948
@@ -50,11 +50,6 @@ export class Task extends EventTarget {
5050options?: RemoveEventListenerOptionsArgument
5151) => void
525253-/**
54- * The number of times the task function has been executed
55- */
56-runs = 0
57-5853get name (): string {
5954return this.#name
6055}
@@ -67,6 +62,16 @@ export class Task extends EventTarget {
6762}
6863}
696465+get runs (): number {
66+return this.#runs
67+}
68+69+/**
70+ * Check if either our signal or the bench-level signal is aborted
71+ * `true` if either signal is aborted
72+ */
73+ #aborted = false
74+7075/**
7176 * The task asynchronous status
7277 */
@@ -98,17 +103,14 @@ export class Task extends EventTarget {
98103 #result: TaskResult = notStartedTaskResult
99104100105/**
101- * The task-level abort signal
106+ * The number of times the task function has been executed
102107 */
103-readonly #signal: AbortSignal | undefined
108+#runs = 0
104109105110/**
106- * Check if either our signal or the bench-level signal is aborted
107- * @returns `true` if either signal is aborted
111+ * The task-level abort signal
108112 */
109-get #aborted (): boolean {
110-return this.#signal?.aborted === true || this.#bench.opts.signal?.aborted === true
111-}
113+readonly #signal: AbortSignal | undefined
112114113115constructor (bench: Bench, name: string, fn: Fn, fnOpts: FnOptions = {}) {
114116super()
@@ -128,23 +130,31 @@ export class Task extends EventTarget {
128130}
129131}
130132133+this.reset(false)
134+131135if (this.#signal) {
132-this.#signal.addEventListener(
133-'abort',
134-this.#onAbort.bind(this),
135-{ once: true }
136-)
136+if (this.#signal.aborted) {
137+this.#onAbort()
138+} else {
139+this.#signal.addEventListener(
140+'abort',
141+this.#onAbort.bind(this),
142+{ once: true }
143+)
144+}
137145}
138146139147if (this.#bench.opts.signal) {
140-this.#bench.opts.signal.addEventListener(
141-'abort',
142-this.#onAbort.bind(this),
143-{ once: true }
144-)
148+if (this.#bench.opts.signal.aborted) {
149+this.#onAbort()
150+} else {
151+this.#bench.opts.signal.addEventListener(
152+'abort',
153+this.#onAbort.bind(this),
154+{ once: true }
155+)
156+}
145157}
146-147-this.reset(false)
148158}
149159150160/**
@@ -153,10 +163,10 @@ export class Task extends EventTarget {
153163 * @internal
154164 */
155165reset (emit = true): void {
156-if (emit) this.dispatchEvent(new BenchEvent('reset', this))
157-this.runs = 0
158-166+this.#runs = 0
159167this.#result = this.#aborted ? abortedTaskResult : notStartedTaskResult
168+169+if (emit) this.dispatchEvent(new BenchEvent('reset', this))
160170}
161171162172/**
@@ -469,6 +479,7 @@ export class Task extends EventTarget {
469479}
470480471481 #onAbort (): void {
482+this.#aborted = true
472483if (
473484abortableStates.includes(this.#result.state as typeof abortableStates[number])
474485) {
@@ -501,7 +512,7 @@ export class Task extends EventTarget {
501512latencySamples?: number[]
502513}): void {
503514if (isValidSamples(latencySamples)) {
504-this.runs = latencySamples.length
515+this.#runs = latencySamples.length
505516506517sortSamples(latencySamples)
507518