addInstance method

Future<PooledInstanceProxy> addInstance(
  1. PooledInstance instance, [
  2. PooledCallbak? callbak
])

Transfer PooledInstance to one of isolates, call it's PooledInstance.init method and make it avaialble for communication via the PooledInstanceProxy returned

Implementation

Future<PooledInstanceProxy> addInstance(PooledInstance instance,
    [PooledCallbak? callbak]) async {
  var pi = PooledInstanceProxy._(instance._instanceId, this, callbak);

  var min = 10000000;
  var minIndex = 0;

  for (var i = 0; i < numberOfIsolates; i++) {
    var x = _pooledInstances.entries
        .where((e) => e.value.isolateIndex == i)
        .fold(0, (int previousValue, _) => previousValue + 1);
    if (x < min) {
      min = x;
      minIndex = i;
    }
  }

  _pooledInstances[pi._instanceId] = _InstanceMapEntry(pi, minIndex);

  var completer = Completer<PooledInstanceProxy>();
  creationCompleters[pi._instanceId] = completer;

  _isolateSendPorts[minIndex]!.send(instance);

  return completer.future;
}