dispose method

Future<void> dispose()

Stops the running program, waits up to 2 s, and restores the Style defaults. Safe to call more than once; never throws.

A program the body starts after this is stopped as soon as it starts, so a prompt helper returns null and the body can finish. Arbitrary work in the body cannot be cancelled. isRunning stays true until the body actually ends; done can still await its result.

Implementation

Future<void> dispose() async {
  _disposed = true;
  if (_programRunning) _program?.kill();
  final deadline = Completer<void>();
  // A command's timers are cancelled when kill ends its program.
  final timer =
      Zone.root.createTimer(const Duration(seconds: 2), deadline.complete);
  try {
    await Future.any<Object?>([_run, deadline.future]);
  } catch (_) {
    // Reported by press, waitFor, quit or done.
  } finally {
    timer.cancel();
  }
  _restoreDefaults();
}