queryChannels method

Stream<List<Channel>> queryChannels({
  1. Filter? filter,
  2. List<SortOption<ChannelState>>? channelStateSort,
  3. bool state = true,
  4. bool watch = true,
  5. bool presence = false,
  6. int? memberLimit,
  7. int? messageLimit,
  8. PaginationParams paginationParams = const PaginationParams(),
  9. bool waitForConnect = true,
})

Requests channels with a given query.

Implementation

Stream<List<Channel>> queryChannels({
  Filter? filter,
  List<SortOption<ChannelState>>? channelStateSort,
  bool state = true,
  bool watch = true,
  bool presence = false,
  int? memberLimit,
  int? messageLimit,
  PaginationParams paginationParams = const PaginationParams(),
  bool waitForConnect = true,
}) async* {
  if (!_connectionIdManager.hasConnectionId) {
    // ignore: parameter_assignments
    watch = false;
  }

  final hash = generateHash([
    filter,
    channelStateSort,
    state,
    watch,
    presence,
    memberLimit,
    messageLimit,
    paginationParams,
  ]);

  if (_queryChannelsStreams.containsKey(hash)) {
    yield await _queryChannelsStreams[hash]!;
  } else {
    final channels = await queryChannelsOffline(
      filter: filter,
      channelStateSort: channelStateSort,
      paginationParams: paginationParams,
    );
    if (channels.isNotEmpty) yield channels;

    try {
      final newQueryChannelsFuture = queryChannelsOnline(
        filter: filter,
        sort: channelStateSort,
        state: state,
        watch: watch,
        presence: presence,
        memberLimit: memberLimit,
        messageLimit: messageLimit,
        paginationParams: paginationParams,
        waitForConnect: waitForConnect,
      ).whenComplete(() {
        _queryChannelsStreams.remove(hash);
      });

      _queryChannelsStreams[hash] = newQueryChannelsFuture;

      yield await newQueryChannelsFuture;
    } catch (_) {
      if (channels.isEmpty) rethrow;
    }
  }
}