Last active
August 29, 2015 14:01
-
-
Save rogeralsing/fe5a57ad7308332a4334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."); |
This almost does what I want, however, this will repeat the "Chat is idle..." every 5 sec.
I only want one such notification 5 sec after the chat message, and if the chat is again active and then idles again, I want a new idle message..
e.g.
x: hello
y: hi
-5 sec passes..
chat is idle...
-100 sec passes.. the idle message should not repeat here
x: Im back
y: nice
-5 sec passes
chat is idle...
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
Nice @mattpodwysocki. Could also use timeout.