I have a collection of items of following class:
public class Event
{
public DateTimeOffset Timestamp;
public object Data;
}
I want to create IObservable<Event>
where each item is published at the time of Timestamp
in the future. Is this possible with Observable.Delay
or do I have to write my own IObservable<T>
implementation?
I will mention that this structure is something like a log file. There can be tens of thousands of Event
items, but only 1-2 are to be published per second.
While my first answer is working as intended, performance of creating the observable sequence is not ideal with hundreds of thousands of events - you pay substantial initialization cost (order of 10 seconds on my machine).
To improve performance, taking advantage of already sorted nature of my data, I implemented custom IEnumerable<Event>
that is looping through events, yielding and sleeping between them. With this IEnumerable
one can easily call ToObservable<T>
and it works as intended:
IObservable<Event> CreateSimulation(IEnumerable<Event> events)
{
IEnumerable<Event> simulation()
{
foreach(var ev in events)
{
var now = DateTime.UtcNow;
if(ev.Timestamp > now)
{
Thread.Sleep(ev.Timestamp - now);
}
yield return ev;
}
}
return simulation().ToObservable();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With