Chromium Code Reviews| Index: runtime/bin/vmstats/models.dart |
| =================================================================== |
| --- runtime/bin/vmstats/models.dart (revision 0) |
| +++ runtime/bin/vmstats/models.dart (revision 0) |
| @@ -0,0 +1,130 @@ |
| +part of vmstats; |
|
siva
2013/03/05 23:12:44
Ditto comment.
Tom Ball
2013/03/06 00:34:43
Done.
|
| + |
| +// Base class of model that reports changes to registered listeners. |
| +abstract class ObservableModel { |
| + List<ModelListener> _listeners = []; |
| + |
| + void addListener(Function onUpdate, Function onFailure) { |
| + _listeners.add(new ModelListener(onUpdate, onFailure)); |
| + } |
| + |
| + void removeListener(Function onUpdate) { |
| + Iterator<ModelListener> iterator = _listeners.iterator; |
| + while (iterator.moveNext()) { |
| + if (iterator.current._onUpdate == onUpdate) { |
| + _listeners.remove(iterator.current); |
| + return; |
| + } |
| + } |
| + } |
| + |
| + void notifySuccess() { |
| + _listeners.forEach((listener) => listener.changed(this)); |
| + } |
| + |
| + void notifyFailure() { |
| + _listeners.forEach((listener) => listener.failed()); |
| + } |
| +} |
| + |
| +// Model of a set of listener functions to call when a model changes. |
| +class ModelListener { |
| + Function _onUpdate; |
| + Function _onFailure; |
| + |
| + ModelListener(Function onUpdate, Function onFailure) { |
| + _onUpdate = onUpdate; |
| + _onFailure = onFailure; |
| + } |
|
siva
2013/03/05 23:12:44
ModelListener(this._onUpdate, this._onFailure);
Tom Ball
2013/03/06 00:34:43
Done.
|
| + |
| + void changed(IsolateListModel model) => Function.apply(_onUpdate, [model]); |
| + void failed() => Function.apply(_onFailure, []); |
| +} |
| + |
| + |
| +// Model of the current running isolates. |
| +class IsolateListModel extends ObservableModel { |
| + List<Isolate> _isolates = []; |
| + |
| + void update() { |
| + HttpRequest.getString('/isolates').then( |
| + (Map response) => _onUpdate(JSON.parse(response)), |
| + onError: (e) => notifyFailure()); |
| + } |
| + |
| + void _onUpdate(Map isolateMap) { |
| + _isolates = []; |
| + List list = isolateMap['isolates']; |
| + for (int i = 0; i < list.length; i++) { |
| + _isolates.add(new Isolate(list[i])); |
| + } |
| + notifySuccess(); |
| + } |
| + |
| + String toString() { |
| + return _isolates.join(', '); |
| + } |
| + |
| + // List delegate method subset. |
| + Isolate elementAt(int index) => _isolates[index]; |
| + |
| + void forEach(void f(Isolate element)) => _isolates.forEach(f); |
| + |
| + bool isEmpty() => _isolates.isEmpty; |
| + |
| + Iterator<Isolate> get iterator => _isolates.iterator; |
| + |
| + int get length => _isolates.length; |
| + |
| + Isolate operator[](int index) => _isolates[index]; |
| +} |
| + |
| + |
| +// Model of a single isolate. |
| +class Isolate { |
| + String _name; |
| + int _port; |
| + int _startTime; |
| + int _stackLimit; |
| + Space _newSpace; |
| + Space _oldSpace; |
| + |
| + // Create an isolate from a map describing an isolate in the observed VM. |
| + Isolate(Map raw) { |
| + _name = raw['name']; |
| + _port = raw['port']; |
| + _startTime = raw['starttime']; |
| + _stackLimit = raw['stacklimit']; |
| + _newSpace = new Space(raw['newspace']); |
| + _oldSpace = new Space(raw['oldspace']); |
| + } |
| + |
| + String get name => _name; |
| + int get port => _port; |
| + int get startTime => _startTime; |
| + int get stackLimit => _stackLimit; |
| + Space get newSpace => _newSpace; |
| + 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.
|
| + |
| + String toString() { |
| + return '$_name: ${_newSpace.used + _oldSpace.used}K'; |
| + } |
| +} |
| + |
| +// Model of a memory space. |
| +class Space { |
| + int _used; |
| + int _capacity; |
| + |
| + Space(Map raw) { |
| + _used = raw['used']; |
| + _capacity = raw['capacity']; |
| + } |
| + |
| + int get used => _used; |
| + int get capacity => _capacity; |
| + |
| + String toString() { |
| + return 'used: $_used capacity: $_capacity'; |
| + } |
| +} |