lookup method

Future<List<T>> lookup({
  1. Iterable<Object?>? tags,
  2. int? max,
})

Finds a number of elements that have all the desired tags.

If tags is omitted or empty, any element of the registry can be returned.

If max is specified, it must be greater than zero. In that case, at most the first max results are returned, in whatever order the registry finds its results. Otherwise all matching elements are returned.

Implementation

Future<List<T>> lookup({Iterable<Object?>? tags, int? max}) async {
  if (max != null && max < 1) {
    throw RangeError.range(max, 1, null, 'max');
  }
  if (tags != null) tags = tags.toList(growable: false);
  var completer = Completer<List<T>>();
  var port = singleCompletePort(completer, callback: (List response) {
    // Response is even-length list of (id, element) pairs.
    var cache = _cache;
    var count = response.length ~/ 2;
    var result = List<T?>.filled(count, null);
    for (var i = 0; i < count; i++) {
      var id = response[i * 2] as int;
      T? element = response[i * 2 + 1] as T;
      element = cache.register(id, element) as T?;
      result[i] = element;
    }
    return result;
  }, timeout: _timeout);
  _commandPort.send(list4(_findValue, tags, max, port));
  return await completer.future;
}