There is already a good question on database polling using Reactive (Database polling with Reactive Extensions)
I have a similar question, but with a twist: I need to feed a value from the previous result into the next request. Basically, I would like to poll this:
interface ResultSet<T>
{
int? CurrentAsOfHandle {get;}
IList<T> Results {get;}
}
Task<ResultSet<T>> GetNewResultsAsync<T>(int? previousRequestHandle);
The idea is that this returns all new items since the previous request
- every minute I would like to call
GetNewResultsAsync
- I would like to pass the
CurrentAsOf
from the previous call as the argument to thepreviousRequest
parameter - the next call to
GetNewResultsAsync
should actually happen one minute after the previous one
Basically, is there a better way than:
return Observable.Create<IMessage>(async (observer, cancellationToken) =>
{
int? currentVersion = null;
while (!cancellationToken.IsCancellationRequested)
{
MessageResultSet messageResultSet = await ReadLatestMessagesAsync(currentVersion);
currentVersion = messageResultSet.CurrentVersionHandle;
foreach (IMessage resultMessage in messageResultSet.Messages)
observer.OnNext(resultMessage);
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
}
});
Also note that this version allows the messageResultSet
to be collected while waiting for the next iteration (for example, I thought maybe I could use Scan
to pass the previous result set object into the next iteration)