Skip to content

Commit

Permalink
TestClock yield with setTimeout(0) (#3592)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored Sep 11, 2024
1 parent 95bf1ee commit 35a0f81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-dogs-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

TestClock yield with setTimeout(0)
12 changes: 10 additions & 2 deletions packages/effect/src/TestClock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,21 @@ export class TestClockImpl implements TestClock {
return Option.none()
})
}

private yieldTimer = core.async<void>((resume) => {
const timer = setTimeout(() => {
resume(core.void)
}, 0)
return core.sync(() => clearTimeout(timer))
})

/**
* Returns whether all descendants of this fiber are done or suspended.
*/
suspended(): Effect.Effect<HashMap.HashMap<FiberId.FiberId, FiberStatus.FiberStatus>, void> {
return pipe(
this.freeze(),
core.zip(this.live.provide(pipe(effect.sleep(Duration.millis(5)), core.zipRight(this.freeze())))),
core.zip(pipe(this.yieldTimer, core.zipRight(this.freeze()))),
core.flatMap(([first, last]) =>
Equal.equals(first, last) ?
core.succeed(first) :
Expand All @@ -328,7 +336,7 @@ export class TestClockImpl implements TestClock {
pipe(
this.suspended(),
core.zipWith(
pipe(this.live.provide(effect.sleep(Duration.millis(10))), core.zipRight(this.suspended())),
pipe(this.yieldTimer, core.zipRight(this.suspended())),
Equal.equals
),
effect.filterOrFail(identity, constVoid),
Expand Down

0 comments on commit 35a0f81

Please sign in to comment.