eventLoopIsolate static method

void eventLoopIsolate(
  1. SendPort sendPort
)

Implementation

static void eventLoopIsolate(SendPort sendPort) async {
  final receivePort = ReceivePort();
  final nvimProc = await Process.start('nvim', ['--embed']);

  // Pass any data sent from main thread to neovim process.
  receivePort.listen((data) {
    nvimProc.stdin.add(data);
  });

  // First message gives a way for main isolate to communicate with this one.
  sendPort.send(receivePort.sendPort);

  // Event loop.
  await for (final data in nvimProc.stdout) {
    final List<dynamic> deserialized = mpack
        .deserialize(Uint8List.fromList(data), extDecoder: ExtTypeDecoder());
    switch (deserialized[0]) {
      case RESPONSE:
        if (deserialized[2] != null) {
          // TODO(smolck)
          // Throw any errors from Neovim.
          throw deserialized[2][1];
        }
        sendPort.send(
          _NvimIsolateMsg(
            msgType: _NvimIsolateMsgType.Response,
            data: deserialized[3],
            maybeResponseId: deserialized[1],
          ),
        );
        break;
      case REQUEST:
        sendPort.send(
          _NvimIsolateMsg(
            msgType: _NvimIsolateMsgType.Request,
            maybeMethod: deserialized[2],
            data: deserialized[3],
          ),
        );
        break;
      case NOTIFICATION:
        sendPort.send(
          _NvimIsolateMsg(
            msgType: _NvimIsolateMsgType.Notification,
            maybeMethod: deserialized[1],
            data: deserialized[2],
          ),
        );
        break;
    }
  }
}