Change lineinfo into async promise

This commit is contained in:
Tor Hveem 2013-10-15 17:20:35 +02:00
parent dd77529cba
commit 5ed02a5e54

View file

@ -277,7 +277,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'models', 'plugins', functi
}]); }]);
weechat.factory('connection', ['$q', '$rootScope', '$log', 'handlers', 'colors', 'models', function($q, $rootScope, $log, handlers, colors, models) { weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers', 'colors', 'models', function($q, $rootScope, $log, storage, handlers, colors, models) {
protocol = new WeeChatProtocol(); protocol = new WeeChatProtocol();
var websocket = null; var websocket = null;
@ -314,6 +314,7 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', 'handlers', 'colors',
websocket.binaryType = "arraybuffer" websocket.binaryType = "arraybuffer"
websocket.onopen = function (evt) { websocket.onopen = function (evt) {
$log.info("Connected to relay");
doSend(WeeChatProtocol.formatInit({ doSend(WeeChatProtocol.formatInit({
password: passwd, password: passwd,
compression: 'off' compression: 'off'
@ -321,7 +322,8 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', 'handlers', 'colors',
doSendWithCallback(WeeChatProtocol.formatHdata({ doSendWithCallback(WeeChatProtocol.formatHdata({
path: 'buffer:gui_buffers(*)', path: 'buffer:gui_buffers(*)',
keys: ['number,full_name,short_name,title'] keys: ['number,full_name,short_name,title']
})).then(function(hdata) { })).then(function(message) {
$log.info("Parsing bufinfo");
var bufferInfos = message['objects'][0]['content']; var bufferInfos = message['objects'][0]['content'];
// buffers objects // buffers objects
for (var i = 0; i < bufferInfos.length ; i++) { for (var i = 0; i < bufferInfos.length ; i++) {
@ -332,16 +334,19 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', 'handlers', 'colors',
models.setActiveBuffer(buffer.id); models.setActiveBuffer(buffer.id);
} }
} }
// Request latest buffer lines for each buffer
$rootScope.getLines();
});
doSend(WeeChatProtocol.formatSync({}));
$log.info("Connected to relay");
$rootScope.connected = true; $rootScope.connected = true;
$rootScope.$apply(); }).then(function() {
$log.info("Parsing lineinfo");
doSendWithCallback(WeeChatProtocol.formatHdata({
path: "buffer:gui_buffers(*)/own_lines/last_line(-"+storage.get('lines')+")/data",
keys: []
})).then(function(hdata) {
handlers.handleLineInfo(hdata);
});
}).then(function() {
doSend(WeeChatProtocol.formatSync({}));
$log.info("Synced");
});
} }
websocket.onclose = function (evt) { websocket.onclose = function (evt) {
@ -380,18 +385,9 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', 'handlers', 'colors',
})); }));
} }
var getLines = function(count) {
doSendWithCallback(WeeChatProtocol.formatHdata({
path: "buffer:gui_buffers(*)/own_lines/last_line(-"+count+")/data",
keys: []
})).then(function(hdata) {
handlers.handleLineInfo(hdata);
});
}
return { return {
send: doSend, send: doSend,
getLines: getLines,
connect: connect, connect: connect,
sendMessage: sendMessage sendMessage: sendMessage
} }
@ -481,9 +477,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
$scope.connect = function() { $scope.connect = function() {
connection.connect($scope.host, $scope.port, $scope.password, $scope.ssl); connection.connect($scope.host, $scope.port, $scope.password, $scope.ssl);
} }
$rootScope.getLines = function() {
connection.getLines($scope.lines);
}
/* Function gets called from bufferLineAdded code if user should be notified */ /* Function gets called from bufferLineAdded code if user should be notified */
$rootScope.createHighlight = function(buffer, message) { $rootScope.createHighlight = function(buffer, message) {