Skip to content

Commit 3a0e948

Browse files
authored
fix: improve aborted handling (#416)
1 parent 8da741e commit 3a0e948

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"lint:fix": "eslint --cache --fix src test examples eslint.config.js tsdown.config.ts vitest.config.ts vitest.pretest.config.ts",
2727
"release": "bumpp package.json --commit --push --tag",
2828
"ci:pretest": "vitest --config vitest.pretest.config.ts run",
29-
"test": "vitest --retry=5 run",
30-
"test:coverage": "vitest --retry=5 --coverage run"
29+
"test": "vitest run",
30+
"test:coverage": "vitest --coverage run"
3131
},
3232
"main": "./dist/index.js",
3333
"module": "./dist/index.js",

src/bench.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ export class Bench extends EventTarget {
171171
remove (name: string): this {
172172
const task = this.getTask(name)
173173
if (task) {
174-
this.dispatchEvent(new BenchEvent('remove', task))
175174
this.#tasks.delete(name)
175+
this.dispatchEvent(new BenchEvent('remove', task))
176176
}
177177
return this
178178
}
@@ -181,10 +181,10 @@ export class Bench extends EventTarget {
181181
* reset tasks and remove their result
182182
*/
183183
reset (): void {
184-
this.dispatchEvent(new BenchEvent('reset'))
185184
for (const task of this.#tasks.values()) {
186185
task.reset()
187186
}
187+
this.dispatchEvent(new BenchEvent('reset'))
188188
}
189189

190190
/**

src/task.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ export class Task extends EventTarget {
5050
options?: RemoveEventListenerOptionsArgument
5151
) => void
5252

53-
/**
54-
* The number of times the task function has been executed
55-
*/
56-
runs = 0
57-
5853
get name (): string {
5954
return this.#name
6055
}
@@ -67,6 +62,16 @@ export class Task extends EventTarget {
6762
}
6863
}
6964

65+
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
99104

100105
/**
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
104109

105110
/**
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
112114

113115
constructor (bench: Bench, name: string, fn: Fn, fnOpts: FnOptions = {}) {
114116
super()
@@ -128,23 +130,31 @@ export class Task extends EventTarget {
128130
}
129131
}
130132

133+
this.reset(false)
134+
131135
if (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
}
138146

139147
if (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
}
149159

150160
/**
@@ -153,10 +163,10 @@ export class Task extends EventTarget {
153163
* @internal
154164
*/
155165
reset (emit = true): void {
156-
if (emit) this.dispatchEvent(new BenchEvent('reset', this))
157-
this.runs = 0
158-
166+
this.#runs = 0
159167
this.#result = this.#aborted ? abortedTaskResult : notStartedTaskResult
168+
169+
if (emit) this.dispatchEvent(new BenchEvent('reset', this))
160170
}
161171

162172
/**
@@ -469,6 +479,7 @@ export class Task extends EventTarget {
469479
}
470480

471481
#onAbort (): void {
482+
this.#aborted = true
472483
if (
473484
abortableStates.includes(this.#result.state as typeof abortableStates[number])
474485
) {
@@ -501,7 +512,7 @@ export class Task extends EventTarget {
501512
latencySamples?: number[]
502513
}): void {
503514
if (isValidSamples(latencySamples)) {
504-
this.runs = latencySamples.length
515+
this.#runs = latencySamples.length
505516

506517
sortSamples(latencySamples)
507518

test/basic-async.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect, test } from 'vitest'
33
import { Bench, hrtimeNow, now, type Task } from '../src'
44

55
// If running in CI, allow a bit more leeway for the mean value
6-
const maxMeanValue = process.env.CI ? 1025 : 1002
6+
const maxMeanValue = process.env.CI ? 1100 : 1002
77

88
test.each([['now()'], ['hrtimeNow()']])('%s basic (async)', async mode => {
99
const bench = new Bench({

test/basic-sync.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Bench, hrtimeNow, now, type Task } from '../src'
44
import { sleep } from './utils'
55

66
// If running in CI, allow a bit more leeway for the mean value
7-
const maxMeanValue = process.env.CI ? 1025 : 1002
7+
const maxMeanValue = process.env.CI ? 1100 : 1002
88

99
test.each([['now()'], ['hrtimeNow()']])('%s basic (sync)', mode => {
1010
const bench = new Bench({

0 commit comments

Comments
 (0)