start method
Implementation
Future<void> start(OnnxIsolateType type) async {
if (_starting != null) {
await _starting; // Wait for the pending start to finish.
return;
}
if (_isolate != null) {
return;
}
// The _starting flag is set with a completer which will complete when
// the isolate start up is fully finished (including setting the _sendPort).
final Completer<void> completer = Completer<void>();
_starting = completer.future;
final receivePort = ReceivePort();
_isolate = await Isolate.spawn(
switch (type) {
OnnxIsolateType.miniLm => ortMiniLmIsolateEntryPoint,
OnnxIsolateType.minishLab => ortMinishLabIsolateEntryPoint,
},
receivePort.sendPort,
onError: receivePort.sendPort, // Handle isolate errors.
);
// Wait for the SendPort from the new isolate.
final sendPort = await receivePort.first as SendPort;
_sendPort = sendPort;
// Mark the start process as complete.
completer.complete();
_starting = null;
}