| 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;
|
| +
|
| +// 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(int errorCode) {
|
| + _listeners.forEach((listener) => listener.failed(errorCode));
|
| + }
|
| +}
|
| +
|
| +// 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;
|
| + }
|
| +
|
| + void changed(IsolateListModel model) => Function.apply(_onUpdate, [model]);
|
| + void failed(int errorCode) => Function.apply(_onFailure, [errorCode]);
|
| +}
|
| +
|
| +
|
| +// Model of the current running isolates.
|
| +class IsolateListModel extends ObservableModel {
|
| + List<Isolate> _isolates = [];
|
| +
|
| + void update() {
|
| + sendRequest('/isolates',
|
| + (Map response) => _onUpdate(response),
|
| + (int errorCode) => notifyFailure(errorCode));
|
| + }
|
| +
|
| + 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;
|
| +
|
| + 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';
|
| + }
|
| +}
|
|
|