countMessages method

Future<CountMessagesResult> countMessages(
  1. dynamic channels, {
  2. Keyset? keyset,
  3. String? using,
  4. Timetoken? timetoken,
})

Get multiple channels' message count using one call.

channels can either be a Map<String, Timetoken> or Set<String>:

  • if you want to count messages in all channels up to a common timetoken, pass in a Set<String> and a named parameter timetoken.
  • if you want to specify separate timetoken for each channel, pass in a Map<String, Timetoken>. Additionally, if a value in the map is null, it will use a timetoken from a named parameter timetoken.

Implementation

Future<CountMessagesResult> countMessages(dynamic channels,
    {Keyset? keyset, String? using, Timetoken? timetoken}) {
  keyset ??= _core.keysets[using];

  var params = CountMessagesParams(keyset);
  if (channels is Set<String>) {
    Ensure(timetoken)
        .isNotNull('When you pass in a Set, timetoken cannot be null.');

    params =
        CountMessagesParams(keyset, channels: channels, timetoken: timetoken);
  } else if (channels is Map<String, Timetoken>) {
    params = CountMessagesParams(keyset,
        channelsTimetoken:
            channels.map((key, value) => MapEntry(key, value)));
  } else {
    Ensure.fail('invalid-type', 'channels',
        ['Set<String>', 'Map<String, Timetoken>']);
  }

  return defaultFlow(
      keyset: keyset,
      core: _core,
      params: params,
      serialize: (object, [_]) => CountMessagesResult.fromJson(object));
}