Search code examples
system.reactivereactive-programmingreactive-extensions-js

Rx Publish() breaks IObservable of XDocuments


I have the following Rx Query that produces a IObservable problem is it does not work with Publish() so as the number of subscribers increases so does the memmory load

// Works
Observable
    .Interval(TimeSpan.FromSeconds(1.0))
    .Select(_ => XDocument.Load("http://test.com/data.xml"));

// Broken
Observable
    .Interval(TimeSpan.FromSeconds(1.0))
    .Select(_ => XDocument.Load("http://test.com/data.xml")).Publish();

Is there a better way of createing such an Observable?


Solution

  • Until you call Connect(), the IConnectableObservable is disconnected from its source, so nothing is going to happen (Interval won't start its timer) until somebody Subscribe's to it. Connect() will make the Publish subscribe to its source.