flutter_flutter/sdk/lib/framework/net/image_cache.dart
Eric Seidel 8f975399e1 Move image loading out of C++ into Dart
We already know how to talk to the network_service from Dart
via fetch.dart.  Might as well use that for Image loading
as well insetad of having ImageLoader do it.

As part of this I've renamed *ImageLoader to *ImageDecoder
and moved all the image loading logic into Dart.  This required
me to teach the idl system about mojo handles so that I could
pass the resulting MojoHandle from fetch.dart up through to
ImageDecoder.

R=abarth@chromium.org, jackson@google.com, hansmuller@google.com

Review URL: https://codereview.chromium.org/1173703002.
2015-06-09 12:14:13 -07:00

38 lines
1.1 KiB
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:sky';
import 'dart:collection';
import 'fetch.dart';
import 'package:mojom/mojo/url_response.mojom.dart';
final HashMap<String, List<ImageDecoderCallback>> _pendingRequests =
new HashMap<String, List<ImageDecoderCallback>>();
final HashMap<String, Image> _completedRequests =
new HashMap<String, Image>();
void load(String url, ImageDecoderCallback callback) {
Image result = _completedRequests[url];
if (result != null) {
callback(_completedRequests[url]);
return;
}
bool newRequest = false;
_pendingRequests.putIfAbsent(url, () {
newRequest = true;
return new List<ImageDecoderCallback>();
}).add(callback);
if (newRequest) {
fetchUrl(url).then((UrlResponse response) {
new ImageDecoder(response.body.handle.h, (image) {
_completedRequests[url] = image;
_pendingRequests[url].forEach((c) => c(image));
_pendingRequests.remove(url);
});
});
}
}