Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(593)

Side by Side Diff: sdk/lib/developer/extension.dart

Issue 1282883003: Move service extension handler execution to a timer (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 dart.developer; 5 part of dart.developer;
6 6
7 class ServiceExtensionResponse { 7 class ServiceExtensionResponse {
8 final String _result; 8 final String _result;
9 final int _errorCode; 9 final int _errorCode;
10 final String _errorDetail; 10 final String _errorDetail;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 'handler', 99 'handler',
100 'Must be a ServiceExtensionHandler'); 100 'Must be a ServiceExtensionHandler');
101 } 101 }
102 _extensions[method] = handler; 102 _extensions[method] = handler;
103 } 103 }
104 104
105 bool _extensionExists(String method) { 105 bool _extensionExists(String method) {
106 return _extensions[method] != null; 106 return _extensions[method] != null;
107 } 107 }
108 108
109 bool _invokeExtension(String method, 109 bool _invokeExtension(String method,
Ivan Posva 2015/08/10 21:40:21 _scheduleExtension?
Cutch 2015/08/10 21:56:20 Done.
110 List<String> parameterKeys, 110 List<String> parameterKeys,
111 List<String> parameterValues, 111 List<String> parameterValues,
112 SendPort replyPort, 112 SendPort replyPort,
113 Object id) { 113 Object id) {
114 ServiceExtensionHandler handler = _extensions[method]; 114 ServiceExtensionHandler handler = _extensions[method];
Ivan Posva 2015/08/10 21:40:21 Combine this with the check for extension and retu
Cutch 2015/08/10 21:56:20 Done.
115 assert(handler != null); 115 assert(handler != null);
116 var parameters = {}; 116 // Defer execution of handler until next event loop.
117 for (var i = 0; i < parameterKeys.length; i++) { 117 Timer.run(() {
118 parameters[parameterKeys[i]] = parameterValues[i]; 118 var parameters = {};
119 } 119 for (var i = 0; i < parameterKeys.length; i++) {
120 var response; 120 parameters[parameterKeys[i]] = parameterValues[i];
121 try { 121 }
122 response = handler(method, parameters); 122 var response;
123 } catch (e, st) { 123 try {
124 var errorDetails = (st == null) ? '$e' : '$e\n$st'; 124 response = handler(method, parameters);
125 response = new ServiceExtensionResponse.error( 125 } catch (e, st) {
126 ServiceExtensionResponse.kExtensionError, 126 var errorDetails = (st == null) ? '$e' : '$e\n$st';
127 errorDetails);
128 _postResponse(replyPort, id, response);
129 return true;
130 }
131 if (response is! Future) {
132 response = new ServiceExtensionResponse.error(
133 ServiceExtensionResponse.kExtensionError,
134 "Extension handler must return a Future");
135 _postResponse(replyPort, id, response);
136 return true;
137 }
138 response.catchError((e, st) {
139 var errorDetails = (st == null) ? '$e' : '$e\n$st';
140 return new ServiceExtensionResponse.error(
141 ServiceExtensionResponse.kExtensionError,
142 errorDetails);
143 }).then((response) {
144 if (response == null) {
145 response = new ServiceExtensionResponse.error( 127 response = new ServiceExtensionResponse.error(
146 ServiceExtensionResponse.kExtensionError, 128 ServiceExtensionResponse.kExtensionError,
147 "Extension handler returned null"); 129 errorDetails);
130 _postResponse(replyPort, id, response);
131 return;
148 } 132 }
149 _postResponse(replyPort, id, response); 133 if (response is! Future) {
134 response = new ServiceExtensionResponse.error(
135 ServiceExtensionResponse.kExtensionError,
136 "Extension handler must return a Future");
137 _postResponse(replyPort, id, response);
138 return;
139 }
140 response.catchError((e, st) {
141 var errorDetails = (st == null) ? '$e' : '$e\n$st';
142 return new ServiceExtensionResponse.error(
143 ServiceExtensionResponse.kExtensionError,
144 errorDetails);
145 }).then((response) {
146 if (response == null) {
147 response = new ServiceExtensionResponse.error(
148 ServiceExtensionResponse.kExtensionError,
149 "Extension handler returned null");
150 }
151 _postResponse(replyPort, id, response);
152 });
150 }); 153 });
151 // Push an event on the event loop so that we invoke the scheduled microtasks.
152 Timer.run(() {});
153 return true; 154 return true;
154 } 155 }
155 156
156 _postResponse(SendPort replyPort, 157 _postResponse(SendPort replyPort,
157 Object id, 158 Object id,
158 ServiceExtensionResponse response) { 159 ServiceExtensionResponse response) {
159 assert(replyPort != null); 160 assert(replyPort != null);
160 if (id == null) { 161 if (id == null) {
161 // No id -> no response. 162 // No id -> no response.
162 // TODO(johnmccutchan): This code and the code in service.cc leave the 163 // TODO(johnmccutchan): This code and the code in service.cc leave the
(...skipping 10 matching lines...) Expand all
173 sb.write('"result":'); 174 sb.write('"result":');
174 } 175 }
175 sb.write('${response._toString()},'); 176 sb.write('${response._toString()},');
176 if (id is String) { 177 if (id is String) {
177 sb.write('"id":"$id"}'); 178 sb.write('"id":"$id"}');
178 } else { 179 } else {
179 sb.write('"id":$id}'); 180 sb.write('"id":$id}');
180 } 181 }
181 replyPort.send(sb.toString()); 182 replyPort.send(sb.toString());
182 } 183 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698