putIfAbsent method

ImageStreamCompleter putIfAbsent (Object key, ImageStreamCompleter loader())

Returns the previously cached ImageStream for the given key, if available; if not, calls the given callback to obtain it first. In either case, the key is moved to the "most recently used" position.

The arguments must not be null. The loader cannot return null.

Implementation

ImageStreamCompleter putIfAbsent(Object key, ImageStreamCompleter loader()) {
  assert(key != null);
  assert(loader != null);
  ImageStreamCompleter result = _pendingImages[key];
  // Nothing needs to be done because the image hasn't loaded yet.
  if (result != null)
    return result;
  // Remove the provider from the list so that we can move it to the
  // recently used position below.
  final _CachedImage image = _cache.remove(key);
  if (image != null) {
    _cache[key] = image;
    return image.completer;
  }
  result = loader();
  void listener(ImageInfo info, bool syncCall) {
    // Images that fail to load don't contribute to cache size.
    final int imageSize = info?.image == null ? 0 : info.image.height * info.image.width * 4;
    final _CachedImage image = _CachedImage(result, imageSize);
    // If the image is bigger than the maximum cache size, and the cache size
    // is not zero, then increase the cache size to the size of the image plus
    // some change.
    if (maximumSizeBytes > 0 && imageSize > maximumSizeBytes) {
      _maximumSizeBytes = imageSize + 1000;
    }
    _currentSizeBytes += imageSize;
    _pendingImages.remove(key);
    _cache[key] = image;
    result.removeListener(listener);
    _checkCacheSize();
  }
  if (maximumSize > 0 && maximumSizeBytes > 0) {
    _pendingImages[key] = result;
    result.addListener(listener);
  }
  return result;
}