-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Restart schedule on end #1767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restart schedule on end #1767
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1810,26 +1810,35 @@ class ZStream[-R, +E, +A](val process: ZManaged[R, E, Pull[R, E, A]]) extends Se | |
for { | ||
as <- self.process | ||
init <- schedule.initial.toManaged_ | ||
state <- Ref.make[(Boolean, schedule.State, Option[() => B])]((false, init, None)).toManaged_ | ||
state <- Ref.make[(schedule.State, Option[() => B])]((init, None)).toManaged_ | ||
pull = state.get.flatMap { | ||
case (done, sched, decision0) => | ||
if (done) Pull.end | ||
else | ||
for { | ||
a <- as.optional.mapError(Some(_)) | ||
c <- a match { | ||
case Some(a) => | ||
for { | ||
decision <- schedule.update(a, sched) | ||
_ <- clock.sleep(decision.delay) | ||
_ <- state.set((!decision.cont, decision.state, Some(decision.finish))) | ||
} yield if (decision.cont) f(a) else g(decision.finish()) | ||
|
||
case None => | ||
state.set((false, sched, None)) *> decision0 | ||
.fold[Pull[R1 with Clock, E1, C]](Pull.end)(b => Pull.emit(g(b()))) | ||
} | ||
} yield c | ||
case (sched0, finish0) => | ||
// Before pulling from the stream, we need to check whether the previous | ||
// action ended the schedule, in which case we must emit its final output | ||
finish0 match { | ||
case None => | ||
for { | ||
a <- as.optional.mapError(Some(_)) | ||
c <- a match { | ||
// There's a value emitted by the underlying stream, we emit it | ||
// and check whether the schedule ends; in that case, we record | ||
// its final state, to be emitted during the next pull | ||
case Some(a) => | ||
for { | ||
decision <- schedule.update(a, sched0) | ||
_ <- clock.sleep(decision.delay) | ||
sched <- if (decision.cont) UIO.succeed(decision.state) else schedule.initial | ||
finish = if (decision.cont) None else Some(decision.finish) | ||
_ <- state.set((sched, finish)) | ||
} yield f(a) | ||
|
||
// The stream ends when both the underlying stream ends and the final | ||
// schedule value has been emitted | ||
case None => Pull.end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means that we don't emit the schedule's final value if the stream ended before the schedule ended. Is that intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. If for instance the stream is completely empty, there never was a value of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'm good with that 👍🏻 |
||
} | ||
} yield c | ||
case Some(b) => state.set((sched0, None)) *> Pull.emit(g(b())) | ||
} | ||
} | ||
} yield pull | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm why did it change to
2
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Schedule.recurs(2)
will consume three elements, as in the test forscheduleWith
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok let me wrap my head around this for a sec..running your state machine manually ;)