共享单个订阅(Publish RefCount)
此代码将订阅 emails
observable 两次:
emails.Where(email => email.From == "John").Subscribe(email => Console.WriteLine("A"));
emails.Where(email => email.From == "Mary").Subscribe(email => Console.WriteLine("B"));
要共享 emails
的单一订阅,我们可以使用 Publish
和 RefCount
代替:
var _emails = emails.Publish().RefCount();
_emails.Where(email => email.From == "John").Subscribe(email => Console.WriteLine("A"));
_emails.Where(email => email.From == "Mary").Subscribe(email => Console.WriteLine("B"));