Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 part of vmstats; | |
|
siva
2013/03/05 23:12:44
Ditto comment.
Tom Ball
2013/03/06 00:34:43
Done.
| |
| 2 | |
| 3 // Base class of model that reports changes to registered listeners. | |
| 4 abstract class ObservableModel { | |
| 5 List<ModelListener> _listeners = []; | |
| 6 | |
| 7 void addListener(Function onUpdate, Function onFailure) { | |
| 8 _listeners.add(new ModelListener(onUpdate, onFailure)); | |
| 9 } | |
| 10 | |
| 11 void removeListener(Function onUpdate) { | |
| 12 Iterator<ModelListener> iterator = _listeners.iterator; | |
| 13 while (iterator.moveNext()) { | |
| 14 if (iterator.current._onUpdate == onUpdate) { | |
| 15 _listeners.remove(iterator.current); | |
| 16 return; | |
| 17 } | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 void notifySuccess() { | |
| 22 _listeners.forEach((listener) => listener.changed(this)); | |
| 23 } | |
| 24 | |
| 25 void notifyFailure() { | |
| 26 _listeners.forEach((listener) => listener.failed()); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // Model of a set of listener functions to call when a model changes. | |
| 31 class ModelListener { | |
| 32 Function _onUpdate; | |
| 33 Function _onFailure; | |
| 34 | |
| 35 ModelListener(Function onUpdate, Function onFailure) { | |
| 36 _onUpdate = onUpdate; | |
| 37 _onFailure = onFailure; | |
| 38 } | |
|
siva
2013/03/05 23:12:44
ModelListener(this._onUpdate, this._onFailure);
Tom Ball
2013/03/06 00:34:43
Done.
| |
| 39 | |
| 40 void changed(IsolateListModel model) => Function.apply(_onUpdate, [model]); | |
| 41 void failed() => Function.apply(_onFailure, []); | |
| 42 } | |
| 43 | |
| 44 | |
| 45 // Model of the current running isolates. | |
| 46 class IsolateListModel extends ObservableModel { | |
| 47 List<Isolate> _isolates = []; | |
| 48 | |
| 49 void update() { | |
| 50 HttpRequest.getString('/isolates').then( | |
| 51 (Map response) => _onUpdate(JSON.parse(response)), | |
| 52 onError: (e) => notifyFailure()); | |
| 53 } | |
| 54 | |
| 55 void _onUpdate(Map isolateMap) { | |
| 56 _isolates = []; | |
| 57 List list = isolateMap['isolates']; | |
| 58 for (int i = 0; i < list.length; i++) { | |
| 59 _isolates.add(new Isolate(list[i])); | |
| 60 } | |
| 61 notifySuccess(); | |
| 62 } | |
| 63 | |
| 64 String toString() { | |
| 65 return _isolates.join(', '); | |
| 66 } | |
| 67 | |
| 68 // List delegate method subset. | |
| 69 Isolate elementAt(int index) => _isolates[index]; | |
| 70 | |
| 71 void forEach(void f(Isolate element)) => _isolates.forEach(f); | |
| 72 | |
| 73 bool isEmpty() => _isolates.isEmpty; | |
| 74 | |
| 75 Iterator<Isolate> get iterator => _isolates.iterator; | |
| 76 | |
| 77 int get length => _isolates.length; | |
| 78 | |
| 79 Isolate operator[](int index) => _isolates[index]; | |
| 80 } | |
| 81 | |
| 82 | |
| 83 // Model of a single isolate. | |
| 84 class Isolate { | |
| 85 String _name; | |
| 86 int _port; | |
| 87 int _startTime; | |
| 88 int _stackLimit; | |
| 89 Space _newSpace; | |
| 90 Space _oldSpace; | |
| 91 | |
| 92 // Create an isolate from a map describing an isolate in the observed VM. | |
| 93 Isolate(Map raw) { | |
| 94 _name = raw['name']; | |
| 95 _port = raw['port']; | |
| 96 _startTime = raw['starttime']; | |
| 97 _stackLimit = raw['stacklimit']; | |
| 98 _newSpace = new Space(raw['newspace']); | |
| 99 _oldSpace = new Space(raw['oldspace']); | |
| 100 } | |
| 101 | |
| 102 String get name => _name; | |
| 103 int get port => _port; | |
| 104 int get startTime => _startTime; | |
| 105 int get stackLimit => _stackLimit; | |
| 106 Space get newSpace => _newSpace; | |
| 107 Space get oldSpace => _oldSpace; | |
|
siva
2013/03/05 23:12:44
why have the fields as _ and then a set of getters
Tom Ball
2013/03/06 00:34:43
Okay, but that makes instances mutable.
srdjan
2013/03/06 00:43:21
You can make them final, that makes them immutable
Tom Ball
2013/03/06 01:04:49
Done.
| |
| 108 | |
| 109 String toString() { | |
| 110 return '$_name: ${_newSpace.used + _oldSpace.used}K'; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Model of a memory space. | |
| 115 class Space { | |
| 116 int _used; | |
| 117 int _capacity; | |
| 118 | |
| 119 Space(Map raw) { | |
| 120 _used = raw['used']; | |
| 121 _capacity = raw['capacity']; | |
| 122 } | |
| 123 | |
| 124 int get used => _used; | |
| 125 int get capacity => _capacity; | |
| 126 | |
| 127 String toString() { | |
| 128 return 'used: $_used capacity: $_capacity'; | |
| 129 } | |
| 130 } | |
| OLD | NEW |