load method

Future<XAssetThumbnail> load(
  1. String assetId, {
  2. required int sizePx,
})

Fetch (cache hit, in-flight share, or new request). Repeated calls for the same key while the future is in flight share the same future so we don't hit the platform channel twice.

Implementation

Future<XAssetThumbnail> load(String assetId, {required int sizePx}) {
  final k = _key(assetId, sizePx);
  final cached = peek(assetId, sizePx);
  if (cached != null) return Future<XAssetThumbnail>.value(cached);
  final pending = _inflight[k];
  if (pending != null) return pending;
  final future = XGallery.thumbnail(assetId, sizePx: sizePx).then(
    (thumb) {
      _entries[k] = thumb;
      if (_entries.length > capacity) _entries.remove(_entries.keys.first);
      _inflight.remove(k);
      return thumb;
    },
    onError: (Object err, StackTrace st) {
      _inflight.remove(k);
      throw err;
    },
  );
  _inflight[k] = future;
  return future;
}