events method

Stream<LiveEvent> events()

Streams the events this app posts to the nylo VM service stream.

Implementation

Stream<LiveEvent> events() {
  late final StreamController<LiveEvent> controller;
  StreamSubscription<JsonRpcNotification>? subscription;

  controller = StreamController<LiveEvent>(
    onListen: () async {
      subscription = client.notifications.listen((notification) {
        final LiveEvent? event = parseStreamEvent(notification, 'nylo');
        if (event != null) controller.add(event);
      }, onDone: controller.close);
      try {
        await client.call('streamListen', {'streamId': 'nylo'});
      } on JsonRpcException catch (e) {
        // 103: the stream is already subscribed on this connection.
        if (e.code != 103) controller.addError(e);
      } catch (e) {
        controller.addError(e);
      }
    },
    onCancel: () async {
      await subscription?.cancel();
      if (client.isClosed) return;
      try {
        await client.call('streamCancel', {'streamId': 'nylo'});
      } catch (_) {
        // Nothing to clean up if the app is already gone.
      }
    },
  );
  return controller.stream;
}