Skip to content

Instantly share code, notes, and snippets.

@rogeralsing
Last active August 29, 2015 14:01
Show Gist options
  • Save rogeralsing/fe5a57ad7308332a4334 to your computer and use it in GitHub Desktop.
Save rogeralsing/fe5a57ad7308332a4334 to your computer and use it in GitHub Desktop.
var chatMessages = GetObservableOfChatMessages();
var chatIsIdle = chatMessages. _dontknow_ (TimeSpan.FromSeconds(5));
chatMessages.Subscribe (msg => Console.WriteLine("{0} says: {1}",msg.User,msg.Text));
//I want this to occur every time the chatmessages observable have been silent for 5 sec
//not just once
chatIsIdle.Subscribe ( ... => Console.WriteLine("Chat is idle...");
@richkeenan
Copy link

You could make an extension method, BufferUntilInactive

public static IObservable<IList<T>> BufferUntilInactive<T>(this IObservable<T> stream, TimeSpan delay)
{
    var closes = stream.Throttle(delay);
    return stream.Window(() => closes).SelectMany(window => window.ToList());
}

And just ignore the return values, so your above code looks like

var chatIsIdle = chatMessages.BufferUntilInactive(TimeSpan.FromSeconds(5));
chatIsIdle.Subscribe ( _ => Console.WriteLine("Chat is idle...");

I got this from a similar question I asked on SO a while back, so credit to Colonel Panic for his answer, http://stackoverflow.com/q/8849810/255231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment