我有一个订阅的可观察对象。这个可观察对象将返回一个具有ActivationType属性的对象,该属性可以被设置多次。
我想要实现的是日志消息每当ActivationType被设置为"Type1"。但是,如果ActivationType设置为"Type2",则只记录一次消息,如果ActivationType设置为"Type2",则等待30秒后再记录。
如果我输入:
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
我相信油门()是我正在寻找的,但我不确定如何有条件地触发它。
有什么建议吗?
啊,对于几乎无法理解的Window
运算符来说,这是一个完美的例子!
编辑:我一个月发十几次这个链接,我发誓——这是我见过的最好的Window
, Join
, Buffer
, GroupJoin
等操作符:
Lee Campbell: Rx Part 9-Join、Window、Buffer和Group Join
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();
}
为什么不使用两个流呢?
var baseStream = myObservable.Publish().RefCount(); // evaluate once
var type1 = baseStream.Where(o => o.ActivationType == "Type1");
var type2 = baseStream.Where(o => o.ActivationType == "Type2").Throttle(TimeSpan.FromSeconds(30));
type1.Merge(type2).Subscribe(Log);