Chromium Code Reviews| Index: sky/sdk/lib/framework/net/image_cache.dart |
| diff --git a/sky/sdk/lib/framework/net/image_cache.dart b/sky/sdk/lib/framework/net/image_cache.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f03b7a0a4885d1e81f400ddebd16b35854e50aa8 |
| --- /dev/null |
| +++ b/sky/sdk/lib/framework/net/image_cache.dart |
| @@ -0,0 +1,30 @@ |
| +// 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' as sky; |
| +import 'dart:collection'; |
| + |
| +class ImageCache { |
|
abarth-chromium
2015/06/02 00:52:47
There's no need for this class. You can just put
jackson
2015/06/02 01:18:25
Acknowledged.
|
| + static final ImageCache _sharedCache = new ImageCache(); |
| + static ImageCache shared() { return _sharedCache; } |
| + |
| + final HashMap pendingRequests = new HashMap<String, List<sky.ImageLoaderCallback>>(); |
| + final HashMap completedRequests = new HashMap<String, sky.Image>(); |
|
abarth-chromium
2015/06/02 00:52:47
It's more important to put the types on the declar
jackson
2015/06/02 01:18:25
Acknowledged.
|
| + |
| + void load(String url, sky.ImageLoaderCallback callback) { |
| + if (completedRequests.containsKey(url)) { |
| + callback(completedRequests[url]); |
| + } else if (pendingRequests.containsKey(url)) { |
|
abarth-chromium
2015/06/02 00:52:47
Using putIfAbsent [1] is the idiomatic (and faster
jackson
2015/06/02 01:18:25
Acknowledged.
|
| + pendingRequests[url].add(callback); |
| + } else { |
| + pendingRequests[url] = new List(); |
| + pendingRequests[url].add(callback); |
| + new sky.ImageLoader(url, (image) { |
| + completedRequests[url] = image; |
| + pendingRequests[url].forEach((c) => c(image)); |
| + pendingRequests[url].remove(url); |
| + }).load(); |
| + } |
| + } |
| +} |