本文介绍了油门只有特定条件满足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个观察的,我订阅的。这obsevable将返回一个具有可设置多次的属性称为ActivationType的对象。

I have an observable that I am subscribing on. This obsevable will be returning an object that has a property called ActivationType that can be set multiple times.

我所试图实现的是日志每当ActivationType设置为留言类型1。但是,如果ActivationType设置为2型,记录消息只有一次,再次登录,如果ActivationType是2型前等待30秒。

What I am trying to achieve is log a message whenever ActivationType is set to "Type1". However, if ActivationType is set to "Type2", log the message only once and wait for 30 seconds before logging again if ActivationType is "Type2".

所以,如果我有

myObservable
    .Where(o => o.ActivationType == "Type1" || o.ActivationType == "Type2")  //listen for types 1 and 2
    .Throttle() // ??? somehow only throttle if we are currently looking at Type2
    .Subscribe(Log); //log some stuff



我相信油门()就是我寻找,但我不知道如何有条件触发它。

I believe Throttle() is what I am looking for but am not sure how to trigger it conditionally.

有什么建议?

推荐答案

嗯, !
我:对几乎不可能理解窗口运营商

编辑一个完美的情况下,像发布了十几次了一个月,我发誓,这个链接 - 最好读得来速是我见过的窗口加入缓存群组加入等运营商:

I post this link like a dozen times a month, I swear - best read-thru I've seen of the Window, Join, Buffer, GroupJoin, etc. operators:

var source = new Subject<Thing>();

var feed = source.Publish().RefCount();
var ofType1 = feed.Where(t => t.ActivationType == "Type1");
var ofType2 = feed
    // only window the type2s
    .Where(t => t.ActivationType == "Type2")
    // our "end window selector" will be a tick 30s off from start
    .Window(() => Observable.Timer(TimeSpan.FromSeconds(30)))
    // we want the first one in each window...
    .Select(lst => lst.Take(1))
    // moosh them all back together
    .Merge();

    // We want all "type 1s" and the buffered outputs of "type 2s"
    var query = ofType1.Merge(ofType2);

    // Let's set up a fake stream of data
    var running = true;
    var feeder = Task.Factory.StartNew(
       () => {
         // until we say stop...
         while(running)
         {
             // pump new Things into the stream every 500ms
             source.OnNext(new Thing());
             Thread.Sleep(500);
         }
    });

    using(query.Subscribe(Console.WriteLine))
    {
        // Block until we hit enter so we can see the live output
        // from the above subscribe
        Console.ReadLine();
        // Shutdown our fake feeder
        running = false;
        feeder.Wait();
     }

这篇关于油门只有特定条件满足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:48