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

Unified Diff: sky/examples/stocks-fn/lib/stock_data.dart

Issue 1038533002: Load data for StocksApp incrementally (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: remove spurious diff Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/examples/stocks-fn/lib/stock_app.dart ('k') | sky/examples/stocks-fn/lib/stock_data.json » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/examples/stocks-fn/lib/stock_data.dart
diff --git a/sky/examples/stocks-fn/lib/stock_data.dart b/sky/examples/stocks-fn/lib/stock_data.dart
index 5381ab5af8118cab4fdb09e14ed48df469d95e8d..9be6e37dff6162d3bd5ca314b1d2ea8e8c9a95b2 100644
--- a/sky/examples/stocks-fn/lib/stock_data.dart
+++ b/sky/examples/stocks-fn/lib/stock_data.dart
@@ -12,6 +12,8 @@ import 'package:sky/framework/net/fetch.dart';
// "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
// Data in stock_data.json
+final Random _rng = new Random();
+
class Stock {
String symbol;
String name;
@@ -31,36 +33,41 @@ class Stock {
symbol = fields[0];
name = fields[1];
marketCap = fields[4];
- var rng = new Random();
- percentChange = (rng.nextDouble() * 20) - 10;
+ percentChange = (_rng.nextDouble() * 20) - 10;
}
}
-class StockOracle {
- List<Stock> stocks;
+class StockData {
+ List<List<String>> _data;
- StockOracle(this.stocks);
+ StockData(this._data);
- StockOracle.fromCompanyList(List<List<String>> list) {
- stocks = list.map((fields) => new Stock.fromFields(fields)).toList();
+ void appendTo(List<Stock> stocks) {
+ for (List<String> fields in _data)
+ stocks.add(new Stock.fromFields(fields));
}
+}
- Stock lookupBySymbol(String symbol) {
- this.stocks.forEach((stock) {
- if (stock.symbol == symbol)
- return stock;
- });
- return null;
+typedef void StockDataCallback(StockData data);
+const _kChunkCount = 30;
+
+class StockDataFetcher {
+ int _currentChunk = 0;
+ final StockDataCallback callback;
+
+ StockDataFetcher(this.callback) {
+ _fetchNextChunk();
}
-}
-Future<StockOracle> fetchStockOracle() async {
- Response response = await fetch('lib/stock_data.json');
+ void _fetchNextChunk() {
+ fetch('data/stock_data_${_currentChunk++}.json').then((Response response) {
+ String json = response.bodyAsString();
+ JsonDecoder decoder = new JsonDecoder();
+
+ callback(new StockData(decoder.convert(json)));
- return trace('stocks::fetchStockOracle', () {
- String json = response.bodyAsString();
- JsonDecoder decoder = new JsonDecoder();
- var companyList = decoder.convert(json);
- return new StockOracle.fromCompanyList(companyList);
- });
+ if (_currentChunk < _kChunkCount)
+ _fetchNextChunk();
+ });
+ }
}
« no previous file with comments | « sky/examples/stocks-fn/lib/stock_app.dart ('k') | sky/examples/stocks-fn/lib/stock_data.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698