initRuntime method

Future<Stream<DataPackage>> initRuntime(
  1. Map<String, List<DataPackage>> modules,
  2. StreamController<DataPackage> outputController
)

Implementation

Future<Stream<DataPackage>> initRuntime(
    Map<String, List<DataPackage>> modules,
    StreamController<DataPackage> outputController) async {
  Logger.logInfo("Initruntime got modules: " + modules.toString());
  final req = InitRuntimeRequest(
      runtime: Runtime.RUNTIME_DART,
      modules: modules.entries.map((e) => InitRuntimeRequest_ModuleChannels(
          moduleId: e.key, channelPackets: e.value)));

  Logger.logInfo("Runtime request: " + req.toString());
  await _stub.initRuntime(req);

  final listenCalled = Completer<void>();

  final listenFunc = outputController.onListen;
  outputController.onListen = () async {
    listenFunc?.call();
    await Future.delayed(Duration.zero, () {
      listenCalled.complete();
    });
  };

  final pingReceived = Completer<void>();
  _responseStream = _stub.sendReceivePackages(outputController.stream);

  final ret = _wrapInputStream(_responseStream!, pingReceived);

  await listenCalled.future;

  // Send the ping to the server. The return ping is caught in wrapInputStream.
  final pingPkt = DataPackage()
    ..controlVal = ControlPackage(
        ctrlType: CtrlType.CTRL_RUNTIME_PING, runtime: Runtime.RUNTIME_DART);
  _outputController = outputController;
  outputController.add(pingPkt);
  return ret;
}