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

Side by Side Diff: sky/sdk/example/stocks/lib/stock_data.dart

Issue 1229743002: Teach sky_shell.exe to run from a snapshot (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 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 | « no previous file | sky/sdk/lib/mojo/net/fetch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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:convert'; 5 import 'dart:convert';
6 import 'dart:math'; 6 import 'dart:math';
7 7
8 import 'package:sky/mojo/net/fetch.dart'; 8 import 'package:sky/mojo/net/fetch.dart';
9 import 'package:sky/mojo/asset_bundle.dart'; 9 import 'package:sky/mojo/asset_bundle.dart';
10 10
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 typedef void StockDataCallback(StockData data); 52 typedef void StockDataCallback(StockData data);
53 const _kChunkCount = 30; 53 const _kChunkCount = 30;
54 54
55 String _urlToFetch(int chunk) { 55 String _urlToFetch(int chunk) {
56 if (rootBundle == null) 56 if (rootBundle == null)
57 return '../data/stock_data_${chunk}.json'; 57 return '../data/stock_data_${chunk}.json';
58 return 'https://domokit.github.io/example/stocks/data/stock_data_${chunk}.json '; 58 return 'https://domokit.github.io/example/stocks/data/stock_data_${chunk}.json ';
59 } 59 }
60 60
61 class StockDataFetcher { 61 class StockDataFetcher {
62 int _currentChunk = 0; 62 int _nextChunk = 0;
63 final StockDataCallback callback; 63 final StockDataCallback callback;
64 64
65 StockDataFetcher(this.callback) { 65 StockDataFetcher(this.callback) {
66 _fetchNextChunk(); 66 _fetchNextChunk();
67 } 67 }
68 68
69 void _fetchNextChunk() { 69 void _fetchNextChunk() {
70 fetchBody(_urlToFetch(_currentChunk++)).then((Response response) { 70 fetchBody(_urlToFetch(_nextChunk++)).then((Response response) {
71 String json = response.bodyAsString(); 71 String json = response.bodyAsString();
72 if (json == null) {
73 print("Failed to load stock data chunk ${_nextChunk - 1}");
74 return;
75 }
72 JsonDecoder decoder = new JsonDecoder(); 76 JsonDecoder decoder = new JsonDecoder();
73 77
74 callback(new StockData(decoder.convert(json))); 78 callback(new StockData(decoder.convert(json)));
75 79
76 if (_currentChunk < _kChunkCount) 80 if (_nextChunk < _kChunkCount)
77 _fetchNextChunk(); 81 _fetchNextChunk();
78 }); 82 });
79 } 83 }
80 } 84 }
OLDNEW
« no previous file with comments | « no previous file | sky/sdk/lib/mojo/net/fetch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698