OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'dart:sky' as sky; | 5 import 'dart:sky'; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 import 'fetch.dart'; |
| 8 import 'package:mojom/mojo/url_response.mojom.dart'; |
7 | 9 |
8 final HashMap<String, List<sky.ImageLoaderCallback>> _pendingRequests = | 10 final HashMap<String, List<ImageDecoderCallback>> _pendingRequests = |
9 new HashMap<String, List<sky.ImageLoaderCallback>>(); | 11 new HashMap<String, List<ImageDecoderCallback>>(); |
10 | 12 |
11 final HashMap<String, sky.Image> _completedRequests = | 13 final HashMap<String, Image> _completedRequests = |
12 new HashMap<String, sky.Image>(); | 14 new HashMap<String, Image>(); |
13 | 15 |
14 void load(String url, sky.ImageLoaderCallback callback) { | 16 void load(String url, ImageDecoderCallback callback) { |
15 sky.Image result = _completedRequests[url]; | 17 Image result = _completedRequests[url]; |
16 if (result != null) { | 18 if (result != null) { |
17 callback(_completedRequests[url]); | 19 callback(_completedRequests[url]); |
18 return; | 20 return; |
19 } | 21 } |
20 | 22 |
21 bool newRequest = false; | 23 bool newRequest = false; |
22 _pendingRequests.putIfAbsent(url, () { | 24 _pendingRequests.putIfAbsent(url, () { |
23 newRequest = true; | 25 newRequest = true; |
24 return new List<sky.ImageLoaderCallback>(); | 26 return new List<ImageDecoderCallback>(); |
25 }).add(callback); | 27 }).add(callback); |
26 if (newRequest) { | 28 if (newRequest) { |
27 new sky.ImageLoader(url, (image) { | 29 fetchUrl(url).then((UrlResponse response) { |
28 _completedRequests[url] = image; | 30 new ImageDecoder(response.body.handle.h, (image) { |
29 _pendingRequests[url].forEach((c) => c(image)); | 31 _completedRequests[url] = image; |
30 _pendingRequests.remove(url); | 32 _pendingRequests[url].forEach((c) => c(image)); |
| 33 _pendingRequests.remove(url); |
| 34 }); |
31 }); | 35 }); |
32 } | 36 } |
33 } | 37 } |
OLD | NEW |