拍摄 ReplaySubject<T> 的快照缓冲 [英] Taking a snapshot of ReplaySubject<T> buffer

查看:27
本文介绍了拍摄 ReplaySubject<T> 的快照缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 ReplaySubject,它允许订阅者像往常一样接收重播缓冲区和未来的通知.另外,我希望能够拍摄快照"当前缓冲区的数据并将其作为列表同步返回,无需订阅.

I have a large ReplaySubject that allows subscribers to receive the replay buffer and future notifications as normal. In addition, I would like to be able to take a "snapshot" of the current buffer and return that as a list synchronously, without having to subscribe.

有没有办法做到这一点?

Is there a way to do this?

谢谢

推荐答案

你能不能不只是订阅,接收项目,然后取消订阅?

Can you not just subscribe, receive the items, then unsubscribe?

public static List<T> Snapshot<T>(ReplaySubject<T> subject)
{
    List<T> snapshot = new List<T>();
    using (subject.Subscribe(item => snapshot.Add(item))
    {
        // Deliberately empty; subscribing will add everything to the list.
    }
    return snapshot;
}

当然,这是假设订阅 ReplaySubject 会同步调用元素处理程序.你想检查一下,但这正是我所期望的.

That's assuming that subscribing to a ReplaySubject<T> invokes the element handler synchronously, of course. You'd want to check that, but that's what I'd expect.

您还应该考虑是否要以某种方式处理错误/完成.

You should also consider whether you want to handle errors / completion somehow.

这篇关于拍摄 ReplaySubject&lt;T&gt; 的快照缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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