lazy-timeout.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, setTimeout, clearTimeout */
  24. "use strict";
  25. const timeouts = new Map();
  26. browser.runtime.onMessage.addListener((message, sender) => {
  27. if (message.method == "singlefile.lazyTimeout.setTimeout") {
  28. let tabTimeouts = timeouts.get(sender.tab.id);
  29. let frameTimeouts;
  30. if (tabTimeouts) {
  31. frameTimeouts = tabTimeouts.get(sender.frameId);
  32. const previousTimeoutId = frameTimeouts.get(message.type);
  33. if (previousTimeoutId) {
  34. clearTimeout(previousTimeoutId);
  35. }
  36. }
  37. const timeoutId = setTimeout(async () => {
  38. try {
  39. const tabTimeouts = timeouts.get(sender.tab.id);
  40. const frameTimeouts = tabTimeouts.get(sender.frameId);
  41. if (tabTimeouts) {
  42. deleteTimeout(frameTimeouts, message.type);
  43. }
  44. await browser.tabs.sendMessage(sender.tab.id, { method: "singlefile.lazyTimeout.onTimeout", type: message.type });
  45. } catch (error) {
  46. // ignored
  47. }
  48. }, message.delay);
  49. if (!tabTimeouts) {
  50. tabTimeouts = new Map();
  51. frameTimeouts = new Map();
  52. tabTimeouts.set(sender.frameId, frameTimeouts);
  53. timeouts.set(sender.tab.id, tabTimeouts);
  54. }
  55. frameTimeouts.set(message.type, timeoutId);
  56. return Promise.resolve({});
  57. }
  58. if (message.method == "singlefile.lazyTimeout.clearTimeout") {
  59. let tabTimeouts = timeouts.get(sender.tab.id);
  60. if (tabTimeouts) {
  61. const frameTimeouts = tabTimeouts.get(sender.frameId);
  62. if (frameTimeouts) {
  63. const timeoutId = frameTimeouts.get(message.type);
  64. if (timeoutId) {
  65. clearTimeout(timeoutId);
  66. }
  67. deleteTimeout(frameTimeouts, message.type);
  68. }
  69. }
  70. return Promise.resolve({});
  71. }
  72. });
  73. browser.tabs.onRemoved.addListener(tabId => timeouts.delete(tabId));
  74. function deleteTimeout(framesTimeouts, type) {
  75. framesTimeouts.delete(type);
  76. }