OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var binding = require('binding').Binding.create('serial'); | 5 var binding = require('binding').Binding.create('serial'); |
6 | 6 |
raymes
2014/08/14 02:58:04
Please add a comment to this file explaining what
Sam McNally
2014/08/14 04:33:51
Done.
| |
7 function createAsyncProxy(targetPromise, functionNames) { | 7 function createAsyncProxy(targetPromise, functionNames) { |
8 var functionProxies = {}; | 8 var functionProxies = {}; |
9 $Array.forEach(functionNames, function(name) { | 9 $Array.forEach(functionNames, function(name) { |
10 functionProxies[name] = function() { | 10 functionProxies[name] = function() { |
11 var args = arguments; | 11 var args = arguments; |
12 return targetPromise.then(function(target) { | 12 return targetPromise.then(function(target) { |
13 return $Function.apply(target[name], target, args); | 13 return $Function.apply(target[name], target, args); |
14 }); | 14 }); |
15 } | 15 }; |
16 }); | 16 }); |
17 return functionProxies; | 17 return functionProxies; |
18 } | 18 } |
19 | 19 |
20 var serialService = createAsyncProxy(requireAsync('serial_service'), [ | 20 var serialService = createAsyncProxy(requireAsync('serial_service'), [ |
21 'getDevices', | 21 'getDevices', |
22 'createConnection', | |
23 'getConnection', | |
24 'getConnections', | |
22 ]); | 25 ]); |
23 | 26 |
27 function forwardToConnection(methodName) { | |
28 return function(connectionId) { | |
29 var args = $Array.slice(arguments, 1); | |
30 return serialService.getConnection(connectionId).then(function(connection) { | |
31 return $Function.apply(connection[methodName], connection, args); | |
32 }); | |
33 }; | |
34 } | |
35 | |
24 binding.registerCustomHook(function(bindingsAPI) { | 36 binding.registerCustomHook(function(bindingsAPI) { |
25 var apiFunctions = bindingsAPI.apiFunctions; | 37 var apiFunctions = bindingsAPI.apiFunctions; |
26 apiFunctions.setHandleRequestWithPromise('getDevices', | 38 apiFunctions.setHandleRequestWithPromise('getDevices', |
27 serialService.getDevices); | 39 serialService.getDevices); |
40 | |
41 apiFunctions.setHandleRequestWithPromise('connect', function(path, options) { | |
42 return serialService.createConnection(path, options).then(function(result) { | |
43 return result.info; | |
44 }).catch (function(e) { | |
45 throw new Error('Failed to connect to the port.'); | |
46 }); | |
47 }); | |
48 | |
49 apiFunctions.setHandleRequestWithPromise( | |
50 'disconnect', forwardToConnection('close')); | |
51 apiFunctions.setHandleRequestWithPromise( | |
52 'getInfo', forwardToConnection('getInfo')); | |
53 apiFunctions.setHandleRequestWithPromise( | |
54 'update', forwardToConnection('setOptions')); | |
55 apiFunctions.setHandleRequestWithPromise( | |
56 'getControlSignals', forwardToConnection('getControlSignals')); | |
57 apiFunctions.setHandleRequestWithPromise( | |
58 'setControlSignals', forwardToConnection('setControlSignals')); | |
59 apiFunctions.setHandleRequestWithPromise( | |
60 'flush', forwardToConnection('flush')); | |
61 apiFunctions.setHandleRequestWithPromise( | |
62 'setPaused', forwardToConnection('setPaused')); | |
63 | |
64 apiFunctions.setHandleRequestWithPromise('getConnections', function() { | |
65 return serialService.getConnections().then(function(connections) { | |
66 var promises = []; | |
67 for (var id in connections) { | |
68 promises.push(connections[id].getInfo()); | |
69 } | |
70 return Promise.all(promises); | |
71 }); | |
72 }); | |
28 }); | 73 }); |
29 | 74 |
30 exports.binding = binding.generate(); | 75 exports.binding = binding.generate(); |
OLD | NEW |