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 const InvisibleCodec = baseCodecFrom(InvisibleDictionary);
export function doTimed<T>(message: string, callback: () => T): T { export function doTimed<T>(message: string, callback: () => T): T {
const hiddenId = InvisibleCodec.encode(v7()); if (import.meta.env.DEV) {
console.time(message + hiddenId); const hiddenId = InvisibleCodec.encode(v7());
try { console.time(message + hiddenId);
const output = callback(); try {
return output; const output = callback();
} finally { return output;
console.timeEnd(message + hiddenId); } finally {
console.timeEnd(message + hiddenId);
}
} else {
return callback();
} }
} }
export async function doTimedAsync<T>(message: string, callback: () => T): Promise<Awaited<T>> { export function doTimedAsync<T, R = T extends Promise<T> ? Promise<T> : T>(
const hiddenId = InvisibleCodec.encode(v7()); message: string,
console.time(message + hiddenId); callback: () => R,
try { throwError: boolean = false
const output = await callback(); ): R {
return output; if (import.meta.env.DEV) {
} finally { return new Promise(async (resolve, reject) => {
console.timeEnd(message + hiddenId); 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;
} }
} }