Situation
I have a system where one request produces two responses. The request and responses have corresponding observables:
IObservable<RequestSent> _requests;
IObservable<MainResponseReceived> _mainResponses;
IObservable<SecondResponseReceived> _secondaryResponses;
it is guaranteed that RequestSent
event occurs earlier than MainResponseReceived
and SecondaryResponseReceived
but the responses come in random order.
What I have
Originally I wanted handler that handles both responses, so I zipped the observables:
_requests
.SelectMany(async request =>
{
var main = _mainResponses.FirstAsync(m => m.Id == request.Id);
var secondary = _secondaryResponses.FirstAsync(s => s.Id == request.Id);
var zippedResponse = main.Zip(secondary, (m, s) => new MainAndSecondaryResponseReceived {
Request = request,
Main = m,
Secondary = s
});
return await zippedResponse.FirstAsync(); ;
})
.Subscribe(OnMainAndSecondaryResponseReceived);
What I need
Now I need to handle also MainResponseReceived
without waiting for SecondaryResponseRecieved and it must be guaranteed, that the OnMainResponseRecieved completes before OnMainAndSecondaryResponseReceived
is called
How to define the two subscriptions, please?
Test case 1:
RequestSent
occursMainResponseReceived
occurs -> OnMainResponseReceived is calledSecondaryResponseReceive
d occurs -> OnMainAndSecondaryResponseReceived is called
Test case 2:
RequestSent
occursSecondaryResponseReceived
occursMainResponseReceived occurs
-> OnMainResponseReceived is called -> OnMainAndSecondaryResponseReceived is called