Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c# - How to create an Observable that caches each of the calculated items? (equivalence of Lazy<T>)

I would like to create a sequence (Observable<T>) that is able to cache the items, so the calculations inside the pipeline are processed only once. For instance:

var obs = Observable
.Range(1, 100)
.SelectMany(x => GetItemAsync(x));

I would like that having 2 subscribers, the results of the GetItemAsync are cached so the 2nd subscriber will get them from the cached values, so the method shouldn't be called at all for any subsequent subscription.

I would like to do something similar to what Lazy<T> does, but with Reactive Extensions

question from:https://stackoverflow.com/questions/65599334/how-to-create-an-observable-that-caches-each-of-the-calculated-items-equivalen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Replay operator returns an IConnectableObservable<T>, which is an IObservable<T> with an extra Connect method. This observable can be subscribed by any number of observers. It propagates to all observers all past and future notifications coming from the underlying observable, starting from the time it was connected and ending to the time it was disconnected. Here is an example:

var connectable = Observable
    .Range(1, 100)
    .SelectMany(x => GetItemAsync(x))
    .Replay();

var subscription1 = connectable.Subscribe(x => Console.WriteLine(x))
var subscription2 = connectable.Subscribe(x => Console.WriteLine(x))

var connection = connectable.Connect(); // Subscribe to the 'SelectMany' observable

//...

connection.Dispose() // Unsubscribe from the 'SelectMany' observable

This example demonstrates how to connect manually to the underlying observable, which is important when using other multicast operators like the Publish. But it's less important with the Replay operator because of its replay functionality: it doesn't matter whether it will be subscribed before or after it connects to the underlying observable. So you may choose to avoid connecting manually, and use one of the two available auto-connect operators:

  1. RefCount: connects to the underlying observable when it is subscribed for the first time, and disconnects when its last subscriber unsubscribes.

  2. AutoConnect(0): connects to the underlying observable immediately, and stays connected forever.

Example:

var observable = Observable
    .Range(1, 100)
    .SelectMany(x => GetItemAsync(x))
    .Replay()
    .AutoConnect(0);

// call observable.Subscribe any time and as many times you want

Both RefCount and AutoConnect also disconnect automatically when the underlying observable completes successfully or with an error.

Connecting and disconnecting more than once is not a supported scenario, and may produce unexpected results. If you want to disconnect and reconnect, you'd better use a different Replay connectable each time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...