AsyncCanonicalizingFetcher<K, V> class Null safety

Helper to fetch a value asynchronously, where multiple outstanding requests for the same resource are canonicalized to a single request. This can prevent loading the same resource more than once.

Consider, for example, a cache of a resource loaded over the network, using the following algorithm:

Future<Thing> get(Key key) {
  if (cache has an entry for key) {
    return cache[key];
  }
  Fetch Thing t over network;
  Store t at cache[key];
  return t;
}

If a resource associated with some key k is being fetched over the network, and a second request for an equivalent key k2 comes in while the network operation for k in in ongoing, the above algorithm would fetch the resource twice. This can be avoided by using get from a subclass of AsyncCanonicalilzingFetcher<Thing, Key> that implements create to load a Thing over the network.

This is a trivial amount of code, but it's a little tricky to get it right. A sample usage can be found in the jovial_svg caching sample.

Constructors

AsyncCanonicalizingFetcher()

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

create(K key) Future<V>
Create a value given key. This method is to be overridden by subclasses.
get(K key) Future<V>
Get the value identified by key. If this is called with a key that is equivalent to one that is currently being loaded, a identical Future identical to the one returned for the prior call will be returned.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
A string representation of this object. [...]
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited