TelegramBots / Telegram.Bot.Extensions.Polling

Provides ITelegramBotClient extensions for polling updates
MIT License
46 stars 12 forks source link

Add cancellation token to YieldUpdatesAsync #3

Closed tuscen closed 3 years ago

tuscen commented 4 years ago

Current implementation doesn't allow the following common pattern:

await foreach (var update in receiver.YieldUpdatesAsync().WithCancellation(cancellationToken))
{
    // ... 
}

To allow this to work YieldUpdatesAsync needs to have cancellation token as a parameter with an attribute:

public interface IYieldingUpdateReceiver
{
    int PendingUpdates { get; }
    IAsyncEnumerable<Update> YieldUpdatesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default);
}

This will allow uniform cancellation support in IYieldingUpdateReceiver implementations instead of ad-hoc params in different implementations.

MihaZupan commented 4 years ago

Looks good, I will look into implementing it.

tuscen commented 4 years ago

Do you think if there's another way of controlling receiving thread in the queued update receiver other than StartReceiving/StopReceiving? I've tried to implement cancellation token support to YieldUpdatesAsync and with the blocking receiver it was straightforward. But with the queued one the result is a complete mess from an API standpoint when you have two ways of stopping receiving thread.

tuscen commented 3 years ago

Fixed in #7