* Runs all effects scheduled to occur on or before the specified instant, * which may depend on the current time, in order.
(f: (instant: number) => number)
| 390 | * which may depend on the current time, in order. |
| 391 | */ |
| 392 | run(f: (instant: number) => number): Effect.Effect<void> { |
| 393 | return pipe( |
| 394 | this.awaitSuspended(), |
| 395 | core.zipRight(pipe( |
| 396 | ref.modify(this.clockState, (data) => { |
| 397 | const end = f(data.instant) |
| 398 | const sorted = pipe( |
| 399 | data.sleeps, |
| 400 | Chunk.sort<readonly [number, Deferred.Deferred<void>]>( |
| 401 | pipe(number.Order, Order.mapInput((_) => _[0])) |
| 402 | ) |
| 403 | ) |
| 404 | if (Chunk.isNonEmpty(sorted)) { |
| 405 | const [instant, deferred] = Chunk.headNonEmpty(sorted) |
| 406 | if (instant <= end) { |
| 407 | return [ |
| 408 | Option.some([end, deferred] as const), |
| 409 | makeData(instant, Chunk.tailNonEmpty(sorted)) |
| 410 | ] as const |
| 411 | } |
| 412 | } |
| 413 | return [Option.none(), makeData(end, data.sleeps)] as const |
| 414 | }), |
| 415 | core.flatMap((option) => { |
| 416 | switch (option._tag) { |
| 417 | case "None": { |
| 418 | return core.void |
| 419 | } |
| 420 | case "Some": { |
| 421 | const [end, deferred] = option.value |
| 422 | return pipe( |
| 423 | core.deferredSucceed(deferred, void 0), |
| 424 | core.zipRight(core.yieldNow()), |
| 425 | core.zipRight(this.run(() => end)) |
| 426 | ) |
| 427 | } |
| 428 | } |
| 429 | }) |
| 430 | )) |
| 431 | ) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |