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

Side by Side 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 unified diff | 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 »
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 import 'package:sky/framework/debug/tracing.dart'; 7 import 'package:sky/framework/debug/tracing.dart';
8 import 'package:sky/framework/net/fetch.dart'; 8 import 'package:sky/framework/net/fetch.dart';
9 9
10 // Snapshot from http://www.nasdaq.com/screening/company-list.aspx 10 // Snapshot from http://www.nasdaq.com/screening/company-list.aspx
11 // Fetched 2/23/2014. 11 // Fetched 2/23/2014.
12 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote", 12 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
13 // Data in stock_data.json 13 // Data in stock_data.json
14 14
15 final Random _rng = new Random();
16
15 class Stock { 17 class Stock {
16 String symbol; 18 String symbol;
17 String name; 19 String name;
18 double lastSale; 20 double lastSale;
19 String marketCap; 21 String marketCap;
20 double percentChange; 22 double percentChange;
21 23
22 Stock(this.symbol, this.name, this.lastSale, this.marketCap, this.percentChang e); 24 Stock(this.symbol, this.name, this.lastSale, this.marketCap, this.percentChang e);
23 25
24 Stock.fromFields(List<String> fields) { 26 Stock.fromFields(List<String> fields) {
25 // FIXME: This class should only have static data, not lastSale, etc. 27 // FIXME: This class should only have static data, not lastSale, etc.
26 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Sum mary Quote", 28 // "Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Sum mary Quote",
27 lastSale = 0.0; 29 lastSale = 0.0;
28 try{ 30 try{
29 lastSale = double.parse(fields[2]); 31 lastSale = double.parse(fields[2]);
30 } catch(_) {} 32 } catch(_) {}
31 symbol = fields[0]; 33 symbol = fields[0];
32 name = fields[1]; 34 name = fields[1];
33 marketCap = fields[4]; 35 marketCap = fields[4];
34 var rng = new Random(); 36 percentChange = (_rng.nextDouble() * 20) - 10;
35 percentChange = (rng.nextDouble() * 20) - 10;
36 } 37 }
37 } 38 }
38 39
39 class StockOracle { 40 class StockData {
40 List<Stock> stocks; 41 List<List<String>> _data;
41 42
42 StockOracle(this.stocks); 43 StockData(this._data);
43 44
44 StockOracle.fromCompanyList(List<List<String>> list) { 45 void appendTo(List<Stock> stocks) {
45 stocks = list.map((fields) => new Stock.fromFields(fields)).toList(); 46 for (List<String> fields in _data)
46 } 47 stocks.add(new Stock.fromFields(fields));
47
48 Stock lookupBySymbol(String symbol) {
49 this.stocks.forEach((stock) {
50 if (stock.symbol == symbol)
51 return stock;
52 });
53 return null;
54 } 48 }
55 } 49 }
56 50
57 Future<StockOracle> fetchStockOracle() async { 51 typedef void StockDataCallback(StockData data);
58 Response response = await fetch('lib/stock_data.json'); 52 const _kChunkCount = 30;
59 53
60 return trace('stocks::fetchStockOracle', () { 54 class StockDataFetcher {
61 String json = response.bodyAsString(); 55 int _currentChunk = 0;
62 JsonDecoder decoder = new JsonDecoder(); 56 final StockDataCallback callback;
63 var companyList = decoder.convert(json); 57
64 return new StockOracle.fromCompanyList(companyList); 58 StockDataFetcher(this.callback) {
65 }); 59 _fetchNextChunk();
60 }
61
62 void _fetchNextChunk() {
63 fetch('data/stock_data_${_currentChunk++}.json').then((Response response) {
64 String json = response.bodyAsString();
65 JsonDecoder decoder = new JsonDecoder();
66
67 callback(new StockData(decoder.convert(json)));
68
69 if (_currentChunk < _kChunkCount)
70 _fetchNextChunk();
71 });
72 }
66 } 73 }
OLDNEW
« 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