Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created December 13, 2011 02:16
Show Gist options
  • Save krishnabhargav/1470141 to your computer and use it in GitHub Desktop.
Save krishnabhargav/1470141 to your computer and use it in GitHub Desktop.
The class that takes care of throttling the string messages for 1/2 second and returns unique properties that changed
public class StringMessageThrottler
{
//Subject acts as the message bus. It is a IObservable<string> in our case.
private readonly ISubject<string> _eventObservables = new Subject<string>();
private readonly IObservable<string> _distinctPropertyChanged;
public StringMessageThrottler()
{
var eventsAggregatedForHalfSecond = _eventObservables.Buffer(TimeSpan.FromMilliseconds(500), Scheduler.ThreadPool);
//get the unique properties that were changed within the buffered time
_distinctPropertyChanged = eventsAggregatedForHalfSecond.SelectMany(s=>s.Distinct());
}
public IDisposable Subscribe(Action<string> invoke)
{
//When you subscribe, subscribe on the final observable chain.
return _distinctPropertyChanged.Subscribe(invoke);
}
public void Publish(string s)
{
//This is stupid that _eventObservables.Publish is not what is expected.
//Instead to publish, do a OnNext(s) call. Publish does something entirely different I guess!
//But come on guys, IObserver.Publish() -> I expect to publish the damn message
_eventObservables.OnNext(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment