computeIDs method
FutureOr<List<(D, V)> >
computeIDs(
- List<
D> ids, - ComputeCallIDs<
D, V> call, { - PosComputeCall<
List< ? posCompute,V> > - bool resolve = true,
Computes values for the given ids, reusing and composing the
computations returned by getByIDs.
Overlapping or in-flight ID computations are shared, and only missing IDs trigger new computation.
The returned list respects the order of ids and contains (ID, value)
pairs aligned with the input sequence.
The result may be returned synchronously or asynchronously.
Implementation
FutureOr<List<(D, V)>> computeIDs(List<D> ids, ComputeCallIDs<D, V> call,
{PosComputeCall<List<V>>? posCompute, bool resolve = true}) {
var computers =
getByIDs(ids, call, posCompute: posCompute, resolve: resolve);
return computers.computeAll().resolveMapped((results) {
var allComputations = results.entries.map((e) {
var computedIDs = e.key;
var computedValues = e.value;
var intersectionIDs = computedIDs.intersection(ids);
if (intersectionIDs.isEmpty) return <(D, V)>[];
if (intersectionIDs.length == computedIDs.length &&
computedIDs.length == computedValues.length) {
var values = List.generate(computedIDs.length, (i) {
var id = computedIDs[i];
var v = computedValues[i];
return (id, v);
});
return values;
}
var intersectionValues = computedIDs.getValuesByIndexes(
intersectionIDs.map((e) => e.$1),
computedValues,
);
return intersectionValues;
}).toList();
if (allComputations.isEmpty) {
return [];
}
final cmp = compare ?? _Comparer._defaultCompare;
List<(D, V)> allComputedValues;
if (allComputations.length == 1) {
// Already sorted:
allComputedValues = allComputations.first;
} else {
allComputedValues = allComputations.expand((l) => l).toList();
// Ensure sorted:
if (allComputedValues.length > 1) {
allComputedValues.sort((a, b) => cmp(a.$1, b.$1));
}
}
if (allComputedValues.length <= 1) {
assert(allComputedValues.isEmpty ||
ids.contains(allComputedValues.first.$1));
return allComputedValues;
}
var idsValues = ids
.map((id) => allComputedValues.binarySearch(id, cmp))
.nonNulls
.toList();
return idsValues;
});
}