| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.vmstats; | |
| 6 | |
| 7 // Base class of model that reports changes to registered listeners. | |
| 8 abstract class ObservableModel { | |
| 9 List<ModelListener> _listeners = []; | |
| 10 | |
| 11 void addListener(Function onUpdate, Function onFailure) { | |
| 12 _listeners.add(new ModelListener(onUpdate, onFailure)); | |
| 13 } | |
| 14 | |
| 15 void removeListener(Function onUpdate) { | |
| 16 Iterator<ModelListener> iterator = _listeners.iterator; | |
| 17 while (iterator.moveNext()) { | |
| 18 if (iterator.current._onUpdate == onUpdate) { | |
| 19 _listeners.remove(iterator.current); | |
| 20 return; | |
| 21 } | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 void notifySuccess() { | |
| 26 _listeners.forEach((listener) => listener.changed(this)); | |
| 27 } | |
| 28 | |
| 29 void notifyFailure() { | |
| 30 _listeners.forEach((listener) => listener.failed()); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 // Model of a set of listener functions to call when a model changes. | |
| 35 class ModelListener { | |
| 36 Function _onUpdate; | |
| 37 Function _onFailure; | |
| 38 | |
| 39 ModelListener(this._onUpdate, this._onFailure) {} | |
| 40 | |
| 41 void changed(IsolateListModel model) => Function.apply(_onUpdate, [model]); | |
| 42 void failed() => Function.apply(_onFailure, []); | |
| 43 } | |
| 44 | |
| 45 | |
| 46 // Model of the current running isolates. | |
| 47 class IsolateListModel extends ObservableModel { | |
| 48 List<Isolate> _isolates = []; | |
| 49 | |
| 50 void update() { | |
| 51 HttpRequest.getString('/isolates').then( | |
| 52 (Map response) => _onUpdate(JSON.parse(response)), | |
| 53 onError: (e) => notifyFailure()); | |
| 54 } | |
| 55 | |
| 56 void _onUpdate(Map isolateMap) { | |
| 57 _isolates = []; | |
| 58 List list = isolateMap['isolates']; | |
| 59 for (int i = 0; i < list.length; i++) { | |
| 60 _isolates.add(new Isolate(list[i])); | |
| 61 } | |
| 62 notifySuccess(); | |
| 63 } | |
| 64 | |
| 65 String toString() { | |
| 66 return _isolates.join(', '); | |
| 67 } | |
| 68 | |
| 69 // List delegate method subset. | |
| 70 Isolate elementAt(int index) => _isolates[index]; | |
| 71 | |
| 72 void forEach(void f(Isolate element)) => _isolates.forEach(f); | |
| 73 | |
| 74 bool isEmpty() => _isolates.isEmpty; | |
| 75 | |
| 76 Iterator<Isolate> get iterator => _isolates.iterator; | |
| 77 | |
| 78 int get length => _isolates.length; | |
| 79 | |
| 80 Isolate operator[](int index) => _isolates[index]; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 // Model of a single isolate. | |
| 85 class Isolate { | |
| 86 final String handle; | |
| 87 final String name; | |
| 88 final int port; | |
| 89 final int startTime; | |
| 90 final int stackLimit; | |
| 91 final Space newSpace; | |
| 92 final Space oldSpace; | |
| 93 | |
| 94 // Create an isolate from a map describing an isolate in the observed VM. | |
| 95 Isolate(Map raw): | |
| 96 handle = raw['handle'], | |
| 97 name = raw['name'], | |
| 98 port = raw['port'], | |
| 99 startTime = raw['starttime'], | |
| 100 stackLimit = raw['stacklimit'], | |
| 101 newSpace = new Space(raw['newspace']), | |
| 102 oldSpace = new Space(raw['oldspace']) {} | |
| 103 | |
| 104 String toString() { | |
| 105 return '$name: ${newSpace.used + oldSpace.used}K'; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Model of a memory space. | |
| 110 class Space { | |
| 111 final int used; | |
| 112 final int capacity; | |
| 113 | |
| 114 Space(Map raw): used = raw['used'], capacity = raw['capacity'] {} | |
| 115 | |
| 116 String toString() { | |
| 117 return 'used: $used capacity: $capacity'; | |
| 118 } | |
| 119 } | |
| OLD | NEW |