initialize method

Future<void> initialize()

Initializes the isolate pool

Implementation

Future<void> initialize() async {
  for (int i = 0; i < _poolSize; i++) {
    final receivePort = ReceivePort();
    final completer = Completer<SendPort>();

    receivePort.listen((message) {
      if (message is SendPort) {
        completer.complete(message);
      } else if (message is _IsolateResponse) {
        // Create a new completer and complete it with the result
        final response = message.withNewCompleter();
        response.completer.complete(message.result);
        message.completer.complete(message.result);
      }
    });

    final isolate = await Isolate.spawn(
      _isolateEntryPoint,
      receivePort.sendPort,
    );

    _isolates.add(isolate);
    _receivePorts.add(receivePort);
    _sendPorts.add(await completer.future);
  }
}