mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
28 lines
929 B
TypeScript
28 lines
929 B
TypeScript
import path from 'path';
|
|
import picocolors from 'picocolors';
|
|
|
|
type Formatter = (result: string) => string;
|
|
|
|
export function traceSync<T>(prefix: string, fn: () => T, timeFormatter: Formatter = picocolors.blue): T {
|
|
const start = Bun.nanoseconds();
|
|
const result = fn();
|
|
const end = Bun.nanoseconds();
|
|
console.log(`${timeFormatter(`[${((end - start) / 1e6).toFixed(3)}ms]`)} ${prefix}`);
|
|
return result;
|
|
}
|
|
traceSync.skip = <T>(_prefix: string, fn: () => T): T => fn();
|
|
|
|
export const traceAsync = async <T>(prefix: string, fn: () => Promise<T>, timeFormatter: Formatter = picocolors.blue): Promise<T> => {
|
|
const start = Bun.nanoseconds();
|
|
const result = await fn();
|
|
const end = Bun.nanoseconds();
|
|
console.log(`${timeFormatter(`[${((end - start) / 1e6).toFixed(3)}ms]`)} ${prefix}`);
|
|
return result;
|
|
};
|
|
|
|
export interface TaskResult {
|
|
readonly start: number,
|
|
readonly end: number,
|
|
readonly taskName: string
|
|
}
|