getChannelStates method

  1. @override
Future<List<ChannelState>> getChannelStates({
  1. Filter? filter,
  2. List<SortOption<ChannelState>>? channelStateSort,
  3. PaginationParams? paginationParams,
})

Get all the stored ChannelStates

Optionally, pass filter, sort, paginationParams for filtering out states.

Implementation

@override
Future<List<ChannelState>> getChannelStates({
  Filter? filter,
  List<SortOption<ChannelState>>? channelStateSort,
  PaginationParams? paginationParams,
}) async {
  assert(_debugIsConnected, '');
  assert(() {
    if (channelStateSort?.any((it) => it.comparator == null) ?? false) {
      throw ArgumentError(
        'SortOption requires a comparator in order to sort',
      );
    }
    return true;
  }(), '');

  _logger.info('getChannelStates');

  final channels = await db!.channelQueryDao.getChannels(filter: filter);

  final channelStates = await Future.wait(
    channels.map((e) => getChannelStateByCid(e.cid)),
  );

  // Sort the channel states
  var comparator = _defaultChannelStateComparator;
  if (channelStateSort != null && channelStateSort.isNotEmpty) {
    comparator = _combineComparators(
      channelStateSort.map((it) => it.comparator).withNullifyer,
    );
  }
  channelStates.sort(comparator);

  final offset = paginationParams?.offset;
  if (offset != null && offset > 0 && channelStates.isNotEmpty) {
    channelStates.removeRange(0, offset);
  }

  if (paginationParams?.limit != null) {
    return channelStates.take(paginationParams!.limit).toList();
  }

  return channelStates;
}