spawn static method

Future<Nvim> spawn({
  1. String nvimBinary = 'nvim',
  2. List<String> commandArgs = const ['--embed'],
  3. NvimHandler? onNotify,
  4. NvimHandler? onRequest,
})

Implementation

static Future<Nvim> spawn(
    {String nvimBinary = 'nvim',
    List<String> commandArgs = const ['--embed'],
    NvimHandler? onNotify,
    NvimHandler? onRequest}) async {
  var nvim = Nvim._spawn(nvimBinary, commandArgs);
  if (onRequest != null) {
    nvim.onRequest = onRequest;
  }

  if (onNotify != null) {
    nvim.onNotify = onNotify;
  }

  final receivePort = ReceivePort();
  nvim._nvimIsolate =
      await Isolate.spawn(eventLoopIsolate, receivePort.sendPort);
  nvim._nvimRxStream = receivePort.asBroadcastStream();
  nvim._nvimTxPort = await nvim._nvimRxStream!.first;

  nvim._nvimRxStream!.listen((msg) {
    if (msg is SendPort) {
      nvim._nvimTxPort = msg;
    } else if (!(msg is _NvimIsolateMsg)) {
      throw 'This should not happen, maybe open an issue at https://github.com/smolck/dart-nvim-api/issues';
    } else if (msg is _NvimIsolateMsg) {
      switch (msg.msgType) {
        case _NvimIsolateMsgType.Response:
          nvim._waiting[msg.maybeResponseId]?.complete(msg.data);
          break;
        case _NvimIsolateMsgType.Notification:
          nvim._onNotify(nvim, msg.maybeMethod!, msg.data);
          break;
        case _NvimIsolateMsgType.Request:
          nvim._onRequest(nvim, msg.maybeMethod!, msg.data);
          break;
      }
    }
  });

  return nvim;
}