start method

Future<void> start(
  1. WolfDefenderBundle bundle
)

Implementation

Future<void> start(WolfDefenderBundle bundle) async {
  if (_sendPort != null) return;
  if (_starting != null) return _starting;
  final completer = Completer<void>();
  _starting = completer.future;
  final handshake = ReceivePort();
  try {
    _isolate = await Isolate.spawn(
      _entryPoint,
      handshake.sendPort,
      onError: handshake.sendPort,
    );
    final first = await handshake.first;
    if (first is! SendPort) {
      throw Exception('Failed to start the WolfDefender isolate: $first');
    }
    _sendPort = first;
    await _request<void>(
      _InitializeMessage(bundle, fonnxOrtDylibPathOverride),
    );
    completer.complete();
  } catch (error, stackTrace) {
    completer.completeError(error, stackTrace);
    // Nothing else waits on this future when the caller gets the error.
    unawaited(completer.future.catchError((_) {}));
    _isolate?.kill();
    _isolate = null;
    _sendPort = null;
    rethrow;
  } finally {
    handshake.close();
    _starting = null;
  }
}