chatEvents method

Stream<ChatEvents> chatEvents(
  1. ChatId chatId,
  2. ChatVersion? ver,
  3. FutureOr<ChatVersion?> onVer()
)

Subscribes to ChatEvents of the specified Chat.

Implementation

Stream<ChatEvents> chatEvents(
  ChatId chatId,
  ChatVersion? ver,
  FutureOr<ChatVersion?> Function() onVer,
) {
  Log.debug('chatEvents($chatId, $ver, onVer)', '$runtimeType');

  return _graphQlProvider.chatEvents(chatId, ver, onVer).asyncExpand((
    event,
  ) async* {
    Log.trace('chatEvents($chatId): ${event.data}', '$runtimeType');

    var events = ChatEvents$Subscription.fromJson(event.data!).chatEvents;
    if (events.$$typename == 'SubscriptionInitialized') {
      events as ChatEvents$Subscription$ChatEvents$SubscriptionInitialized;
      yield const ChatEventsInitialized();
    } else if (events.$$typename == 'Chat') {
      final chat = events as ChatEvents$Subscription$ChatEvents$Chat;
      final data = _chat(chat);
      yield ChatEventsChat(data.chat);
    } else if (events.$$typename == 'ChatEventsVersioned') {
      var mixin =
          events as ChatEvents$Subscription$ChatEvents$ChatEventsVersioned;
      yield ChatEventsEvent(
        ChatEventsVersioned(mixin.events.map(chatEvent).toList(), mixin.ver),
      );
    }
  });
}