Adam Barth 9f228349e9 Use ImageResource instead of Future<sky.Image>
Using ImageResource solves two problems:

1) Listeners can be notified synchronously when the sky.Image is already
   available. This change removes flash of 0x0 layout when moving an
   already-cached image around in the render tree.

2) In the future, when we support animated images, we can notify listeners
   multiple times whenever a new image is available.
2015-08-10 20:43:32 -07:00

29 lines
953 B
Dart

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:collection';
import 'dart:sky' as sky;
import 'package:mojo/mojo/url_response.mojom.dart';
import 'package:sky/base/image_resource.dart';
import 'package:sky/mojo/net/fetch.dart';
final HashMap<String, ImageResource> _cache = new Map<String, ImageResource>();
ImageResource load(String url) {
return _cache.putIfAbsent(url, () {
Completer<sky.Image> completer = new Completer<sky.Image>();
fetchUrl(url).then((UrlResponse response) {
if (response.statusCode >= 400) {
print("Failed (${response.statusCode}) to load image ${url}");
completer.complete(null);
} else {
new sky.ImageDecoder(response.body.handle.h, completer.complete);
}
});
return new ImageResource(completer.future);
});
}