From 61c8eb297f6e88886cbb081a43c9acecf0ec62bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 17 Oct 2024 14:40:21 +0200 Subject: [PATCH] fix: avoid duplicate benchmark run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/benchmark.js | 25 ++++++++++++++----------- src/index.d.ts | 9 +++------ src/lib.js | 5 +++-- src/utils.js | 4 ++-- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/benchmark.js b/src/benchmark.js index 933fc12..a71fc40 100644 --- a/src/benchmark.js +++ b/src/benchmark.js @@ -29,7 +29,12 @@ import { } from './reporter/terminal/index.js' import { runtime } from './runtime.js' import { now } from './time.js' -import { isAsyncFnResource, isFunction, isObject } from './utils.js' +import { + isAsyncFnResource, + isAsyncFunction, + isFunction, + isObject, +} from './utils.js' let groupName = null const groups = new Map() @@ -75,6 +80,10 @@ export function group(name, cb = undefined) { } if (!isFunction(cb)) throw new TypeError(`expected function, got ${cb.constructor.name}`) + if (isAsyncFunction(cb)) + throw new TypeError( + `expected synchronous function, got asynchronous function ${cb.constructor.name}` + ) if (isObject(name)) { if (name.name != null && 'string' !== typeof name.name) throw new TypeError( @@ -127,14 +136,8 @@ export function group(name, cb = undefined) { before: name.before ?? emptyFunction, after: name.after ?? emptyFunction, }) - if (isAsyncFnResource(cb)) { - cb().then(() => { - groupName = null - }) - } else { - cb() - groupName = null - } + cb() + groupName = null } /** @@ -362,7 +365,7 @@ export async function run(opts = {}) { if (once) log('') log(groupHeader(groupName, opts)) } - isAsyncFnResource(groupOpts.before) + isAsyncFunction(groupOpts.before) ? await groupOpts.before() : groupOpts.before() @@ -372,7 +375,7 @@ export async function run(opts = {}) { opts, groupOpts ) - isAsyncFnResource(groupOpts.after) + isAsyncFunction(groupOpts.after) ? await groupOpts.after() : groupOpts.after() } diff --git a/src/index.d.ts b/src/index.d.ts index f23dc7d..13f6710 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9,12 +9,9 @@ export type GroupOptions = { after?: () => void | Promise } -export function group(cb: () => void | Promise): void -export function group(name: string, cb: () => void | Promise): void -export function group( - options: GroupOptions, - cb: () => void | Promise -): void +export function group(cb: () => void): void +export function group(name: string, cb: () => void): void +export function group(options: GroupOptions, cb: () => void): void export type BenchmarkOptions = { samples?: number // minimum number of samples diff --git a/src/lib.js b/src/lib.js index 74d701f..085f198 100644 --- a/src/lib.js +++ b/src/lib.js @@ -22,6 +22,7 @@ import { AsyncFunction, checkDividend, isAsyncFnResource, + isAsyncFunction, isFunction, isObject, } from './utils.js' @@ -216,8 +217,8 @@ export async function measure(fn, opts = {}) { opts.before = opts.before ?? emptyFunction opts.after = opts.after ?? emptyFunction - const asyncBefore = isAsyncFnResource(opts.before) - const asyncAfter = isAsyncFnResource(opts.after) + const asyncBefore = isAsyncFunction(opts.before) + const asyncAfter = isAsyncFunction(opts.after) const asyncFunction = opts.async || asyncBefore || asyncAfter diff --git a/src/utils.js b/src/utils.js index 39682aa..41f0aad 100644 --- a/src/utils.js +++ b/src/utils.js @@ -27,7 +27,7 @@ export const AsyncFunction = (async () => {}).constructor * @param {Function} fn the function to check * @returns {Boolean} true if the function is an async function */ -const isAsyncFunction = fn => { +export const isAsyncFunction = fn => { return AsyncFunction === fn?.constructor } @@ -49,7 +49,7 @@ export const isAsyncFnResource = fn => { const promiseLike = isPromiseLike(fnCall) if (promiseLike) { // silence promise rejection - fnCall.catch(() => {}) + fnCall.then(() => {}).catch(() => {}) } return promiseLike } catch {