getByIDs method

Map<ComputeIDs<D>, TimedComputeOnce<List<V>>> getByIDs(
  1. List<D> ids,
  2. ComputeCallIDs<D, V> call, {
  3. PosComputeCall<List<V>>? posCompute,
  4. bool resolve = true,
})

Returns computations for the given ids, reusing any overlapping in-flight computations.

  • Only IDs not already being computed will trigger a new call.
  • posCompute, if provided, is invoked after each computation resolves.
  • If resolve is true, computations may resolve eagerly.

Returns a map of computations keyed by their ID sets.

Each entry represents a computation responsible for a subset of ids. Existing in-flight computations are reused, and a new computation is created only for IDs not currently being computed.

The returned map contains all computations required to fully resolve the requested ids.

See computeIDs.

Implementation

Map<ComputeIDs<D>, TimedComputeOnce<List<V>>> getByIDs(
    List<D> ids, ComputeCallIDs<D, V> call,
    {PosComputeCall<List<V>>? posCompute, bool resolve = true}) {
  var calling = _calls.entries.where((e) => e.key.containsAny(ids)).toList();

  var callingIDs = calling.map((c) => c.key.ids);

  final compare = this.compare ?? _Comparer._defaultCompare;

  var idsNotCalling = ids.toList();

  idsNotCalling.sort((a, b) => compare(a, b));

  for (var callIDs in callingIDs) {
    for (var id in callIDs) {
      var idx = _Comparer.binarySearchIndex(idsNotCalling, id, compare);
      if (idx >= 0) {
        idsNotCalling.removeAt(idx);
      }
    }
  }

  if (idsNotCalling.isEmpty) {
    var computers = Map.fromEntries(calling);
    return computers;
  }

  final idsNotCallingKey =
      ComputeIDs(idsNotCalling, compare: compare, hash: hash);

  var newComputer = get(
    idsNotCallingKey,
    () => call(idsNotCallingKey.ids),
    posCompute: posCompute,
    resolve: resolve,
  );

  var computers = Map.fromEntries([
    MapEntry(idsNotCallingKey, newComputer),
    ...calling,
  ]);

  return computers;
}