| 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 name; |
| 87 final int port; |
| 88 final int startTime; |
| 89 final int stackLimit; |
| 90 final Space newSpace; |
| 91 final Space oldSpace; |
| 92 |
| 93 // Create an isolate from a map describing an isolate in the observed VM. |
| 94 Isolate(Map raw): |
| 95 name = raw['name'], |
| 96 port = raw['port'], |
| 97 startTime = raw['starttime'], |
| 98 stackLimit = raw['stacklimit'], |
| 99 newSpace = new Space(raw['newspace']), |
| 100 oldSpace = new Space(raw['oldspace']) {} |
| 101 |
| 102 String toString() { |
| 103 return '$name: ${newSpace.used + oldSpace.used}K'; |
| 104 } |
| 105 } |
| 106 |
| 107 // Model of a memory space. |
| 108 class Space { |
| 109 final int used; |
| 110 final int capacity; |
| 111 |
| 112 Space(Map raw): used = raw['used'], capacity = raw['capacity'] {} |
| 113 |
| 114 String toString() { |
| 115 return 'used: $used capacity: $capacity'; |
| 116 } |
| 117 } |
| OLD | NEW |