fix!: improve BenchEvent, remove types `BenchEventsMap` and `TaskEven… · tinylibs/tinybench@7a8daed
@@ -3,8 +3,9 @@ import pLimit from 'p-limit'
33import type {
44AddEventListenerOptionsArgument,
55BenchEvents,
6-BenchEventsMap,
76BenchOptions,
7+EventListener,
8+EventListenerObject,
89Fn,
910FnOptions,
1011RemoveEventListenerOptionsArgument,
@@ -19,7 +20,7 @@ import {
1920defaultMinimumWarmupTime,
2021emptyFunction,
2122} from './constants'
22-import { createBenchEvent } from './event'
23+import { BenchEvent } from './event'
2324import { Task } from './task'
2425import {
2526defaultConvertTaskResultForConsoleTable,
@@ -34,6 +35,12 @@ import {
3435 * The Bench class keeps track of the benchmark tasks and controls them.
3536 */
3637export class Bench extends EventTarget {
38+declare addEventListener: <K extends BenchEvents>(
39+type: K,
40+listener: EventListener<K> | EventListenerObject<K> | null,
41+options?: AddEventListenerOptionsArgument
42+) => void
43+3744/**
3845 * Executes tasks concurrently based on the specified concurrency mode.
3946 *
@@ -53,6 +60,12 @@ export class Bench extends EventTarget {
5360 */
5461readonly opts: Readonly<ResolvedBenchOptions>
556263+declare removeEventListener: <K extends BenchEvents>(
64+type: K,
65+listener: EventListener<K> | EventListenerObject<K> | null,
66+options?: RemoveEventListenerOptionsArgument
67+) => void
68+5669/**
5770 * The JavaScript runtime environment.
5871 */
@@ -115,7 +128,7 @@ export class Bench extends EventTarget {
115128this.opts.signal.addEventListener(
116129'abort',
117130() => {
118-this.dispatchEvent(createBenchEvent('abort'))
131+this.dispatchEvent(new BenchEvent('abort'))
119132},
120133{ once: true }
121134)
@@ -134,21 +147,13 @@ export class Bench extends EventTarget {
134147if (!this.#tasks.has(name)) {
135148const task = new Task(this, name, fn, fnOpts)
136149this.#tasks.set(name, task)
137-this.dispatchEvent(createBenchEvent('add', task))
150+this.dispatchEvent(new BenchEvent('add', task))
138151} else {
139152throw new Error(`Task "${name}" already exists`)
140153}
141154return this
142155}
143156144-override addEventListener<K extends BenchEvents>(
145-type: K,
146-listener: BenchEventsMap[K],
147-options?: AddEventListenerOptionsArgument
148-): void {
149-super.addEventListener(type, listener, options)
150-}
151-152157/**
153158 * get a task based on the task name
154159 * @param name - the task name
@@ -166,25 +171,17 @@ export class Bench extends EventTarget {
166171remove (name: string): this {
167172const task = this.getTask(name)
168173if (task) {
169-this.dispatchEvent(createBenchEvent('remove', task))
174+this.dispatchEvent(new BenchEvent('remove', task))
170175this.#tasks.delete(name)
171176}
172177return this
173178}
174179175-override removeEventListener<K extends BenchEvents>(
176-type: K,
177-listener: BenchEventsMap[K],
178-options?: RemoveEventListenerOptionsArgument
179-): void {
180-super.removeEventListener(type, listener, options)
181-}
182-183180/**
184181 * reset tasks and remove their result
185182 */
186183reset (): void {
187-this.dispatchEvent(createBenchEvent('reset'))
184+this.dispatchEvent(new BenchEvent('reset'))
188185for (const task of this.#tasks.values()) {
189186task.reset()
190187}
@@ -199,15 +196,15 @@ export class Bench extends EventTarget {
199196await this.#warmupTasks()
200197}
201198let values: Task[] = []
202-this.dispatchEvent(createBenchEvent('start'))
199+this.dispatchEvent(new BenchEvent('start'))
203200if (this.concurrency === 'bench') {
204201values = await this.#mapTasksConcurrently(task => task.run())
205202} else {
206203for (const task of this.#tasks.values()) {
207204values.push(await task.run())
208205}
209206}
210-this.dispatchEvent(createBenchEvent('complete'))
207+this.dispatchEvent(new BenchEvent('complete'))
211208return values
212209}
213210@@ -224,11 +221,11 @@ export class Bench extends EventTarget {
224221this.#warmupTasksSync()
225222}
226223const values: Task[] = []
227-this.dispatchEvent(createBenchEvent('start'))
224+this.dispatchEvent(new BenchEvent('start'))
228225for (const task of this.#tasks.values()) {
229226values.push(task.runSync())
230227}
231-this.dispatchEvent(createBenchEvent('complete'))
228+this.dispatchEvent(new BenchEvent('complete'))
232229return values
233230}
234231@@ -249,6 +246,7 @@ export class Bench extends EventTarget {
249246Stack: task.result.error.stack,
250247}
251248 : convert(task)
249+/* eslint-enable perfectionist/sort-objects */
252250})
253251}
254252@@ -282,7 +280,7 @@ export class Bench extends EventTarget {
282280 * warmup the benchmark tasks.
283281 */
284282async #warmupTasks (): Promise<void> {
285-this.dispatchEvent(createBenchEvent('warmup'))
283+this.dispatchEvent(new BenchEvent('warmup'))
286284if (this.concurrency === 'bench') {
287285await this.#mapTasksConcurrently(task => task.warmup())
288286} else {
@@ -296,7 +294,7 @@ export class Bench extends EventTarget {
296294 * warmup the benchmark tasks (sync version)
297295 */
298296 #warmupTasksSync (): void {
299-this.dispatchEvent(createBenchEvent('warmup'))
297+this.dispatchEvent(new BenchEvent('warmup'))
300298for (const task of this.#tasks.values()) {
301299task.warmupSync()
302300}