Reactive Framework / DoubleClick

2019-04-14 09:30发布

问题:

I know that there is an easy way to do this - but it has beaten me tonight ...

I want to know if two events occur within 300 milliseconds of each other, as in a double click.

Two leftdown mouse clicks in 300 milliseconds - I know this is what the reactive framework was built for - but damn if I can find a good doc that has simple examples for all the extenstion operatores - Throttle, BufferWithCount, BufferWithTime - all of which just werent' doing it for me....

回答1:

The TimeInterval method will give you the time between values.

public static IObservable<Unit> DoubleClicks<TSource>(
    this IObservable<TSource> source, TimeSpan doubleClickSpeed, IScheduler scheduler)
{
    return source
        .TimeInterval(scheduler)
        .Skip(1)
        .Where(interval => interval.Interval <= doubleClickSpeed)
        .RemoveTimeInterval();
}

If you want to be sure that triple clicks don't trigger values, you could just use Repeat on a hot observable (I've used a FastSubject here as the clicks will all come on one thread and therefore don't require the heaviness of the normal Subjects):

public static IObservable<TSource> DoubleClicks<TSource>(
    this IObservable<TSource> source, TimeSpan doubleClickSpeed, IScheduler scheduler)
{
    return source.Multicast<TSource, TSource, TSource>(
        () => new FastSubject<TSource>(), // events won't be multithreaded
        values =>
        {
            return values
                .TimeInterval(scheduler)
                .Skip(1)
                .Where(interval => interval.Interval <= doubleClickSpeed)
                .RemoveTimeInterval()
                .Take(1)
                .Repeat();
        });
}


回答2:

Edit - Use TimeInterval() instead.

The Zip() and Timestamp() operators might be a good start.

var ioClicks = Observable.FromEvent<MouseButtonEventHandler, RoutedEventArgs>(
                                        h => new MouseButtonEventHandler(h),
                                        h => btn.MouseLeftButtonDown += h,
                                        h => btn.MouseLeftButtonDown -= h);
var ioTSClicks = ioClicks.Timestamp();

var iodblClicks = ioTSClicks.Zip(ioTSClicks.Skip(1), 
                                 (r, l) => l.Timestamp - r.Timestamp)
                            .Where(tspan => tspan.TotalMilliseconds < 300);

Probably best to test this via the test scheduler, so you know exactly what you're getting:

[Fact]
public void DblClick()
{
    // setup
    var ioClicks = _scheduler.CreateHotObservable(
                     OnNext(210, "click"),
                     OnNext(220, "click"),
                     OnNext(300, "click"),
                     OnNext(365, "click"))
                     .Timestamp(_scheduler);

    // act
    Func<IObservable<TimeSpan>> target = 
        () => ioClicks.Zip(ioClicks.Skip(1), 
                          (r, l) => l.Timestamp - r.Timestamp)
                        .Where(tspan => tspan.Ticks < 30);
    var actuals = _scheduler.Run(target);

    // assert
    Assert.Equal(actuals.Count(), 1);
    // + more
}
public static Recorded<Notification<T>> OnNext<T>(long ticks, T value)
{
    return new Recorded<Notification<T>>(
        ticks, 
        new Notification<T>.OnNext(value));
}