I have a worker which exposes a Subject<string>, which publishes log messages very quickly. Writing to the console is slow, so I want to only write to the console every 100ms, at the most. When the task is finished I want to write out the most recent published string, to avoid having things like Doing work 2312/2400 ...done. (or even ...done if the task takes <100ms.)
I’m new to reactive extensions, and though I’ve heard talks about how awesome they are, this is the first time I’ve noticed a situation where they could help me.
So, in summary,
1) Don’t give me an event more than once every 100ms
2) I need to know about the final event, regardless of when it arrives.
I’ll put my code in an answer below, but please suggest something better. Maybe I’ve missed a standard call which achieves this?
It’s actually simpler than you make out if the observable that is the source of your events called
OnCompleted()when it’s done. This will do the trick by itself:This is because
Samplewill fire one last time when the source of it’s events completes. Here’s a full test:Even though I sleep for 3.5 seconds, 4 events fire, the last one firing when I call
OnCompleted().Another point to note is that it’s bad form if
Worker.GetObservable()actually returns aSubject<string>– even if it is that in the Worker class, what it really should do is return just theIObservable<string>interface. This is more than mere style, it is a separation of concerns and returning the minimum functional interface needed.