Skip to content

Commit

Permalink
avoid sleep for zero duration in schedule (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Jan 19, 2024
1 parent e574992 commit 10df798
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-stingrays-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

avoid sleep for zero duration in schedule
27 changes: 15 additions & 12 deletions packages/effect/src/internal/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,21 @@ class ScheduleDriverImpl<Env, In, Out> implements Schedule.ScheduleDriver<Env, I
core.flatMap((now) =>
pipe(
core.suspend(() => this.schedule.step(now, input, state)),
core.flatMap(([state, out, decision]) =>
ScheduleDecision.isDone(decision) ?
pipe(
ref.set(this.ref, [Option.some(out), state] as const),
core.zipRight(core.fail(Option.none()))
) :
pipe(
ref.set(this.ref, [Option.some(out), state] as const),
core.zipRight(effect.sleep(Duration.millis(Intervals.start(decision.intervals) - now))),
core.as(out)
)
)
core.flatMap(([state, out, decision]) => {
const setState = ref.set(this.ref, [Option.some(out), state] as const)
if (ScheduleDecision.isDone(decision)) {
return core.zipRight(setState, core.fail(Option.none()))
}
const millis = Intervals.start(decision.intervals) - now
if (millis <= 0) {
return core.as(setState, out)
}
return pipe(
setState,
core.zipRight(effect.sleep(Duration.millis(millis))),
core.as(out)
)
})
)
)
)
Expand Down

0 comments on commit 10df798

Please sign in to comment.