Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I promised to add the solution I came up with myself, so here it is. As long as I haven't found anything better I'll use this, although I still think there has to be a better way :)</p> <p>I use a class that uses a <code>ConcurrentDictionary</code> to keep the latest value from each registered observable. When an observable is unregistered, its latest value is removed again, as well as the subscription associated with it.</p> <p>When any registered observable generates a value, all latest values are collected and sent to a <code>Subject</code>.</p> <pre><code>public class DynamicCombineLatest&lt;T&gt; { private readonly IDictionary&lt;IObservable&lt;T&gt;, T&gt; _latestValues = new ConcurrentDictionary&lt;IObservable&lt;T&gt;, T&gt;(); private readonly IDictionary&lt;IObservable&lt;T&gt;, IDisposable&gt; _subscriptions = new ConcurrentDictionary&lt;IObservable&lt;T&gt;, IDisposable&gt;(); private readonly ISubject&lt;IEnumerable&lt;T&gt;&gt; _result = new Subject&lt;IEnumerable&lt;T&gt;&gt;(); public void AddObservable(IObservable&lt;T&gt; observable) { var subscription = observable.Subscribe(t =&gt; { _latestValues[observable] = t; _result.OnNext(_latestValues.Values); }); _subscriptions[observable] = subscription; } public void RemoveObservable(IObservable&lt;T&gt; observable) { _subscriptions[observable].Dispose(); _latestValues.Remove(observable); _subscriptions.Remove(observable); } public IObservable&lt;IEnumerable&lt;T&gt;&gt; Result { get { return _result; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload