Say you have a source observable of type IObservable<uint>. I'm looking for a transformation function
IObservable<uint> Countdown(IObservable<uint> source)
{
// how? Something like ...
return source.Select(value => ProduceInnerObservable(value)).Switch()
}
that transforms source as follows:
- When
sourceproduces a value, the resulting observable should immediately publish the same value - Say the last element published was
N. In the absence of any new elements fromsource, the result observable should publish the valueN-1afterNticks; thenN-2afterN-1ticks; and so on, until 0 is published - When
sourcepublishes a new value, this timer-like behaviour is reset based on the new value
Example inputs/outputs:
| Tick time | Source observable | Result observable |
|---|---|---|
| 0 | 10 | 10 |
| 10 | 9 | |
| 19 | 8 | |
| 21 | 2 | 2 |
| 23 | 1 | |
| 24 | 0 | |
| 100 | 1 | 1 |
| 101 | 0 | |
| ... | ... | ... |
What's the most elegant way to implement this?
Assuming your "ticks" are in tenths of a second - probably this will suit your needs: