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 fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge dev/live-clock into mainoscabrielbefore adoption923d465
line 1why, one step back1import { Clock, Context, DateTime, Duration, Effect, Layer } from "effect";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 2–4why, one step back23export class DevClock extends Context.Service<4 DevClock,Merge dev/live-clock into mainoscabrielbefore adoption923d465
lines 5–8why, one step back5 {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 back9 /** 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 back10 readonly step: Effect.Effect<DateTime.Utc>;11 }Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 12–22why, one step back12>()("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 });22Merge dev/live-clock into mainoscabrielbefore adoption923d465
lines 23–32why, one step back23export 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 );32Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 33–36why, one step back33export 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 back37 const offset = DateTime.distance(devStart, start);Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 38why, one step back38Merge dev/live-clock into mainoscabrielbefore adoption923d465
line 39why, one step back39 const place = (at: DateTime.Utc) => DateTime.addDuration(at, offset);Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 40why, one step back40Merge dev/live-clock into mainoscabrielbefore adoption923d465
lines 41–60why, one step back41 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 };60Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 61why, one step back61 const clock: Clock.Clock = {Merge dev/live-clock into mainoscabrielbefore adoption923d465
lines 62–63why, one step back62 currentTimeMillis: Effect.sync(millis),63 currentTimeMillisUnsafe: millis,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 64–72why, one step back64 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 back73 place,74 run: Effect.sync(() => {75 runningSince ??= real.monotonicTimeNanosUnsafe();76 }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 77–78why, one step back77 set: (at) =>78 Effect.sync(() => {Merge dev/live-clock into mainoscabrielbefore adoption923d465
line 79why, one step back79 standAt(DateTime.toEpochMillis(place(at)));Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 80why, one step back80 }),Merge dev/live-clock into mainoscabrielbefore adoption923d465
lines 81–89why, one step back81 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 }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 90–96why, one step back90 });9192 return Context.make(DevClock, devClock).pipe(93 Context.add(Clock.Clock, clock)94 );95 })96 );