OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of observatory; | 5 part of observatory; |
6 | 6 |
7 /// A request response interceptor is called for each response. | 7 /// A request response interceptor is called for each response. |
8 typedef void RequestResponseInterceptor(); | 8 typedef void RequestResponseInterceptor(); |
9 | 9 |
10 abstract class RequestManager extends Observable { | 10 abstract class RequestManager extends Observable { |
(...skipping 29 matching lines...) Expand all Loading... |
40 error = 'No service found. Did you run with --enable-vm-service ?'; | 40 error = 'No service found. Did you run with --enable-vm-service ?'; |
41 } | 41 } |
42 setResponses([{ | 42 setResponses([{ |
43 'type': 'RequestError', | 43 'type': 'RequestError', |
44 'error': error | 44 'error': error |
45 }]); | 45 }]); |
46 } | 46 } |
47 | 47 |
48 /// Request [requestString] from the VM service. Updates [responses]. | 48 /// Request [requestString] from the VM service. Updates [responses]. |
49 /// Will trigger [interceptor] if one is set. | 49 /// Will trigger [interceptor] if one is set. |
50 Future<Map> get(String requestString) { | 50 void get(String requestString) { |
51 request(requestString).then((responseString) { | 51 if (_application.locationManager.isScriptLink) { |
52 parseResponses(responseString); | 52 // We cache script sources. |
| 53 String scriptName = _application.locationManager.scriptName; |
| 54 getScriptSource(scriptName, requestString).then((source) { |
| 55 if (source != null) { |
| 56 setResponses([{ |
| 57 'type': 'Script', |
| 58 'source': source |
| 59 }]); |
| 60 } else { |
| 61 setResponses([{ |
| 62 'type': 'RequestError', |
| 63 'error': 'Source for $scriptName could not be loaded.' |
| 64 }]); |
| 65 } |
| 66 }); |
| 67 } else { |
| 68 request(requestString).then((responseString) { |
| 69 parseResponses(responseString); |
| 70 }).catchError((e) { |
| 71 setResponseError(e.target); |
| 72 }); |
| 73 } |
| 74 } |
| 75 |
| 76 Future<ScriptSource> getScriptSource(String name, String requestString) { |
| 77 int isolateId = _application.locationManager.currentIsolateId(); |
| 78 Isolate isolate = _application.isolateManager.getIsolate(isolateId); |
| 79 ScriptSource source = isolate.scripts[name]; |
| 80 if (source != null) { |
| 81 return new Future.value(source); |
| 82 } |
| 83 return request(requestString).then((responseString) { |
| 84 var r = JSON.decode(responseString); |
| 85 ScriptSource scriptSource = new ScriptSource(r); |
| 86 isolate.scripts[name] = scriptSource; |
| 87 return scriptSource; |
53 }).catchError((e) { | 88 }).catchError((e) { |
54 setResponseError(e.target); | 89 setResponseError(e.target); |
| 90 return null; |
55 }); | 91 }); |
56 } | 92 } |
57 | 93 |
58 /// Abstract method. Given the [requestString], return a String in the | 94 /// Abstract method. Given the [requestString], return a String in the |
59 /// future which contains the reply from the VM service. | 95 /// future which contains the reply from the VM service. |
60 Future<String> request(String requestString); | 96 Future<String> request(String requestString); |
61 } | 97 } |
62 | 98 |
63 | 99 |
64 class HttpRequestManager extends RequestManager { | 100 class HttpRequestManager extends RequestManager { |
65 Future<String> request(String requestString) { | 101 Future<String> request(String requestString) { |
66 return HttpRequest.getString(prefix + requestString); | 102 return HttpRequest.getString(prefix + requestString); |
67 } | 103 } |
68 } | 104 } |
OLD | NEW |