I have a SignalProducer
producer
that asynchronously sends Int
s. I can sum the values with
producer.scan(0, +)
Suppose I want to reset the sum to 0
if it is > 10
and no other values have been sent for 1 second. My first attempt looked like this:
producer
.scan(0, +)
.flatMap(.latest) { n -> SignalProducer<Int, NoError> in
if n <= 10 {
return SignalProducer(value: n)
} else {
return SignalProducer.merge([
SignalProducer(value: n),
SignalProducer(value: 0).delay(1, on: QueueScheduler.main)
])
}
}
While this correctly sends 0
, it doesn't reset the state in scan
. That is, a sequence of 9, 8, long pause, 7
sends 9, 17, 0, 24
.
Is there a way to combine these two concept in a way that correctly resets the state?