Skip to content
Myr

clock.ts

This file

Path

apps/web/src/dev/clock.ts

135 lines · 5.0 KiB · TypeScript

Last changed by

Merge dev/live-clock into mainoscabrielbefore adoption923d465

History of this file

Take it

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 1–2why, one step back
1/**
2 * The dev world's time: one clock that every part of the world reads, as a

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 3–17why, one step back
3 * test's TestClock is. The seed and the fixtures are written against one
4 * fixed calendar, which starts at `devStart`: each step names its instant5 * on it (`devMinute`, or a fixture's date), and the seed sets the clock6 * there step by step. A world places that calendar on time by one offset,7 * the distance from `devStart` to where the world starts, so every instant8 * it holds is the calendar's moved by the same amount and the gaps between9 * them are as written.10 *11 * A world built for a test starts at `devStart` and its clock stands still12 * where the seed left it, so every timestamp is the same on every run. The13 * running dev server (`backend.ts`) starts its world so that the seed's14 * last instant is the minute it starts in (`startEndingNow`), and its clock15 * runs on from there in real time (`run`), so a page in a browser reads a16 * deadline as the seed meant it and sees it come. Elapsed time and sleeping,17 * which nothing in the world does, are always real.

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

line 18why, one step back
18 */

Merge dev/live-clock into mainoscabrielbefore adoption923d465

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

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

line 20why, one step back
20

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 21why, one step back
21/** The dev world's clock: where its calendar stands, and the way to move it. */

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 22–23why, one step back
22export class DevClock extends Context.Service<
23 DevClock,

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 24–37why, one step back
24 {
25 /** The calendar's instant `at`, as it stands in this world. */26 readonly place: (at: DateTime.Utc) => DateTime.Utc;27 /** Stands the clock at the calendar's instant `at`; a running clock runs on from there. */28 readonly set: (at: DateTime.Utc) => Effect.Effect<void>;29 /** From the instant it stands at, the clock moves in real time. */30 readonly run: Effect.Effect<void>;31 /**32 * When something that happens now happens: on a running clock, the33 * present; on one standing still, a minute after where it stands, which34 * it then stands at, so the thing reads as after all before it.35 */36 readonly step: Effect.Effect<DateTime.Utc>;37 }

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 38–41why, one step back
38>()("web/dev/DevClock") {}
3940const NANOS_PER_MILLI = 1_000_000n;41

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 42why, one step back
42/** Where the calendar starts: every instant the seed writes is this one or a whole minute after. */

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 43–46why, one step back
43export const devStart: DateTime.Utc = DateTime.toUtc(
44 DateTime.makeUnsafe("2026-10-07T09:00:00Z")45);46

Merge dev/live-clock into mainoscabrielbefore adoption923d465

line 47why, one step back
47/** The calendar's instant `minutes` whole minutes after its start. */

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 48–50why, one step back
48export const devMinute = (minutes: number): DateTime.Utc =>
49 DateTime.add(devStart, { minutes });50

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 51–69why, one step back
51/**
52 * Where a world starts whose calendar instant `end` is the present minute:53 * the clock read once, rounded down to its minute, less the calendar's54 * distance from its start to `end`.55 */56export const startEndingNow = (57 end: DateTime.Utc58): Effect.Effect<DateTime.Utc> =>59 Effect.map(DateTime.now, (now) =>60 DateTime.subtractDuration(61 DateTime.startOf(now, "minute"),62 DateTime.distance(devStart, end)63 )64 );6566/**67 * A world's clock, whose calendar starts at `start`: standing at `start`68 * until it is set or run, and the way to do either.69 */

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

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

Merge dev/live-clock into mainoscabrielbefore adoption923d465

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

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

line 75why, one step back
75

Merge dev/live-clock into mainoscabrielbefore adoption923d465

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

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

line 77why, one step back
77

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 78–99why, one step back
78 // The instant the clock was last stood at, and, once it runs, the
79 // monotonic reading it has run from since.80 let stood = DateTime.toEpochMillis(start);81 let runningSince: bigint | undefined;8283 const elapsedNanos = () =>84 runningSince === undefined85 ? 0n86 : real.monotonicTimeNanosUnsafe() - runningSince;8788 const nanos = () => BigInt(stood) * NANOS_PER_MILLI + elapsedNanos();8990 const millis = () => Number(nanos() / NANOS_PER_MILLI);9192 const standAt = (at: number) => {93 stood = at;9495 if (runningSince !== undefined) {96 runningSince = real.monotonicTimeNanosUnsafe();97 }98 };99

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

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

Merge dev/live-clock into mainoscabrielbefore adoption923d465

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

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 103–111why, one step back
103 currentTimeNanos: Effect.sync(nanos),
104 currentTimeNanosUnsafe: nanos,105 // Elapsed time is measured, never seeded.106 monotonicTimeNanos: real.monotonicTimeNanos,107 monotonicTimeNanosUnsafe: () => real.monotonicTimeNanosUnsafe(),108 sleep: (duration) => real.sleep(duration),109 };110111 const devClock = DevClock.of({

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 112–115why, one step back
112 place,
113 run: Effect.sync(() => {114 runningSince ??= real.monotonicTimeNanosUnsafe();115 }),

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

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

Merge dev/live-clock into mainoscabrielbefore adoption923d465

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

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

line 119why, one step back
119 }),

Merge dev/live-clock into mainoscabrielbefore adoption923d465

lines 120–128why, one step back
120 step: Effect.sync(() => {
121 if (runningSince !== undefined) {122 return DateTime.toUtc(DateTime.makeUnsafe(millis()));123 }124125 standAt(millis() + Duration.toMillis(Duration.minutes(1)));126127 return DateTime.toUtc(DateTime.makeUnsafe(stood));128 }),

Merge build/502-web-dev-backend: the web runs locally over a seeded in-process worldoscabrielbefore adoption4aeb23e

lines 129–135why, one step back
129 });
130131 return Context.make(DevClock, devClock).pipe(132 Context.add(Clock.Clock, clock)133 );134 })135 );