I have a 'hot' data source, which exposes an event containing the data I'm interested in.
I've created a 'wrapper' which uses Observable.FromEventPattern
to expose my data source as an Observable.
The problem I'm having is that using the wrapper, I always lose the first few data items, but only the first time I run it.
Some code:
Internally, the Wrapper class uses a RoutedObservable
(successor of this) to allow Subscribers to 'sign up' before the Source has been wired up.
Wrapper.cs
RoutedObservable<Data> _Data = new RoutedObservable<Data>;
_Data.SetSource(Observable.FromEventPattern<EventArgs<Data>>
(
h => _Source.DataReceived += h,
h => _Source.DataReceived -= h
)
.Select(e => e.EventArgs.Data));
public IObservable<Data> Data { get { return _Data.Publish().RefCount(); } }
public void Start { _Source.Start(); }
public void Stop { _Source.Stop(); }
Test.cs
var _list = new List<Data>();
_Wrapper.Start();
var lastData = await _Wrapper.Data
.Do(_list.Add)
.Select(SomeConversion)
.Take(NumberOfSamples)
.LastAsync();
//Check lastData.Id == NumberOfSamples
// (give or take an off-by-one error)
//if Check not OK, have a look inside _list too see what's missing
_Wrapper.Stop();
(The Data.Id
property is re-initialised to zero by the Source on every Stop/Start)
On first run, the _list
has around 8 items missing at the beginning, but on subsequent runs, everything is ok.
Have you already seen something like this and have an idea what the cause may be? Or an idea of what to change/try to work out where the problem is coming from?