反应式框架/DoubleClick [英] Reactive Framework / DoubleClick

查看:64
本文介绍了反应式框架/DoubleClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一种简单的方法可以做到这一点 - 但今晚它打败了我......

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

我想知道两个事件是否在 300 毫秒内发生,就像双击一样.

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

在 300 毫秒内单击鼠标左键两次 - 我知道这就是反应式框架的目的 - 但该死的,如果我能找到一个很好的文档,其中包含所有扩展操作符的简单示例 - Throttle、BufferWithCount、BufferWithTime - 所有这只是没有为我做....

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....

推荐答案

TimeInterval 方法将为您提供值之间的时间.

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();
}

如果您想确保三次点击不会触发值,您可以在热可观察对象上使用 Repeat(我在这里使用了 FastSubject 作为点击将全部出现在一个线程上,因此不需要普通主题的沉重感):

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();
        });
}

这篇关于反应式框架/DoubleClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆