This question already has answers here:
Reactive Throttle returning all items added within the TimeSpan
(3 answers)
How to implement buffering with timeout in RX
(4 answers)
Closed 4 months ago.
I am developing a file monitoring system. What I need is to record each change, collect them to buffer, and throttled emit.
In other words, when there is a change, the buffer start recording. And if there's no change in 200ms, then emit all the changes (either one by one or collection is OK). The Throttle method does not meet my requirement, since it only return the last element in each group (the green and purple point in the following diagram).
I've coded following (using GroupByUntil) as suggested in this answer by cwharris:
using var watcher = new FileSystemWatcher("some_path")
{
Filter = "*.*",
NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size,
IncludeSubdirectories = true,
EnableRaisingEvents = true,
};
var Files = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
h => watcher.Created += h, h => watcher.Created -= h)
.Select(x => x.EventArgs.FullPath)
.GroupByUntil(x => true, g => g.Throttle(TimeSpan.FromMilliseconds(200)))
.SelectMany(x => x.ToArray());
using IDisposable handle = Files.Subscribe(x => Console.WriteLine(String.Join("\n", x)));
Is there any other way to implement this? e.g. Using Buffer and bufferClosingSelector?
As commented by #Theodor Zoulias, since it's a hot observable, the solution becomes:
var Files = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
h => watcher.Created += h, h => watcher.Created -= h)
.Select(x => x.EventArgs.FullPath);
var emits = Files.Throttle(TimeSpan.FromMilliseconds(200));
var FinalFiles = Files.Window(() => emits).SelectMany(x => x.ToArray());
Related
To generate a "measure" every 5 sec I'm doing something like :
var Events = Observable.
Interval(TimeSpan.FromSeconds(5)).
Select(i => factory.GenerateRandomMeasure())
I would like to do the same but based on an existing Measure collection.
I assume I have to do something like :
var Events = existingList.ToObservable();
But is It possible to do add an interval notion in order to get each list item with a interval? (one item every 5 sec for example)
You can do either of these which work just fine:
(1)
var Events =
Observable
.Interval(TimeSpan.FromSeconds(5))
.Zip(existingList, (i, x) => x)
.Select(i => factory.GenerateRandomMeasure());
(2)
var Events2 =
Observable
.Generate(
0,
x => x < existingList.Count,
x => x + 1,
x => existingList[x],
x => TimeSpan.FromSeconds(5))
.Select(i => factory.GenerateRandomMeasure());
The first is probably more sensible and easier to write. The second is very much worth learning if you don't know it already as .Generate is very powerful and can be used in a lot of places.
We are using Rx to monitor activity within our silverlight application so that we can display a message to the user after a period of inactivity.
We are turning events (mouse moves etc.) into observables and then merging the observables together to create a single (allActivity) observable. We then throttle the allActivity observable using a timespan and something subscribes to be notified when the system has been inactive for a period of time.
How can I add a new observable/ sequence to this after the subscription (so that the subscription picks this up without unsubscribing and resubscribing).
e.g. merge several sequences together, throttle, subscribe. Now add an additional sequence to the observable that has been subscribed to.
Example code:
private IObservable<DateTime> allActivity;
public void CreateActivityObservables(UIElement uiElement)
{
// Create IObservables of event types we are interested in and project them as DateTimes
// These are our observables sequences that can push data to subscribers/ observers
// NB: These are like IQueryables in the sense that they do not iterate over the sequence just provide an IObservable type
var mouseMoveActivity = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => uiElement.MouseMove += h, h => uiElement.MouseMove -= h)
.Select(o => DateTime.Now);
var mouseLeftButtonActivity = Observable.FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(h => uiElement.MouseLeftButtonDown += h, h => uiElement.MouseLeftButtonDown -= h)
.Select(o => DateTime.Now);
var mouseRightButtonActivity = Observable.FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(h => uiElement.MouseRightButtonDown += h, h => uiElement.MouseRightButtonDown -= h)
.Select(o => DateTime.Now);
var mouseWheelActivity = Observable.FromEventPattern<MouseWheelEventHandler, MouseWheelEventArgs>(h => uiElement.MouseWheel += h, h => uiElement.MouseWheel -= h)
.Select(o => DateTime.Now);
var keyboardActivity = Observable.FromEventPattern<KeyEventHandler, KeyEventArgs>(h => uiElement.KeyDown += h, h => uiElement.KeyDown -= h)
.Select(o => DateTime.Now);
var streetViewContainer = HtmlPage.Document.GetElementById("streetViewContainer");
var mouseMoveHandler = new EventHandler<HtmlEventArgs>(this.Moo);
bool b = streetViewContainer.AttachEvent("mousemove", mouseMoveHandler);
var browserActivity = Observable.FromEventPattern<Landmark.QDesk.ApplicationServices.IdleTimeoutService.MouseMoveHandler, HtmlEventArgs>(h => this.MyMouseMove += h, h => this.MyMouseMove -= h).Select(o => DateTime.Now);
// Merge the IObservables<DateTime> together into one stream/ sequence
this.allActivity = mouseMoveActivity.Merge(mouseLeftButtonActivity)
.Merge(mouseRightButtonActivity)
.Merge(mouseWheelActivity)
.Merge(keyboardActivity)
.Merge(browserActivity);
}
public IDisposable Subscribe(TimeSpan timeSpan, Action<DateTime> timeoutAction)
{
IObservable<DateTime> timeoutNotification = this.allActivity.Merge (IdleTimeoutService.GetDateTimeNowObservable())
.Throttle(timeSpan)
.ObserveOn(Scheduler.ThreadPool);
return timeoutNotification.Subscribe(timeoutAction);
}
There's an overload to Merge that takes in an IObservable<IObservable<TSource>>. Make the outer sequence a Subject<IObservable<TSource>> and call OnNext to it when you want to add another source to the bunch. The Merge operator will receive the source and subscribe to it:
var xss = new Subject<IObservable<int>>();
xss.Merge().Subscribe(x => Console.WriteLine(x));
xss.OnNext(Observable.Interval(TimeSpan.FromSeconds(1.0)).Select(x => 23 + 8 * (int)x));
xss.OnNext(Observable.Interval(TimeSpan.FromSeconds(0.8)).Select(x => 17 + 3 * (int)x));
xss.OnNext(Observable.Interval(TimeSpan.FromSeconds(1.3)).Select(x => 31 + 2 * (int)x));
...
The easiest way to do this would be to use an intermediate subject in place of the Merge calls.
Subject<DateTime> allActivities = new Subject<DateTime>();
var activitySubscriptions = new CompositeDisposable();
activitySubscriptions.Add(mouseMoveActivity.Subscribe(allActivities));
activitySubscriptions.Add(mouseLeftButtonActivity.Subscribe(allActivities));
//etc ...
//subscribe to activities
allActivities.Throttle(timeSpan)
.Subscribe(timeoutAction);
//later add another
activitySubscriptions.Add(newActivity.Subscribe(allActivities));
The Subject class will stop passing OnNext (and further OnError and OnCompleted) events from any of the observables it is subscribed to if it receives any OnError or OnCompleted.
The main difference between this approach and your sample is that it subscribes to all the events when the subject is created, rather than when you subscribe to the merged observable. Since all of the observables in your example are hot, the difference should not be noticeable.
I want to use Sample to reduce the frequency of items coming out of my observable, but I want to immediately see the first event go through without being held up for the sample duration. After that I want the Sample to only give me an item on the sample interval.
The code I have for the simple Sample is:
var sampler = Observable
.Interval(TimeSpan.FromSeconds(2))
.Select(_ => Unit.Default);
var seq = Observable.FromEventPattern<IntEventArgs>(h => _eventSource.Happened += h, h => _eventSource.Happened -= h)
.Sample(sampler);
So I tried to use this to make it produce an item immediately, however that stops the observable working altogether:
var seq = Observable.FromEventPattern<IntEventArgs>(h => _eventSource.Happened += h, h => _eventSource.Happened -= h)
.Sample(Observable.Return(Unit.Default).Concat(sampler));
Then I thought maybe the problem is the Unit.Default part of the sampler so I tried getting rid of that but now that gives a compiler error:
var sampler = Observable
.Interval(TimeSpan.FromSeconds(2));
var seq = Observable.FromEventPattern<IntEventArgs>(h => _eventSource.Happened += h, h => _eventSource.Happened -= h)
.Observable.Return(Unit.Default).Concat(sampler);
I've tried googling for things like "c# immediate observable sample" but nothing shows up, I guess I'm using the wrong terminology but not sure what I do need...
Any ideas please?
Does this work for you?
var observable = Observable.Merge<IntEventArgs>(h => _eventSource.Happened += h,
h => _eventSource.Happened -= h)
.Publish()
.RefCount();
var seq = Observable.Merge<IntEventArgs>(observable.FirstAsync(),
observable.Skip(1).Sample(sampler));
The Publish() method makes sure that you register only once to your event.
I'm trying to tame a keyboard triggered event that without throttling would fire with a very high frequency. The code below works but it also delays the first invocation of GlobalCopy by the throttling timespan. Is there a way to get the first sample without delay?
observableGlobalCopy = Observable
.FromEventPattern<EventHandler, EventArgs>(h => this.GlobalCopy += h, h => this.GlobalCopy -= h);
observableGlobalCopy
.SubscribeOnDispatcher()
.Throttle(GlobalEventThottle)
.Subscribe(x => GlobalCopyHandler());
You could try something like this:
var observableGlobalCopy2 = observableGlobalCopy
.Window(() => Observable.Timer(TimeSpan.FromSeconds(1.0)))
.Select(_ => _.Take(1))
.Merge();
If I understand your requirements correctly, how about this?
observableGlobalCopy.Take(1)
.Concat(
observableGlobalCopy.Throttle(GlobalEventThottle))
.SubscribeOnDispatcher()
.Subscribe(x => GlobalCopyHandler());
I have two observables. One is from Observable.fromEvent(..), where the underlying event is the user checking a Winforms checkbox. The other is Observable.Interval(..) which I subscribe to in order to do some IO, and I would like to prevent this observable from doing IO, whenever the checkbox is not checked.
I could do it like this:
var gui = new GUI();
var booleans = Observable
.FromEvent<GUI.NewAllowHandler, bool>(
h => gui.NewAllow += h,
h => gui.NewAllow -= h)
Observable.Interval(TimeSpan.FromSeconds(10))
.CombineLatest(booleans, Tuple.Create)
.Where(t => t.Item2)
.Select(t => t.Item1)
.Subscribe(l => DoStuff(l));
but this has the overhead of mixing the booleans in and out of the stream. A nicer way of doing this would be, if I could generate a time-varying value from the booleans variable, which at all times had the value of the last event. Then I could do something like this:
var gui = new GUI();
var booleanState = Observable // typeof(booleanState) == ???
.FromEvent<GUI.NewAllowHandler, bool>(
h => gui.NewAllow += h,
h => gui.NewAllow -= h)
.TimeValue() // hypothetical syntax
Observable.Interval(TimeSpan.FromSeconds(10))
.Where(_ => booleanState)
.Subscribe(l => DoStuff(l));
, which to me seems much closer to the problem statement. Is there anything like this in Rx, or is there anything else, that could make such problems easier to handle?
The Where statement in your interval should work with a properly scoped normal bool:
var booleans = Observable
.FromEvent<GUI.NewAllowHandler, bool>(
h => gui.NewAllow += h,
h => gui.NewAllow -= h)
var isBoxChecked = false;
booleans.Subscribe(t => isBoxChecked = t);
Observable.Interval(TimeSpan.FromSeconds(10))
.Where(_ => isBoxChecked)
.Subscribe(l => DoStuff(l))
Edit: Per your comment, another way of doing it:
intervals = Observable.Interval(TimeSpan.FromSeconds(10));
booleans
.Where(t => t)
.SelectMany(_ => intervals.TakeUntil(booleans))
.Subscribe(l => DoStuff(l))
You need to model the checkbox checked state as Behavior and not as Event stream (because behavior has always a value and this value changes over a period of time - which fits with checkbox checked state). So you can do something like:
var booleans = new BehaviorSubject<bool>(chk.Checked)
var chkEvents = ... //generate boolean observable from checkbox check event
chkEvents.Subscribe(booleans);
Observable.Interval(TimeSpan.FromSeconds(10))
.Where(i => booleans.First())
.Subscribe(i => DoIO());
I'm going to give you two solutions. The first is a very simple and hopefully obvious one using only one observable. The second is a uses both observables.
Since you want to allow the IO only when the box is checked then this is the simplest approach:
Observable
.Interval(TimeSpan.FromSeconds(10))
.Where(_ => gui.IsChecked)
.Subscribe(l => DoStuff(l));
No need at all for the other observable.
But if you really need to use it then the Switch() extension method is your best bet. Try this:
booleans
.Select(b => b == true
? Observable.Interval(TimeSpan.FromSeconds(10))
: Observable.Empty<long>())
.Switch()
.Subscribe(l => DoStuff(l));
It's pretty clean and helps to show that there are empty periods if the checkbox is not ticked.
I hope this helps.