I have the following two observables
System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://test.com/data.xml");
req.Method = "HEAD";
var ob = Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse);
ob().Select(x => x).Select(x => x.Headers["Last-Modified"]).DistinctUntilChanged(x => x);
Observable
.Interval(TimeSpan.FromSeconds(1.0))
.Select(_ => XDocument.Load("http://test.com/data.xml"));
I would like it that the XDocument observable is only executed when "last-modified" header is greater then the previously requested document any ideas?
Firstly .Select(x=>x) is a no-op so you can remove that.
I would change the code up a little bit. First lets break it down into its constituent parts:
1) The Timer. Every second poll the server.
var poll = Observable.Interval(TimeSpan.FromSeconds(1));
2) The call to get the header
var lastModified = Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse).Select(x => x.Headers["Last-Modified"]);
3) The Select to get the Document
.Select(_ => XDocument.Load("http://test.com/data.xml"));
We should be able to compose that nicely:
var lastModified = from interval in Observable.Interval(TimeSpan.FromSeconds(1))
from response in Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse)
select response.Headers["Last-Modified"];
var data = lastModified.DistinctUntilChanged().Select(_ => XDocument.Load("http://test.com/data.xml"));
data.Subscribe(dataXml=>
{
Console.WriteLine("Data has changed!");
Console.WriteLine(datXml);
});
Cavet I just typed that straight into the browser. I would be amazing if it compiles.