runAsyncRestartable method

void runAsyncRestartable(
  1. Future<void> task(
    1. ZenithCancellationToken token
    )
)

Runs task with a fresh ZenithCancellationToken, cancelling any previously active task's token before starting.

This models the restartable concurrency strategy: only the latest invocation's result is relevant; earlier results are discarded.

Implementation

void runAsyncRestartable(
  Future<void> Function(ZenithCancellationToken token) task,
) {
  // Cancel the prior task.
  _currentToken?.cancel();

  final token = ZenithCancellationToken();
  _currentToken = token;

  task(token)
      .then((_) {
        if (identical(_currentToken, token)) {
          _currentToken = null;
        }
      })
      .catchError((_) {
        if (identical(_currentToken, token)) {
          _currentToken = null;
        }
      });
}