private async Task GetTradesStream(IRequestContext context, IMessage message)
{
Log.Debug("Received GetTradesStream from {username}", context.UserSession.Username ?? "<UNKNOWN USER>");
var replyTo = message.ReplyTo;
var endPoint = await _broker.GetPrivateEndPoint<TradesDto>(replyTo);
_subscription = _service.GetTradesStream()
.Select(x =>
{
if (x.IsStateOfTheWorld && x.Trades.Count > MaxSotwTrades)
{
return new TradesDto(new List<TradeDto>(x.Trades.Skip(x.Trades.Count - MaxSotwTrades)), true, false);
}
return x;
})
.Do(o =>
{
Log.Debug(
$"Sending trades update to {replyTo}. Count: {o.Trades.Count}. IsStateOfTheWorld: {o.IsStateOfTheWorld}. IsStale: {o.IsStale}");
})
.TakeUntil(endPoint.TerminationSignal)
.Finally(() => Log.Debug("Tidying up subscription from {replyTo}.", replyTo))
.Subscribe(endPoint);
}