观察同步和异步结果
使用 Rx,我想观察一个公开方法 GetItems
和事件 NewItem
的遗留对象。
当调用 GetItems
时,它将同步返回缓存中所有项目的列表。它还会生成项目的异步获取,这些项目将在收到时通过 NewItem 事件发布。
如何通过制定 LINQ 查询来捕获这两个结果集,以一致的方式观察这两个源(同步 + 异步)?生产订单并不重要。
Using Rx, I want to observe a legacy object that exposes both the method GetItems
and the event NewItem
.
When GetItems
is called, it'll synchronously return a list of any items it has in cache. It'll also spawn an async fetch of items that will be published via the NewItem
event as they are received.
How can I observe both of these sources (sync + async) in a consistent way by formulating a LINQ query such that both result-sets are captured? The production order is of no importance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我看看我是否理解了您的遗留对象。我假设它是一个如下所示的泛型类型:
使用如下所示的新项目事件参数:
现在,我为
LegacyObject创建了一个
:.ToObservable()
扩展方法;此方法为每个订阅者创建一个新的可观察对象 - 在编写这样的扩展方法时,这是正确的做法。
它创建一个
gate
对象来锁定对内部列表的访问。因为您说调用
GetItems
的行为会生成异步函数来获取新项目,所以我确保在调用GetItems 之前创建
。NewItem
订阅inner
订阅会检查新项目是否在列表中,并且仅在主题不在列表中时才调用该主题的OnNext
。调用
GetItems
并通过AddRange
将值添加到内部列表中。在
NewItem
事件开始在另一个线程上触发之前,项目虽然不太可能但有可能不会添加到列表中。这就是为什么对列表的访问有锁的原因。inner
订阅将等待,直到它能够获得锁定,然后再尝试将项目添加到列表中,这将在初始项目添加到列表后发生。最后,内部列表变成一个可观察的,与主题连接起来,并且
o
观察者订阅这个可观察的。使用
CompositeDisposable
将这两个订阅作为单个IDisposable
返回。这就是
ToObservable
方法。现在,我通过在遗留对象上创建一个构造函数来测试这一点,该构造函数允许我传递可枚举值和可观察值。当调用
GetItems
时返回可枚举值,并且可观察值驱动NewItem
事件。因此,我的测试代码如下所示:
写入控制台的值是:
让我知道这是否满足您的需求。
Let me see if I've understood your legacy object. I'm assuming it is a generic type that looks like this:
With the new item event args like this:
Now, I've created a
.ToObservable()
extension method forLegacyObject<T>
:This method creates a new observable for every subscriber - which is the correct thing to do when writing extension methods like this.
It creates a
gate
object to lock access to the internal list.Because you said that the act of calling
GetItems
spawns the async function to get new items I've made sure that theNewItem
subscription is created before the call toGetItems
.The
inner
subscription checks if the new item is in the list or not and only callsOnNext
on the subject if it's not in the list.The call to
GetItems
is made and the values are added to the internal list viaAddRange
.There is an unlikely, but possible, chance that the items won't be added to the list before the
NewItem
event begins firing on another thread. This is why there is a lock around the access to the list. Theinner
subscription will wait until it can obtain the lock before attempting to add items to the list and this will happen after the initial items are added to the list.Finally the internal list is turned into an observable, concatenated with the subject, and the
o
observer subscribes to this observable.The two subscriptions are returned as a single
IDisposable
usingCompositeDisposable
.And that is it for the
ToObservable
method.Now, I tested this by creating a constructor on the legacy object that let me pass in both an enumerable and an observable of values. The enumerable is returned when the
GetItems
is called and the observable drives theNewItem
event.So, my test code looked like this:
And the values written to the console were:
Let me know if this meets your needs.
如果我正确理解你的问题,可以使用这样的方法来完成:
编辑:Enigmativity 在上面的解决方案中发现了竞争条件(请参阅下面的评论)。希望这个可以解决这个问题:
如果有帮助的话,这是我的其余测试代码。我不知道我是否准确地模拟了你的遗留类:
If I understood your question correctly, it could be done using something like this:
Edit: Enigmativity spotted a race condition on the solution above (see comments below). This one hopefully solves that:
Here is the rest of my test code if it helps. I don't know if I accurately modeled your legacy class though: