Files
odoo_source/addons/bus/static/src/js/bus.js
T
Géry Debongnie 4fdf74fe21 [IMP] web+addons: improvement to module system
The module system needs to know the dependencies of a given module
before executing the function.  This is why the dependencies were
defined once in an array, and then were described one more times in the
call to require.

But a trick can simplify this: the boot function can parse the string
representation of the module and extract the calls to require from it.
It is more work for the processor, but it leads to simpler module
definitions.
2015-03-18 09:23:37 +01:00

67 lines
1.9 KiB
JavaScript

odoo.define('bus.bus', function (require) {
var session = require('web.session');
var Widget = require('web.Widget');
var bus = {};
bus.ERROR_DELAY = 10000;
bus.Bus = Widget.extend({
init: function(){
this._super();
this.options = {};
this.activated = false;
this.channels = [];
this.last = 0;
this.stop = false;
},
start_polling: function(){
if(!this.activated){
this.poll();
this.stop = false;
}
},
stop_polling: function(){
this.activated = false;
this.stop = true;
this.channels = [];
},
poll: function() {
var self = this;
self.activated = true;
var data = {'channels': self.channels, 'last': self.last, 'options' : self.options};
session.rpc('/longpolling/poll', data, {shadow : true}).then(function(result) {
_.each(result, _.bind(self.on_notification, self));
if(!self.stop){
self.poll();
}
}, function(unused, e) {
// no error popup if request is interrupted or fails for any reason
e.preventDefault();
// random delay to avoid massive longpolling
setTimeout(_.bind(self.poll, self), bus.ERROR_DELAY + (Math.floor((Math.random()*20)+1)*1000));
});
},
on_notification: function(notification) {
if (notification.id > this.last) {
this.last = notification.id;
}
this.trigger("notification", [notification.channel, notification.message]);
},
add_channel: function(channel){
if(!_.contains(this.channels, channel)){
this.channels.push(channel);
}
},
delete_channel: function(channel){
this.channels = _.without(this.channels, channel);
},
});
// singleton
bus.bus = new bus.Bus();
return bus;
});