From 8c386f7553680a5e9538b8c16839aa0c6a5c9e1f Mon Sep 17 00:00:00 2001 From: David Cormier Date: Mon, 17 Feb 2014 21:15:10 -0500 Subject: [PATCH 1/3] Create helper functions for initialization methods --- js/glowingbear.js | 78 +++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 13b63b3..95ecd5f 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -269,21 +269,56 @@ function($rootScope, var onopen = function () { + var _initializeConnection = function(passwd) { + return ngWebsockets.sendAll([ + weeChat.Protocol.formatInit({ + password: passwd, + compression: noCompression ? 'off' : 'zlib' + }), + + weeChat.Protocol.formatInfo({ + name: 'version' + }) + ]) + }; + + var _requestHotlist = function() { + return ngWebsockets.send( + weeChat.Protocol.formatHdata({ + path: "hotlist:gui_hotlist(*)", + keys: [] + }) + ); + }; + + var _requestNicklist = function() { + return ngWebsockets.send( + weeChat.Protocol.formatNicklist({ + }) + ); + }; + + var _requestBufferInfos = function() { + return ngWebsockets.send( + weeChat.Protocol.formatHdata({ + path: 'buffer:gui_buffers(*)', + keys: ['local_variables,notify,number,full_name,short_name,title'] + }) + ); + }; + + var _requestSync = function() { + return ngWebsockets.send( + weeChat.Protocol.formatSync({}) + ); + }; + $log.info("Connected to relay"); // First command asks for the password and issues // a version command. If it fails, it means the we // did not provide the proper password. - ngWebsockets.sendAll([ - weeChat.Protocol.formatInit({ - password: passwd, - compression: noCompression ? 'off' : 'zlib' - }), - - weeChat.Protocol.formatInfo({ - name: 'version' - }) - ]).then( + _initializeConnection(passwd).then( null, function() { // Connection got closed, lets check if we ever was connected successfully @@ -292,12 +327,7 @@ function($rootScope, } ); - ngWebsockets.send( - weeChat.Protocol.formatHdata({ - path: 'buffer:gui_buffers(*)', - keys: ['local_variables,notify,number,full_name,short_name,title'] - }) - ).then(function(bufinfo) { + _requestBufferInfos().then(function(bufinfo) { var bufferInfos = bufinfo.objects[0].content; // buffers objects for (var i = 0; i < bufferInfos.length ; i++) { @@ -311,25 +341,15 @@ function($rootScope, }); // Send all the other commands required for initialization - ngWebsockets.send( - weeChat.Protocol.formatHdata({ - path: "hotlist:gui_hotlist(*)", - keys: [] - }) - ).then(function(hotlist) { + _requestHotlist().then(function(hotlist) { handlers.handleHotlistInfo(hotlist); }); - ngWebsockets.send( - weeChat.Protocol.formatNicklist({ - }) - ).then(function(nicklist) { + _requestNicklist().then(function(nicklist) { handlers.handleNicklist(nicklist); }); - ngWebsockets.send( - weeChat.Protocol.formatSync({}) - ); + _requestSync(); $rootScope.connected = true; From ba20a79c72ffb978d4eabf0eaa9852955a9d0467 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Mon, 17 Feb 2014 21:18:33 -0500 Subject: [PATCH 2/3] Send init commands when connection is successful Initialization commands are sent when we are sure that the password has been accepted and that the init has been processed by the weechat relay --- js/glowingbear.js | 58 ++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 95ecd5f..426dd17 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -269,6 +269,8 @@ function($rootScope, var onopen = function () { + + // Helper methods for initialization commands var _initializeConnection = function(passwd) { return ngWebsockets.sendAll([ weeChat.Protocol.formatInit({ @@ -313,13 +315,39 @@ function($rootScope, ); }; - $log.info("Connected to relay"); // First command asks for the password and issues // a version command. If it fails, it means the we // did not provide the proper password. _initializeConnection(passwd).then( - null, + function() { + // Connection is successful + // Send all the other commands required for initialization + _requestBufferInfos().then(function(bufinfo) { + var bufferInfos = bufinfo.objects[0].content; + // buffers objects + for (var i = 0; i < bufferInfos.length ; i++) { + var buffer = new models.Buffer(bufferInfos[i]); + models.addBuffer(buffer); + // Switch to first buffer on startup + if (i === 0) { + models.setActiveBuffer(buffer.id); + } + } + }); + + _requestHotlist().then(function(hotlist) { + handlers.handleHotlistInfo(hotlist); + }); + + _requestNicklist().then(function(nicklist) { + handlers.handleNicklist(nicklist); + }); + + _requestSync(); + $log.info("Connected to relay"); + $rootScope.connected = true; + }, function() { // Connection got closed, lets check if we ever was connected successfully if(!$rootScope.waseverconnected) @@ -327,32 +355,6 @@ function($rootScope, } ); - _requestBufferInfos().then(function(bufinfo) { - var bufferInfos = bufinfo.objects[0].content; - // buffers objects - for (var i = 0; i < bufferInfos.length ; i++) { - var buffer = new models.Buffer(bufferInfos[i]); - models.addBuffer(buffer); - // Switch to first buffer on startup - if (i === 0) { - models.setActiveBuffer(buffer.id); - } - } - }); - - // Send all the other commands required for initialization - _requestHotlist().then(function(hotlist) { - handlers.handleHotlistInfo(hotlist); - }); - - _requestNicklist().then(function(nicklist) { - handlers.handleNicklist(nicklist); - }); - - _requestSync(); - - $rootScope.connected = true; - }; var onmessage = function(event) { From 27690ada5806463fb54cc29df9eab4a570217a1e Mon Sep 17 00:00:00 2001 From: David Cormier Date: Mon, 17 Feb 2014 21:46:00 -0500 Subject: [PATCH 3/3] Work around a WeeChat protocol inconvenience Until WeeChat sends a confirmation for init we have to assume that the commands will be received synchronously even though they are sent asynchronously --- js/glowingbear.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 426dd17..7de20f9 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -272,16 +272,23 @@ function($rootScope, // Helper methods for initialization commands var _initializeConnection = function(passwd) { - return ngWebsockets.sendAll([ + + // This is not the proper way to do this. + // WeeChat does not send a confirmation for the init. + // Until it does, We need to "assume" that formatInit + // will be received before formatInfo + ngWebsockets.send( weeChat.Protocol.formatInit({ password: passwd, compression: noCompression ? 'off' : 'zlib' - }), + }) + ); + return ngWebsockets.send( weeChat.Protocol.formatInfo({ name: 'version' }) - ]) + ); }; var _requestHotlist = function() {