dispose method

Future<void> dispose()

Implementation

Future<void> dispose() async {
  assert(_alive);

  // first, delete all listeners.
  // then, send the stop command.
  //
  // don't set notify so we don't signal the
  // eventfd 10000 times.
  final futures = [
    for (final listener in _handlers.values)
      _delete(
        listener: listener,
        notify: false,
      ),
    _sendCmd(_StopCmd(seq: Capability()))
  ];

  // signal the event fd once.
  _notifyIsolate();

  // wait for all commands to complete.
  await Future.wait(futures);

  assert(_isolate != null);
  await _isolateExitCompleter.future;

  ffi.calloc.free(_notifyBuffer);
  _receivePortSubscription.cancel();
  _libc.close(_epollFd);
  _libc.close(_eventFd);

  _alive = false;
}