| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 app; | |
| 6 | |
| 7 abstract class VM extends Observable { | |
| 8 @observable ObservatoryApplication get app => _app; | |
| 9 ObservatoryApplication _app; | |
| 10 | |
| 11 Future<String> fetchString(String path); | |
| 12 | |
| 13 Future<ObservableMap> fetchMap(String path) { | |
| 14 return fetchString(path).then((response) { | |
| 15 try { | |
| 16 var map = JSON.decode(response); | |
| 17 return toObservable(map); | |
| 18 } catch (e, st) { | |
| 19 return toObservable({ | |
| 20 'type': 'Error', | |
| 21 'errorType': 'DecodeError', | |
| 22 'text': '$e $st' | |
| 23 }); | |
| 24 } | |
| 25 }).catchError((error) { | |
| 26 return toObservable({ | |
| 27 'type': 'Error', | |
| 28 'errorType': 'FetchError', | |
| 29 'text': '$error' | |
| 30 }); | |
| 31 }); | |
| 32 } | |
| 33 | |
| 34 @observable final ObservableMap isolates = new ObservableMap(); | |
| 35 Isolate getIsolate(String id) { | |
| 36 var isolate = isolates[id]; | |
| 37 if (isolate != null) { | |
| 38 return isolate; | |
| 39 } | |
| 40 isolate = new Isolate.fromId(this, id); | |
| 41 isolates[id] = isolate; | |
| 42 return isolate; | |
| 43 } | |
| 44 | |
| 45 static bool _foundIsolateInMembers(String id, List<Map> members) { | |
| 46 return members.any((E) => E['id'] == id); | |
| 47 } | |
| 48 | |
| 49 void _updateIsolates(List<Map> members) { | |
| 50 // Find dead isolates. | |
| 51 var deadIsolates = []; | |
| 52 isolates.forEach((k, v) { | |
| 53 if (!_foundIsolateInMembers(k, members)) { | |
| 54 deadIsolates.add(k); | |
| 55 } | |
| 56 }); | |
| 57 // Remove them. | |
| 58 deadIsolates.forEach((id) { | |
| 59 isolates.remove(id); | |
| 60 }); | |
| 61 // Add new isolates. | |
| 62 members.forEach((map) { | |
| 63 var id = map['id']; | |
| 64 var isolate = isolates[id]; | |
| 65 if (isolate == null) { | |
| 66 isolate = new Isolate.fromMap(this, map); | |
| 67 isolates[id] = isolate; | |
| 68 } | |
| 69 isolate.refresh(); | |
| 70 }); | |
| 71 } | |
| 72 | |
| 73 void refreshIsolates() { | |
| 74 fetchMap('isolates').then((map) { | |
| 75 assert(map['type'] == 'IsolateList'); | |
| 76 _updateIsolates(map['members']); | |
| 77 app.setResponse(map); | |
| 78 }); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 | |
| 83 class HttpVM extends VM { | |
| 84 final String address; | |
| 85 HttpVM(this.address); | |
| 86 | |
| 87 Future<String> fetchString(String path) { | |
| 88 Logger.root.info('Fetching $path from $address'); | |
| 89 return HttpRequest.getString(address + path); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 class DartiumVM extends VM { | |
| 94 final Map _outstandingRequests = new Map(); | |
| 95 int _requestSerial = 0; | |
| 96 | |
| 97 DartiumVM() { | |
| 98 window.onMessage.listen(_messageHandler); | |
| 99 print('Connected to DartiumVM'); | |
| 100 } | |
| 101 | |
| 102 void _messageHandler(msg) { | |
| 103 var id = msg.data['id']; | |
| 104 var name = msg.data['name']; | |
| 105 var data = msg.data['data']; | |
| 106 if (name != 'observatoryData') { | |
| 107 return; | |
| 108 } | |
| 109 var completer = _outstandingRequests[id]; | |
| 110 assert(completer != null); | |
| 111 _outstandingRequests.remove(id); | |
| 112 completer.complete(data); | |
| 113 } | |
| 114 | |
| 115 Future<String> fetchString(String path) { | |
| 116 var idString = '$_requestSerial'; | |
| 117 Map message = {}; | |
| 118 message['id'] = idString; | |
| 119 message['method'] = 'observatoryQuery'; | |
| 120 message['query'] = '/$path'; | |
| 121 _requestSerial++; | |
| 122 var completer = new Completer(); | |
| 123 _outstandingRequests[idString] = completer; | |
| 124 window.parent.postMessage(JSON.encode(message), '*'); | |
| 125 return completer.future; | |
| 126 } | |
| 127 } | |
| OLD | NEW |