event_utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. constructor() {
  65. this._listeners = Object.create(null);
  66. }
  67. on(eventName, listener, options = null) {
  68. this._on(eventName, listener, {
  69. external: true,
  70. once: options?.once
  71. });
  72. }
  73. off(eventName, listener, options = null) {
  74. this._off(eventName, listener, {
  75. external: true,
  76. once: options?.once
  77. });
  78. }
  79. dispatch(eventName, data) {
  80. const eventListeners = this._listeners[eventName];
  81. if (!eventListeners || eventListeners.length === 0) {
  82. return;
  83. }
  84. let externalListeners;
  85. for (const {
  86. listener,
  87. external,
  88. once
  89. } of eventListeners.slice(0)) {
  90. if (once) {
  91. this._off(eventName, listener);
  92. }
  93. if (external) {
  94. (externalListeners ||= []).push(listener);
  95. continue;
  96. }
  97. listener(data);
  98. }
  99. if (externalListeners) {
  100. for (const listener of externalListeners) {
  101. listener(data);
  102. }
  103. externalListeners = null;
  104. }
  105. }
  106. _on(eventName, listener, options = null) {
  107. const eventListeners = this._listeners[eventName] ||= [];
  108. eventListeners.push({
  109. listener,
  110. external: options?.external === true,
  111. once: options?.once === true
  112. });
  113. }
  114. _off(eventName, listener, options = null) {
  115. const eventListeners = this._listeners[eventName];
  116. if (!eventListeners) {
  117. return;
  118. }
  119. for (let i = 0, ii = eventListeners.length; i < ii; i++) {
  120. if (eventListeners[i].listener === listener) {
  121. eventListeners.splice(i, 1);
  122. return;
  123. }
  124. }
  125. }
  126. }
  127. exports.EventBus = EventBus;
  128. class AutomationEventBus extends EventBus {
  129. dispatch(eventName, data) {
  130. throw new Error("Not implemented: AutomationEventBus.dispatch");
  131. }
  132. }
  133. exports.AutomationEventBus = AutomationEventBus;