resizeFunctionWorkers method

void resizeFunctionWorkers(
  1. int count,
  2. WorkerFunction function, {
  3. Map? metadata,
})

Implementation

void resizeFunctionWorkers(
  int count,
  WorkerFunction function, {
  Map? metadata,
}) async {
  if (sockets.length < count) {
    for (var i = sockets.length + 1; i <= count; i++) {
      var sock = createWorker(
        function,
        metadata: {'workerId': i}..addAll(metadata ?? {}),
      );
      sock._pool = this;
      sock.onReceivedMessageHandler = (dynamic msg) {
        if (onMessageReceivedHandler != null) {
          onMessageReceivedHandler!(i, msg);
        }
      };
      await sock.init();
      sockets.add(sock);
    }
  } else {
    reduceWorkers(count);
  }
}