runGuarded method

void runGuarded(
  1. Future<void> task()
)

Runs task guarded so that scope disposal automatically cancels it.

A ZenithCancellationToken is created and linked; if dispose is called before task completes, the token is cancelled, stopping any registered onCancel callbacks (e.g. aborting HTTP requests).

Implementation

void runGuarded(Future<void> Function() task) {
  if (_isDisposed || _isClosing) return;

  final token = ZenithCancellationToken();
  _activeTokens.add(token);

  Future.sync(task).whenComplete(() {
    _activeTokens.remove(token);
  });
}