createLayers static method

Future<Layer> createLayers(
  1. List<(fnInitLayer, Object?, bool)> layers, {
  2. bool currentIsModelBus = true,
  3. bool bridgeIsModelBus = false,
  4. Duration? timeout,
})

Builds a chain of layers, each running in its own isolate.

The top layer has Layer.current and the EventBusIsolate to the first worker in Layer.bottom. Every middle layer gets the Layer.top bus leading to the layer above and the Layer.bottom bus leading to the layer below, if it exists.

Each entry of layers is (init function, initial data, isModelBus), where the flag applies to that layer's Layer.current bus.

currentIsModelBus applies to the Layer.current bus of the main isolate. bridgeIsModelBus applies to every EventBusIsolate that connects two layers, so both the main-side and the worker-side bus of each bridge are model buses.

The future completes when EVERY layer of the chain is initialized, including async init functions. An init failure of any layer surfaces here as well. timeout limits the wait (unlimited by default).

Implementation

static Future<Layer> createLayers(List<(fnInitLayer, Object?, bool)> layers,
    {bool currentIsModelBus = true,
    bool bridgeIsModelBus = false,
    Duration? timeout}) async {
  if (layers.isEmpty) {
    throw EventBusException(
        'Layers.createLayers: layers list must not be empty');
  }

  _LayerInitDto storage = _LayerInitDto(
      data: layers, index: 0, bridgeIsModelBus: bridgeIsModelBus);

  Layer l = Layer(
      current: EventBus(isModelBus: currentIsModelBus),
      bottom: EventBusIsolate(
          onInit: _initLayer,
          initialData: storage,
          isModelBus: bridgeIsModelBus));
  var waitInit = (l.bottom as EventBusIsolate).waitInit;
  if (timeout != null) {
    await waitInit.timeout(timeout, onTimeout: () {
      throw EventBusException(
          'Layers.createLayers: initialization timeout ($timeout)');
    });
  } else {
    await waitInit;
  }
  return l;
}