You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.3 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_client.channel', function (require) {
  20. "use strict";
  21. var WebClient = require('web.WebClient');
  22. var BusService = require('bus.BusService');
  23. WebClient.include({
  24. init: function(parent, client_options){
  25. this._super.apply(this, arguments);
  26. this.bus_channels = {};
  27. },
  28. bus_declare_channel: function(channel, method) {
  29. if(!(channel in this.bus_channels)) {
  30. this.bus_channels[channel] = method;
  31. this.call('bus_service', 'addChannel', channel);
  32. }
  33. },
  34. bus_delete_channel: function(channel) {
  35. this.call('bus_service', 'deleteChannel', channel);
  36. this.bus_channels = _.omit(this.bus_channels, channel);
  37. },
  38. show_application: function() {
  39. var res = this._super.apply(this, arguments);
  40. this.call('bus_service', 'onNotification', this, this.bus_notification);
  41. this.call('bus_service', 'startPolling');
  42. return res;
  43. },
  44. bus_notification: function(notifications) {
  45. _.each(notifications, function(notification, index) {
  46. var channel = notification[0];
  47. var message = notification[1];
  48. if(channel in this.bus_channels) {
  49. this.bus_channels[channel](message);
  50. }
  51. }, this);
  52. },
  53. destroy: function() {
  54. _.each(this.bus_channels, function(method, channel) {
  55. this.bus_delete_channel(channel);
  56. }, this);
  57. this.call('bus_service', 'stopPolling');
  58. this._super.apply(this, arguments);
  59. },
  60. });
  61. });