共享單個訂閱(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"));