ResultDeduplicator<K, V> class
Ensures only one Future<Result<V>> is in flight per key at any time.
When run is called concurrently with the same key, all callers share the same underlying future and receive the same result — no duplicate network calls, no thundering herd:
final dedup = ResultDeduplicator<String, User>();
// All three calls resolve from the single network request.
final results = await Future.wait([
dedup.run('u1', () => api.fetchUser('u1')),
dedup.run('u1', () => api.fetchUser('u1')),
dedup.run('u1', () => api.fetchUser('u1')),
]);
Once the in-flight future completes (success or error), the key is evicted so subsequent calls trigger a fresh fetch.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- inFlightCount → int
-
The number of keys with in-flight fetches.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
isInFlight(
K key) → bool -
Whether a fetch for
keyis currently in progress. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
K key, Future< Result< fetch()) → Future<V> >Result< V> > -
Returns the in-flight future for
keyif one exists, otherwise callsfetchto start a new one, registers it, and returns it. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited