565ebb2e by Ean Schuessler

#5084 various improvements

 Handle exceptions in JSON decoding, delay refresh when
 a code 200 occurs, handle channel subscription in the
 library
1 parent f18a1068
define([], function() {
var listeners = [];
var channels = {};
var messenger = {
process: function() {
var self = this;
var url = "/comet/messenger"
var request = new XMLHttpRequest();
var responseLength = 0;
......@@ -13,9 +14,7 @@ define([], function() {
if (request.responseText) {
var newText = request.responseText.substr(responseLength);
if (newText.length > 0) {
for (var i=0; i < listeners.length; i++) {
listeners[i](newText);
}
self.deliver(newText);
}
responseLength = request.responseText.length;
}
......@@ -34,14 +33,33 @@ define([], function() {
};
request.send(null);
},
deliver: function(newText) {
try {
var json = JSON.parse(newText);
if (json.channel && channels[json.channel]) {
for (var i=0; i < channels[json.channel].length; i++) {
channels[json.channel][i](json);
}
}
} catch (err) {
console.log('Error processing COMETd message', newText, err);
}
},
addListener: function(listener) {
listeners.push(listener);
this.addListener('ALL', listener);
},
removeListener: function(listener) {
var index = listeners.indexOf(listener);
if (index > -1) {
array.splice(index, 1);
addListener: function(channel, listener) {
if (!channels[channel]) {
channels[channel] = [];
}
channels[channel].push(listener);
},
removeListener: function(listener) {
this.removeListener('ALL', listener);
},
removeListener: function(channel, listener) {
console.log('Remove listener from channel', listener, channel);
}
}
messenger.process();
......