mod/bigbluebuttonbn/amd/src/roomupdater.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * JS room updater.
  17. *
  18. * @module mod_bigbluebuttonbn/roomupdater
  19. * @copyright 2021 Blindside Networks Inc
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Pending from 'core/pending';
  23. import Templates from "core/templates";
  24. import {exception as displayException} from 'core/notification';
  25. import {getMeetingInfo} from './repository';
  26. let timerReference = null;
  27. let timerRunning = false;
  28. let pollInterval = 0;
  29. let pollIntervalFactor = 1;
  30. const MAX_POLL_INTERVAL_FACTOR = 10;
  31. const resetValues = () => {
  32. timerRunning = false;
  33. timerReference = null;
  34. pollInterval = 0;
  35. pollIntervalFactor = 1;
  36. };
  37. /**
  38. * Start the information poller.
  39. * @param {Number} interval interval in miliseconds between each poll action.
  40. */
  41. export const start = (interval) => {
  42. resetValues();
  43. timerRunning = true;
  44. pollInterval = interval;
  45. poll();
  46. };
  47. /**
  48. * Stop the room updater.
  49. */
  50. export const stop = () => {
  51. if (timerReference) {
  52. clearTimeout(timerReference);
  53. }
  54. resetValues();
  55. };
  56. /**
  57. * Start the information poller.
  58. */
  59. const poll = () => {
  60. if (!timerRunning || !pollInterval) {
  61. // The poller has been stopped.
  62. return;
  63. }
  64. updateRoom()
  65. .then((updateOk) => {
  66. if (!updateOk) {
  67. pollIntervalFactor = (pollIntervalFactor < MAX_POLL_INTERVAL_FACTOR) ?
  68. pollIntervalFactor + 1 : MAX_POLL_INTERVAL_FACTOR;
  69. // We make sure if there is an error that we do not try too often.
  70. }
  71. timerReference = setTimeout(() => poll(), pollInterval * pollIntervalFactor);
  72. return true;
  73. })
  74. .catch();
  75. };
  76. /**
  77. * Update the room information.
  78. *
  79. * @param {boolean} [updatecache=false] should we update cache
  80. * @returns {Promise}
  81. */
  82. export const updateRoom = (updatecache = false) => {
  83. const bbbRoomViewElement = document.getElementById('bigbluebuttonbn-room-view');
  84. if (bbbRoomViewElement === null) {
  85. return Promise.resolve(false);
  86. }
  87. const bbbId = bbbRoomViewElement.dataset.bbbId;
  88. const groupId = bbbRoomViewElement.dataset.groupId;
  89. const pendingPromise = new Pending('mod_bigbluebuttonbn/roomupdater:updateRoom');
  90. return getMeetingInfo(bbbId, groupId, updatecache)
  91. .then(data => {
  92. // Just make sure we have the right information for the template.
  93. data.haspresentations = !!(data.presentations && data.presentations.length);
  94. return Templates.renderForPromise('mod_bigbluebuttonbn/room_view', data);
  95. })
  96. .then(({html, js}) => Templates.replaceNode(bbbRoomViewElement, html, js))
  97. .then(() => pendingPromise.resolve())
  98. .catch(displayException);
  99. };