Commit e1e5d938 authored by Adam Barth's avatar Adam Barth

Load data for StocksApp incrementally

Previously we would spend a lot of time during startup processing all 3k stocks
in the data set. This CL breaks the data up into 100 stock chunks and loads
them incrementally off the network. A future CL will switch to loading them on
demand.

R=ojan@chromium.org

Review URL: https://codereview.chromium.org/1038533002
parent 94d55d40
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -34,18 +34,16 @@ class StocksApp extends App { ...@@ -34,18 +34,16 @@ class StocksApp extends App {
${typography.white.title};''' ${typography.white.title};'''
); );
List<Stock> _sortedStocks = []; StockDataFetcher _stockDataFetcher;
List<Stock> _stocks = [];
bool _isSearching = false; bool _isSearching = false;
bool _isShowingMenu = false; bool _isShowingMenu = false;
String _searchQuery; String _searchQuery;
StocksApp() : super() { StocksApp() : super() {
fetchStockOracle().then((oracle) { _stockDataFetcher = new StockDataFetcher((StockData data) {
setState(() { setState(() {
_sortedStocks = oracle.stocks; data.appendTo(_stocks);
trace('StocksApp::sortStocks', () {
_sortedStocks.sort((a, b) => a.symbol.compareTo(b.symbol));
});
}); });
}); });
} }
...@@ -155,7 +153,7 @@ class StocksApp extends App { ...@@ -155,7 +153,7 @@ class StocksApp extends App {
return new Scaffold( return new Scaffold(
actionBar: actionBar, actionBar: actionBar,
content: new Stocklist(stocks: _sortedStocks, query: _searchQuery), content: new Stocklist(stocks: _stocks, query: _searchQuery),
fab: new FloatingActionButton( fab: new FloatingActionButton(
content: new Icon(type: 'content/add_white', size: 24), level: 3), content: new Icon(type: 'content/add_white', size: 24), level: 3),
drawer: drawer, drawer: drawer,
......
...@@ -12,6 +12,8 @@ import 'package:sky/framework/net/fetch.dart'; ...@@ -12,6 +12,8 @@ import 'package:sky/framework/net/fetch.dart';
// "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote", // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
// Data in stock_data.json // Data in stock_data.json
final Random _rng = new Random();
class Stock { class Stock {
String symbol; String symbol;
String name; String name;
...@@ -31,36 +33,41 @@ class Stock { ...@@ -31,36 +33,41 @@ class Stock {
symbol = fields[0]; symbol = fields[0];
name = fields[1]; name = fields[1];
marketCap = fields[4]; marketCap = fields[4];
var rng = new Random(); percentChange = (_rng.nextDouble() * 20) - 10;
percentChange = (rng.nextDouble() * 20) - 10;
} }
} }
class StockOracle { class StockData {
List<Stock> stocks; List<List<String>> _data;
StockOracle(this.stocks); StockData(this._data);
StockOracle.fromCompanyList(List<List<String>> list) { void appendTo(List<Stock> stocks) {
stocks = list.map((fields) => new Stock.fromFields(fields)).toList(); 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;
} }
} }
Future<StockOracle> fetchStockOracle() async { typedef void StockDataCallback(StockData data);
Response response = await fetch('lib/stock_data.json'); const _kChunkCount = 30;
class StockDataFetcher {
int _currentChunk = 0;
final StockDataCallback callback;
return trace('stocks::fetchStockOracle', () { StockDataFetcher(this.callback) {
_fetchNextChunk();
}
void _fetchNextChunk() {
fetch('data/stock_data_${_currentChunk++}.json').then((Response response) {
String json = response.bodyAsString(); String json = response.bodyAsString();
JsonDecoder decoder = new JsonDecoder(); JsonDecoder decoder = new JsonDecoder();
var companyList = decoder.convert(json);
return new StockOracle.fromCompanyList(companyList); callback(new StockData(decoder.convert(json)));
if (_currentChunk < _kChunkCount)
_fetchNextChunk();
}); });
}
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment