From 1f455c813a6cc9cf0d74f29cf58d14ef5fb869a3 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 27 Oct 2013 15:41:45 -0400 Subject: [PATCH] Callback ids cycle from 0 to 1000 --- js/websockets.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/js/websockets.js b/js/websockets.js index 7da4b38..7cfdc43 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -196,17 +196,35 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers', var callbacks = {} var currentCallBackId = 0; + /* + * Returns the current callback id + */ + var getCurrentCallBackId = function() { + + currentCallBackId += 1; + + if (currentCallBackId > 1000) { + currentCallBackId = 0; + } + + return currentCallBackId; + } + /* * Create a callback, adds it to the callback list * and return it. */ var createCallback = function() { var defer = $q.defer(); - callbacks[++currentCallBackId] = { + var cbId = getCurrentCallBackId(); + + callbacks[cbId] = { time: new Date, - cb: defer + cb: defer, } - callBackIdString = "(" + currentCallBackId + ")"; + + defer.id = cbId + return defer; } @@ -232,7 +250,7 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers', var send = function(message) { message.replace(/[\r\n]+$/g, "").split("\n"); var cb = createCallback(message); - websocket.send(callBackIdString + " " + message); + websocket.send("(" + cb.id + ") " + message); return cb.promise; }