| 295 | } |
| 296 | |
| 297 | class TestClockImpl implements TestClock { |
| 298 | readonly config: Required<TestClock.Options> |
| 299 | readonly liveClock: Clock.Clock |
| 300 | readonly liveConsole: Console.Console |
| 301 | readonly scheduler: TrackingScheduler |
| 302 | // Sorted by descending timestamp, so `pop` wakes the earliest sleep first |
| 303 | readonly sleeps: Array<SleepEntry> = [] |
| 304 | readonly warningSemaphore = Semaphore.makeUnsafe(1) |
| 305 | readonly runSemaphore = Semaphore.makeUnsafe(1) |
| 306 | currentTimestamp = 0 |
| 307 | currentWallNanos = BigInt(0) |
| 308 | currentMonotonicNanos = BigInt(0) |
| 309 | warningState: WarningState = WarningState.Start() |
| 310 | /** |
| 311 | * Tasks scheduled while waking a sleeper, and tasks scheduled by those tasks, |
| 312 | * are counted by that wake's tracker so `run` can wait for the woken fiber, |
| 313 | * and any fibers it resumes or forks, to suspend before checking the next |
| 314 | * sleep. |
| 315 | */ |
| 316 | currentTracker: WakeTracker | undefined = undefined |
| 317 | |
| 318 | constructor(options: TestClock.Options | undefined, context: Context.Context<never>) { |
| 319 | this.config = { ...defaultOptions, ...options } |
| 320 | const currentClock = Context.get(context, Clock.Clock) |
| 321 | this.liveClock = liveClocks.get(currentClock) ?? currentClock |
| 322 | this.liveConsole = Context.get(context, Console.Console) |
| 323 | this.scheduler = new TrackingScheduler(this, Context.get(context, Scheduler.Scheduler)) |
| 324 | } |
| 325 | |
| 326 | // The `TestClock` methods are own arrow functions, so they still work when |
| 327 | // detached from the clock or copied with a spread. |
| 328 | |
| 329 | readonly stateUnsafe = (): TestClock.State => { |
| 330 | // `sleeps` is kept latest first |
| 331 | const pending = this.sleeps.map((entry): [number, Latch.Latch] => [entry.timestamp, entry.latch]).reverse() |
| 332 | return { timestamp: this.currentTimestamp, sleeps: pending } |
| 333 | } |
| 334 | |
| 335 | readonly currentTimeMillisUnsafe = (): number => this.currentTimestamp |
| 336 | readonly currentTimeNanosUnsafe = (): bigint => this.currentWallNanos |
| 337 | readonly monotonicTimeNanosUnsafe = (): bigint => this.currentMonotonicNanos |
| 338 | readonly currentTimeMillis = Effect.sync(this.currentTimeMillisUnsafe) |
| 339 | readonly currentTimeNanos = Effect.sync(this.currentTimeNanosUnsafe) |
| 340 | readonly monotonicTimeNanos = Effect.sync(this.monotonicTimeNanosUnsafe) |
| 341 | |
| 342 | readonly withLive = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 343 | Effect.provideService(effect, Clock.Clock, this.liveClock) |
| 344 | |
| 345 | readonly sleep = (duration: Duration.Duration): Effect.Effect<void> => |
| 346 | Effect.suspend(() => { |
| 347 | const end = this.currentTimestamp + Duration.toMillis(duration) |
| 348 | if (end <= this.currentTimestamp) return Effect.void |
| 349 | const sleeps = this.sleeps |
| 350 | return Effect.acquireUseRelease( |
| 351 | Effect.sync(() => { |
| 352 | const entry = { timestamp: end, latch: Latch.makeUnsafe() } |
| 353 | // Insert before entries with an equal timestamp, so they wake in |
| 354 | // registration order |