OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.developer; | |
6 | |
7 /// Index of [response] that the result should be stored. | |
8 const kResultIndex = 0; | |
9 /// Index of [response] that the error code should be stored. | |
10 const kErrorCodeIndex = 1; | |
11 /// Index of [response] that the error message be stored. | |
12 const kErrorMessageIndex = 2; | |
13 | |
14 /// A service protocol extension handler. Registered with [registerExtension]. | |
15 /// | |
16 /// Must modify [response] according to the following contract: | |
17 /// On success, set index 0 to a JSON encoded result | |
18 /// OR | |
19 /// On failure, set index 1 to an error code and index 2 to a error message. | |
20 /// | |
21 /// [method] - the method name. | |
22 /// [parameters] - the parameters. | |
23 /// [response] - a list of length three that the handler must set. | |
24 typedef void ServiceExtensionHandler(String method, | |
25 Map paremeters, | |
26 List<String> response); | |
27 | |
28 final _extensions = new Map<String, ServiceExtensionHandler>(); | |
29 | |
30 /// Register a [ServiceExtensionHandler] that will be invoked in this isolate | |
31 /// for [method]. | |
32 void registerExtension(String method, ServiceExtensionHandler handler) { | |
33 if (_extensions[method] != null) { | |
34 throw new ArgumentError('Extension already registered: $method.'); | |
35 } | |
36 if (handler is! ServiceExtensionHandler) { | |
37 throw new ArgumentError('handler must be a ServiceExtensionHandler.'); | |
38 } | |
39 _extensions[method] = handler; | |
40 } | |
41 | |
42 bool _extensionExists(String method) { | |
43 return _extensions[method] != null; | |
44 } | |
45 | |
46 bool _invokeExtension(String method, | |
turnidge
2015/08/04 18:21:27
As discussed offline, let's make this async.
| |
47 List<String> parameterKeys, | |
48 List<String> parameterValues, | |
49 List<String> response) { | |
50 ServiceExtensionHandler handler = _extensions[method]; | |
51 if (handler == null) { | |
52 return false; | |
53 } | |
54 var parameters = {}; | |
55 for (var i = 0; i < parameterKeys.length; i++) { | |
56 parameters[parameterKeys[i]] = parameterValues[i]; | |
57 } | |
58 handler(method, parameters, response); | |
59 return true; | |
60 } | |
OLD | NEW |