Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1139)

Side by Side Diff: sky/sdk/lib/framework/net/image_cache.dart

Issue 1179923004: Fix data loading in the stocks app (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/sdk/lib/framework/net/fetch.dart ('k') | sky/sdk/lib/framework/shell.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « sky/sdk/lib/framework/net/fetch.dart ('k') | sky/sdk/lib/framework/shell.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698