MCPcopy Create free account
hub / github.com/Effect-TS/effect / ClockImpl

Class ClockImpl

packages/effect/src/internal/effect.ts:6515–6542  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6513const MAX_TIMER_MILLIS = 2 ** 31 - 1
6514
6515class ClockImpl implements Clock.Clock {
6516 currentTimeMillisUnsafe(): number {
6517 return Date.now()
6518 }
6519 readonly currentTimeMillis: Effect.Effect<number> = sync(() => this.currentTimeMillisUnsafe())
6520 currentTimeNanosUnsafe(): bigint {
6521 return wallTimeNanos()
6522 }
6523 readonly currentTimeNanos: Effect.Effect<bigint> = sync(() => this.currentTimeNanosUnsafe())
6524 monotonicTimeNanosUnsafe(): bigint {
6525 return monotonicNowNanos()
6526 }
6527 readonly monotonicTimeNanos: Effect.Effect<bigint> = sync(() => this.monotonicTimeNanosUnsafe())
6528 sleep(duration: Duration.Duration): Effect.Effect<void> {
6529 return this.sleepMillis(Duration.toMillis(duration))
6530 }
6531 private sleepMillis(millis: number): Effect.Effect<void> {
6532 if (millis <= 0) return yieldNow
6533 else if (!Number.isFinite(millis)) return never
6534 return callbackNoSignal((resume) => {
6535 const continuation = millis > MAX_TIMER_MILLIS
6536 ? this.sleepMillis(millis - MAX_TIMER_MILLIS)
6537 : void_
6538 const handle = setTimeout(() => resume(continuation), Math.min(millis, MAX_TIMER_MILLIS))
6539 return sync(() => clearTimeout(handle))
6540 })
6541 }
6542}
6543
6544const nanosPerMilli = BigInt(1_000_000)
6545

Callers

nothing calls this directly

Calls 4

syncFunction · 0.50

Tested by

no test coverage detected