From 5d188da416be754689a68ebbe5cc36d4f25decce Mon Sep 17 00:00:00 2001 From: MaysWind Date: Fri, 27 May 2016 00:29:05 +0800 Subject: [PATCH] websocket support promise --- .../services/aria2WebSocketRpcService.js | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/app/scripts/services/aria2WebSocketRpcService.js b/app/scripts/services/aria2WebSocketRpcService.js index 65b10db..b1eb3ec 100644 --- a/app/scripts/services/aria2WebSocketRpcService.js +++ b/app/scripts/services/aria2WebSocketRpcService.js @@ -1,10 +1,10 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('aria2WebSocketRpcService', ['$websocket', 'ariaNgSettingService', function ($websocket, ariaNgSettingService) { + angular.module('ariaNg').factory('aria2WebSocketRpcService', ['$q', '$websocket', 'ariaNgSettingService', function ($q, $websocket, ariaNgSettingService) { var rpcUrl = ariaNgSettingService.getJsonRpcUrl(); var socketClient = null; - var sendIdMapping = {}; + var sendIdStates = {}; var getSocketClient = function () { if (socketClient == null) { @@ -24,18 +24,29 @@ var uniqueId = content.id; var result = content.result; - if (!sendIdMapping[uniqueId]) { + if (!sendIdStates[uniqueId]) { return; } - var context = sendIdMapping[uniqueId]; + var state = sendIdStates[uniqueId]; + + if (!state) { + return; + } + + var context = state.context; var callbackMethod = context.callback; + state.deferred.resolve({ + success: true, + context: context + }); + if (callbackMethod) { callbackMethod(result); } - delete sendIdMapping[uniqueId]; + delete sendIdStates[uniqueId]; }); } @@ -52,9 +63,16 @@ var uniqueId = context.uniqueId; var requestBody = angular.toJson(context.requestBody); - sendIdMapping[uniqueId] = context; + var deferred = $q.defer(); - return client.send(requestBody); + sendIdStates[uniqueId] = { + context: context, + deferred: deferred + }; + + client.send(requestBody); + + return deferred.promise; } }; }]);