Skip to content
Myr

clock.ts

This file

Path

apps/web/src/dev/clock.ts

96 lines · 2.9 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

The walk stopped after 200 commits: lines older than 61ef4c0 point at it.

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 1why, one step back
1import { Clock, Context, DateTime, Duration, Effect, Layer } from "effect";

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

lines 2–4why, one step back
2
3export class DevClock extends Context.Service<4 DevClock,

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 5–8why, one step back
5 {
6 readonly place: (at: DateTime.Utc) => DateTime.Utc;7 readonly set: (at: DateTime.Utc) => Effect.Effect<void>;8 readonly run: Effect.Effect<void>;

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

line 9why, one step back
9 /** On a running clock, the present; on one standing still, a minute after where it stands, so each thing reads as after all before it. */

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 10–11why, one step back
10 readonly step: Effect.Effect<DateTime.Utc>;
11 }

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

lines 12–22why, one step back
12>()("web/dev/DevClock") {}
1314const NANOS_PER_MILLI = 1_000_000n;1516export const devStart: DateTime.Utc = DateTime.toUtc(17 DateTime.makeUnsafe("2026-10-07T09:00:00Z")18);1920export const devMinute = (minutes: number): DateTime.Utc =>21 DateTime.add(devStart, { minutes });22

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 23–32why, one step back
23export const startEndingNow = (
24 end: DateTime.Utc25): Effect.Effect<DateTime.Utc> =>26 Effect.map(DateTime.now, (now) =>27 DateTime.subtractDuration(28 DateTime.startOf(now, "minute"),29 DateTime.distance(devStart, end)30 )31 );32

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

lines 33–36why, one step back
33export const devClockAt = (start: DateTime.Utc): Layer.Layer<DevClock> =>
34 Layer.effectContext(35 Effect.gen(function* () {36 const real = yield* Clock.Clock;

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 37why, one step back
37 const offset = DateTime.distance(devStart, start);

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

line 38why, one step back
38

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 39why, one step back
39 const place = (at: DateTime.Utc) => DateTime.addDuration(at, offset);

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

line 40why, one step back
40

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 41–60why, one step back
41 let stood = DateTime.toEpochMillis(start);
42 let runningSince: bigint | undefined;4344 const elapsedNanos = () =>45 runningSince === undefined46 ? 0n47 : real.monotonicTimeNanosUnsafe() - runningSince;4849 const nanos = () => BigInt(stood) * NANOS_PER_MILLI + elapsedNanos();5051 const millis = () => Number(nanos() / NANOS_PER_MILLI);5253 const standAt = (at: number) => {54 stood = at;5556 if (runningSince !== undefined) {57 runningSince = real.monotonicTimeNanosUnsafe();58 }59 };60

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

line 61why, one step back
61 const clock: Clock.Clock = {

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 62–63why, one step back
62 currentTimeMillis: Effect.sync(millis),
63 currentTimeMillisUnsafe: millis,

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

lines 64–72why, one step back
64 currentTimeNanos: Effect.sync(nanos),
65 currentTimeNanosUnsafe: nanos,66 // Elapsed time is measured, never seeded.67 monotonicTimeNanos: real.monotonicTimeNanos,68 monotonicTimeNanosUnsafe: () => real.monotonicTimeNanosUnsafe(),69 sleep: (duration) => real.sleep(duration),70 };7172 const devClock = DevClock.of({

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 73–76why, one step back
73 place,
74 run: Effect.sync(() => {75 runningSince ??= real.monotonicTimeNanosUnsafe();76 }),

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

lines 77–78why, one step back
77 set: (at) =>
78 Effect.sync(() => {

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 79why, one step back
79 standAt(DateTime.toEpochMillis(place(at)));

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

line 80why, one step back
80 }),

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 81–89why, one step back
81 step: Effect.sync(() => {
82 if (runningSince !== undefined) {83 return DateTime.toUtc(DateTime.makeUnsafe(millis()));84 }8586 standAt(millis() + Duration.toMillis(Duration.minutes(1)));8788 return DateTime.toUtc(DateTime.makeUnsafe(stood));89 }),

Walk the swarm take's pre-flight: Amp updated, Claude Code's updater off, pell's key, a fresh herdr sessionoscabrielbefore adoption61ef4c0

lines 90–96why, one step back
90 });
9192 return Context.make(DevClock, devClock).pipe(93 Context.add(Clock.Clock, clock)94 );95 })96 );