LibraStack / UnityMvvmToolkit

Brings data-binding to your Unity project
MIT License
469 stars 27 forks source link

AsyncCommand cancellation issue. #4

Closed ChebanovDD closed 1 year ago

ChebanovDD commented 1 year ago

Bug description

When multiple async commands are running at the same time, cancellation only works for the last async command.

Minimal reproduction code and steps

  1. Execute StartCommand several times
  2. Execute CancelCommand
public MainViewModel()
{
    StartCommand = new AsyncCommand(StartAsync).WithCancellation();
    CancelCommand = new Command(() => StartCommand.Cancel());
}

public IAsyncCommand StartCommand { get; }

public ICommand CancelCommand { get; }

private async UniTask StartAsync(CancellationToken cancellationToken)
{
    while (cancellationToken.IsCancellationRequested == false)
    {
        await UniTask.Delay(1000, cancellationToken: cancellationToken);
        Debug.Log(nameof(StartAsync));
    }
}

Current result

Only the last command will be canceled.

Expected result

All running commands should be canceled.