OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:sky'; | |
6 import 'dart:collection'; | |
7 import 'fetch.dart'; | |
8 import 'package:mojom/mojo/url_response.mojom.dart'; | |
9 | |
10 final HashMap<String, List<ImageDecoderCallback>> _pendingRequests = | |
11 new HashMap<String, List<ImageDecoderCallback>>(); | |
12 | |
13 final HashMap<String, Image> _completedRequests = | |
14 new HashMap<String, Image>(); | |
15 | |
16 void _loadComplete(url, image) { | |
17 _completedRequests[url] = image; | |
18 _pendingRequests[url].forEach((c) => c(image)); | |
19 _pendingRequests.remove(url); | |
20 } | |
21 | |
22 void load(String url, ImageDecoderCallback callback) { | |
23 Image result = _completedRequests[url]; | |
24 if (result != null) { | |
25 callback(_completedRequests[url]); | |
26 return; | |
27 } | |
28 | |
29 bool newRequest = false; | |
30 _pendingRequests.putIfAbsent(url, () { | |
31 newRequest = true; | |
32 return new List<ImageDecoderCallback>(); | |
33 }).add(callback); | |
34 if (newRequest) { | |
35 fetchUrl(url).then((UrlResponse response) { | |
36 if (response.statusCode >= 400) { | |
37 _loadComplete(url, null); | |
38 return; | |
39 } | |
40 new ImageDecoder(response.body.handle.h, | |
41 (image) => _loadComplete(url, image)); | |
42 }); | |
43 } | |
44 } | |
OLD | NEW |