diff --git a/res/app/components/stf/control/control-service.js b/res/app/components/stf/control/control-service.js index aa44d0ad..1215cd36 100644 --- a/res/app/components/stf/control/control-service.js +++ b/res/app/components/stf/control/control-service.js @@ -7,7 +7,7 @@ module.exports = function ControlServiceFactory( var controlService = { } - function ControlService(devices, channel) { + function ControlService(target, channel) { var keyCodes = { 8: 8 // backspace , 13: 13 // enter @@ -79,7 +79,7 @@ module.exports = function ControlServiceFactory( } this.shell = function(command) { - var tx = TransactionService.create(devices) + var tx = TransactionService.create(target) socket.emit('shell.command', channel, tx.channel, { command: command , timeout: 10000 @@ -88,7 +88,7 @@ module.exports = function ControlServiceFactory( } this.identify = function() { - var tx = TransactionService.create(devices) + var tx = TransactionService.create(target) socket.emit('device.identify', channel, tx.channel) return tx } @@ -102,7 +102,7 @@ module.exports = function ControlServiceFactory( .then(function(response) { var manifest = response.data.manifest var app = manifest.application - var tx = TransactionService.create(devices) + var tx = TransactionService.create(target) var params = { url: response.data.url } @@ -121,12 +121,8 @@ module.exports = function ControlServiceFactory( } } - controlService.forOne = function(device, channel) { - return new ControlService([device], channel) - } - - controlService.forMany = function(devices, channel) { - return new ControlService(devices, channel) + controlService.create = function(target, channel) { + return new ControlService(target, channel) } return controlService diff --git a/res/app/components/stf/control/transaction-service.js b/res/app/components/stf/control/transaction-service.js index 93cc7e98..320923e9 100644 --- a/res/app/components/stf/control/transaction-service.js +++ b/res/app/components/stf/control/transaction-service.js @@ -8,7 +8,7 @@ module.exports = function TransactionServiceFactory(socket) { return 'tx.' + uuid.v4() } - function Transaction(devices) { + function MultiDeviceTransaction(devices) { var pending = Object.create(null) , results = [] , channel = createChannel() @@ -49,6 +49,43 @@ module.exports = function TransactionServiceFactory(socket) { }) } + function SingleDeviceTransaction(device) { + var pending = new PendingTransactionResult(device) + , result = pending.result + , channel = createChannel() + + function doneListener(someChannel, data) { + if (someChannel === channel) { + pending.done(data) + } + } + + function progressListener(someChannel, data) { + if (someChannel === channel) { + pending.progress(data) + } + } + + socket.on('tx.done', doneListener) + socket.on('tx.progress', progressListener) + + this.channel = channel + this.result = result + this.results = [result] + this.promise = pending.promise + .finally(function() { + socket.removeListener('tx.done', doneListener) + socket.removeListener('tx.progress', progressListener) + socket.emit('tx.cleanup', channel) + }) + .progressed(function() { + return result + }) + .then(function() { + return result + }) + } + function PendingTransactionResult(device) { var resolver = Promise.defer() , result = new TransactionResult(device) @@ -123,8 +160,13 @@ module.exports = function TransactionServiceFactory(socket) { this.lastData = null } - transactionService.create = function(devices) { - return new Transaction(devices) + transactionService.create = function(target) { + if (Array.isArray(target)) { + return new MultiDeviceTransaction(target) + } + else { + return new SingleDeviceTransaction(target) + } } return transactionService diff --git a/res/app/components/stf/user/group/group-service.js b/res/app/components/stf/user/group/group-service.js index 83ac334b..c03671d6 100644 --- a/res/app/components/stf/user/group/group-service.js +++ b/res/app/components/stf/user/group/group-service.js @@ -6,34 +6,34 @@ module.exports = function GroupServiceFactory( } groupService.invite = function (device) { - var tx = TransactionService.create([device]) + var tx = TransactionService.create(device) socket.emit('group.invite', device.channel, tx.channel, { serial: { value: device.serial , match: 'exact' } }) - return tx.promise.then(function(results) { - if (!results[0].success) { + return tx.promise.then(function(result) { + if (!result.success) { throw new Error('Device refused to join the group') } - return results[0].device + return result.device }) } groupService.kick = function (device) { - var tx = TransactionService.create([device]) + var tx = TransactionService.create(device) socket.emit('group.kick', device.channel, tx.channel, { serial: { value: device.serial , match: 'exact' } }) - return tx.promise.then(function(results) { - if (!results[0].success) { + return tx.promise.then(function(result) { + if (!result.success) { throw new Error('Device refused to be kicked from the group') } - return results[0].device + return result.device }) } diff --git a/res/app/device-control/device-control-controller.js b/res/app/device-control/device-control-controller.js index 7f328f5f..f56da434 100644 --- a/res/app/device-control/device-control-controller.js +++ b/res/app/device-control/device-control-controller.js @@ -15,14 +15,14 @@ module.exports = function DeviceControlCtrl( return $scope.control.install($files) .then(function(tx) { return tx.promise - .progressed(function(results) { + .progressed(function(result) { $scope.$apply(function() { - $scope.installation = results[0] + $scope.installation = result }) }) - .then(function(results) { + .then(function(result) { $scope.$apply(function() { - $scope.installation = results[0] + $scope.installation = result }) }) }) @@ -34,7 +34,7 @@ module.exports = function DeviceControlCtrl( }) .then(function(device) { $scope.device = device - $scope.control = ControlService.forOne(device, device.channel) + $scope.control = ControlService.create(device, device.channel) return device }) .catch(function() {