1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { Clock, Duration, Effect } from "npm:effect";
// Make a string like 3.1s or 123ms from a Duration
const toShortTimeString = (d: Duration.Duration): string =>
Duration.greaterThanOrEqualTo(d, Duration.seconds(90))
? `${(Duration.toSeconds(d) / 60).toFixed(1)}m`
: Duration.greaterThanOrEqualTo(d, Duration.seconds(1))
? `${Duration.toSeconds(d).toFixed(1)}s`
: `${Duration.toMillis(d)}ms`;
export function withTimeLog<A, E, C>(
label: string,
): (self: Effect.Effect<A, E, C>) => Effect.Effect<A, E, C> {
return (self) =>
Effect.gen(function*() {
const start = yield* Clock.currentTimeMillis;
const result = yield* self;
const elapsed = Duration.millis((yield* Clock.currentTimeMillis) - start);
const elapsedDisplay = toShortTimeString(elapsed);
yield* Effect.logDebug(`${label}: ${elapsedDisplay}`);
return result;
});
}