refactor(doTimed): refactor error handling and skip timing when not in dev mode

This commit is contained in:
Daniel 2026-03-19 13:58:26 -05:00
parent b7cac5724d
commit fd06160f7e

View file

@ -4,23 +4,44 @@ import { v7 } from 'uuid';
export const InvisibleCodec = baseCodecFrom(InvisibleDictionary);
export function doTimed<T>(message: string, callback: () => T): T {
const hiddenId = InvisibleCodec.encode(v7());
console.time(message + hiddenId);
try {
const output = callback();
return output;
} finally {
console.timeEnd(message + hiddenId);
if (import.meta.env.DEV) {
const hiddenId = InvisibleCodec.encode(v7());
console.time(message + hiddenId);
try {
const output = callback();
return output;
} finally {
console.timeEnd(message + hiddenId);
}
} else {
return callback();
}
}
export async function doTimedAsync<T>(message: string, callback: () => T): Promise<Awaited<T>> {
const hiddenId = InvisibleCodec.encode(v7());
console.time(message + hiddenId);
try {
const output = await callback();
return output;
} finally {
console.timeEnd(message + hiddenId);
export function doTimedAsync<T, R = T extends Promise<T> ? Promise<T> : T>(
message: string,
callback: () => R,
throwError: boolean = false
): R {
if (import.meta.env.DEV) {
return new Promise(async (resolve, reject) => {
const hiddenId = InvisibleCodec.encode(v7());
console.time(message + hiddenId);
try {
const output = await callback();
resolve(output);
} catch (err) {
console.error(`Error in timed operation "${message}":`, err);
if (throwError) {
reject(err);
} else {
resolve(undefined as R);
}
} finally {
console.timeEnd(message + hiddenId);
}
}) as R;
} else {
return callback() as R;
}
}