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 {
${typography.white.title};'''
);
List<Stock> _sortedStocks = [];
StockDataFetcher _stockDataFetcher;
List<Stock> _stocks = [];
bool _isSearching = false;
bool _isShowingMenu = false;
String _searchQuery;
StocksApp() : super() {
fetchStockOracle().then((oracle) {
_stockDataFetcher = new StockDataFetcher((StockData data) {
setState(() {
_sortedStocks = oracle.stocks;
trace('StocksApp::sortStocks', () {
_sortedStocks.sort((a, b) => a.symbol.compareTo(b.symbol));
});
data.appendTo(_stocks);
});
});
}
......@@ -155,7 +153,7 @@ class StocksApp extends App {
return new Scaffold(
actionBar: actionBar,
content: new Stocklist(stocks: _sortedStocks, query: _searchQuery),
content: new Stocklist(stocks: _stocks, query: _searchQuery),
fab: new FloatingActionButton(
content: new Icon(type: 'content/add_white', size: 24), level: 3),
drawer: drawer,
......
......@@ -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();
});
}
}
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