event_utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.WaitOnType = exports.EventBus = exports.AutomationEventBus = void 0;
  27. exports.waitOnEventOrTimeout = waitOnEventOrTimeout;
  28. const WaitOnType = {
  29. EVENT: "event",
  30. TIMEOUT: "timeout"
  31. };
  32. exports.WaitOnType = WaitOnType;
  33. function waitOnEventOrTimeout({
  34. target,
  35. name,
  36. delay = 0
  37. }) {
  38. return new Promise(function (resolve, reject) {
  39. if (typeof target !== "object" || !(name && typeof name === "string") || !(Number.isInteger(delay) && delay >= 0)) {
  40. throw new Error("waitOnEventOrTimeout - invalid parameters.");
  41. }
  42. function handler(type) {
  43. if (target instanceof EventBus) {
  44. target._off(name, eventHandler);
  45. } else {
  46. target.removeEventListener(name, eventHandler);
  47. }
  48. if (timeout) {
  49. clearTimeout(timeout);
  50. }
  51. resolve(type);
  52. }
  53. const eventHandler = handler.bind(null, WaitOnType.EVENT);
  54. if (target instanceof EventBus) {
  55. target._on(name, eventHandler);
  56. } else {
  57. target.addEventListener(name, eventHandler);
  58. }
  59. const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
  60. const timeout = setTimeout(timeoutHandler, delay);
  61. });
  62. }
  63. class EventBus {
  64. #listeners = Object.create(null);
  65. on(eventName, listener, options = null) {
  66. this._on(eventName, listener, {
  67. external: true,
  68. once: options?.once
  69. });
  70. }
  71. off(eventName, listener, options = null) {
  72. this._off(eventName, listener, {
  73. external: true,
  74. once: options?.once
  75. });
  76. }
  77. dispatch(eventName, data) {
  78. const eventListeners = this.#listeners[eventName];
  79. if (!eventListeners || eventListeners.length === 0) {
  80. return;
  81. }
  82. let externalListeners;
  83. for (const {
  84. listener,
  85. external,
  86. once
  87. } of eventListeners.slice(0)) {
  88. if (once) {
  89. this._off(eventName, listener);
  90. }
  91. if (external) {
  92. (externalListeners ||= []).push(listener);
  93. continue;
  94. }
  95. listener(data);
  96. }
  97. if (externalListeners) {
  98. for (const listener of externalListeners) {
  99. listener(data);
  100. }
  101. externalListeners = null;
  102. }
  103. }
  104. _on(eventName, listener, options = null) {
  105. const eventListeners = this.#listeners[eventName] ||= [];
  106. eventListeners.push({
  107. listener,
  108. external: options?.external === true,
  109. once: options?.once === true
  110. });
  111. }
  112. _off(eventName, listener, options = null) {
  113. const eventListeners = this.#listeners[eventName];
  114. if (!eventListeners) {
  115. return;
  116. }
  117. for (let i = 0, ii = eventListeners.length; i < ii; i++) {
  118. if (eventListeners[i].listener === listener) {
  119. eventListeners.splice(i, 1);
  120. return;
  121. }
  122. }
  123. }
  124. }
  125. exports.EventBus = EventBus;
  126. class AutomationEventBus extends EventBus {
  127. dispatch(eventName, data) {
  128. throw new Error("Not implemented: AutomationEventBus.dispatch");
  129. }
  130. }
  131. exports.AutomationEventBus = AutomationEventBus;