OLD | NEW |
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 Loading... |
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 } |
OLD | NEW |