fix!: improve BenchEvent, remove types `BenchEventsMap` and `TaskEven… · tinylibs/tinybench@7a8daed

@@ -3,8 +3,9 @@ import pLimit from 'p-limit'

33

import type {

44

AddEventListenerOptionsArgument,

55

BenchEvents,

6-

BenchEventsMap,

76

BenchOptions,

7+

EventListener,

8+

EventListenerObject,

89

Fn,

910

FnOptions,

1011

RemoveEventListenerOptionsArgument,

@@ -19,7 +20,7 @@ import {

1920

defaultMinimumWarmupTime,

2021

emptyFunction,

2122

} from './constants'

22-

import { createBenchEvent } from './event'

23+

import { BenchEvent } from './event'

2324

import { Task } from './task'

2425

import {

2526

defaultConvertTaskResultForConsoleTable,

@@ -34,6 +35,12 @@ import {

3435

* The Bench class keeps track of the benchmark tasks and controls them.

3536

*/

3637

export 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

*/

5461

readonly 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 {

115128

this.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 {

134147

if (!this.#tasks.has(name)) {

135148

const task = new Task(this, name, fn, fnOpts)

136149

this.#tasks.set(name, task)

137-

this.dispatchEvent(createBenchEvent('add', task))

150+

this.dispatchEvent(new BenchEvent('add', task))

138151

} else {

139152

throw new Error(`Task "${name}" already exists`)

140153

}

141154

return 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 {

166171

remove (name: string): this {

167172

const task = this.getTask(name)

168173

if (task) {

169-

this.dispatchEvent(createBenchEvent('remove', task))

174+

this.dispatchEvent(new BenchEvent('remove', task))

170175

this.#tasks.delete(name)

171176

}

172177

return 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

*/

186183

reset (): void {

187-

this.dispatchEvent(createBenchEvent('reset'))

184+

this.dispatchEvent(new BenchEvent('reset'))

188185

for (const task of this.#tasks.values()) {

189186

task.reset()

190187

}

@@ -199,15 +196,15 @@ export class Bench extends EventTarget {

199196

await this.#warmupTasks()

200197

}

201198

let values: Task[] = []

202-

this.dispatchEvent(createBenchEvent('start'))

199+

this.dispatchEvent(new BenchEvent('start'))

203200

if (this.concurrency === 'bench') {

204201

values = await this.#mapTasksConcurrently(task => task.run())

205202

} else {

206203

for (const task of this.#tasks.values()) {

207204

values.push(await task.run())

208205

}

209206

}

210-

this.dispatchEvent(createBenchEvent('complete'))

207+

this.dispatchEvent(new BenchEvent('complete'))

211208

return values

212209

}

213210

@@ -224,11 +221,11 @@ export class Bench extends EventTarget {

224221

this.#warmupTasksSync()

225222

}

226223

const values: Task[] = []

227-

this.dispatchEvent(createBenchEvent('start'))

224+

this.dispatchEvent(new BenchEvent('start'))

228225

for (const task of this.#tasks.values()) {

229226

values.push(task.runSync())

230227

}

231-

this.dispatchEvent(createBenchEvent('complete'))

228+

this.dispatchEvent(new BenchEvent('complete'))

232229

return values

233230

}

234231

@@ -249,6 +246,7 @@ export class Bench extends EventTarget {

249246

Stack: 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

*/

284282

async #warmupTasks (): Promise<void> {

285-

this.dispatchEvent(createBenchEvent('warmup'))

283+

this.dispatchEvent(new BenchEvent('warmup'))

286284

if (this.concurrency === 'bench') {

287285

await 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'))

300298

for (const task of this.#tasks.values()) {

301299

task.warmupSync()

302300

}