UPDATE #2
public abstract Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(string symbol, Action<DataEvent<FTXStreamTicker>> handler, CancellationToken ct = default(CancellationToken))
I wasn't able to call this method.
If you look at the signature of the method then you can see there is an extra input parameter called symbol
. The SubscribeToAsync
's methodSelector
is unaware of this.
In order to support the call of SubscribeToTickerUpdatesAsync
or SubscribeToTradeUpdatesAsync
like this:
await SubscribeWithSymbolToAsync<FTXStreamTicker>(stream => stream.SubscribeToTickerUpdatesAsync, "Ticker", cancellationToken);
await SubscribeWithSymbolToAsync<IEnumerable<FTXTrade>>(stream => stream.SubscribeToTradeUpdatesAsync, "Trade", cancellationToken);
we need the following trick:
private async Task<UpdateSubscription?> SubscribeWithSymbolToAsync<T>(
Func<IFTXSocketClientStreams, Func<string, Action<DataEvent<T>>, CancellationToken, Task<CallResult<UpdateSubscription>>>> methodSelector,
string symbol, CancellationToken token)
{
var asyncMethod = (Action<DataEvent<T>> handler, CancellationToken cToken) =>
methodSelector(_socketClient.Streams)(symbol, handler, cToken);
var subscription = await asyncMethod(data => _logger.LogInformation(symbol + ": {@Data}", data.Data), token);
return SubscribeToEvents(subscription);
}
private async Task<UpdateSubscription?> SubscribeToAsync<T>(
Func<IFTXSocketClientStreams, Func<Action<DataEvent<T>>, CancellationToken, Task<CallResult<UpdateSubscription>>>> methodSelector,
Action<DataEvent<T>> handler, CancellationToken token)
{
var asyncMethod = methodSelector(_socketClient.Streams);
var subscription = await asyncMethod(handler, token);
return SubscribeToEvents(subscription);
}
private UpdateSubscription SubscribeToEvents(CallResult<UpdateSubscription> subscription)
{
if (!subscription.Success) return null;
subscription.Data.ConnectionLost += () => _logger.LogError("Connection lost");
subscription.Data.ConnectionRestored += _ => _logger.LogInformation("Connection restored");
return subscription.Data;
}
- The
SubscribeWithSymbolToAsync
binds thesymbol
formal parameter of themethodSelector
to the providedsymbol
parameter- In other words, we convert a
Func<T1, T2, T3>
to aFunc<T2, T3>
by supplyingT1
- In other words, we convert a
- The
SubscribeWithSymbolToAsync
does not rely onSubscribeToAsync
so I extract the common part- The
SubscribeToEvents
handles the event subscribtions
- The