EventEmitter.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  5. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  6. };
  7. var _EventEmitter_listeners;
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.EventEmitter = void 0;
  10. class EventEmitter {
  11. constructor() {
  12. _EventEmitter_listeners.set(this, {});
  13. }
  14. /**
  15. * Adds the listener function to the end of the listeners array for the event.
  16. * No checks are made to see if the listener has already been added. Multiple calls passing
  17. * the same combination of event and listener will result in the listener being added, and
  18. * called, multiple times.
  19. * @returns this, so that calls can be chained
  20. */
  21. on(event, listener) {
  22. const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event] || (__classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event] = []);
  23. listeners.push({ listener });
  24. return this;
  25. }
  26. /**
  27. * Removes the specified listener from the listener array for the event.
  28. * off() will remove, at most, one instance of a listener from the listener array. If any single
  29. * listener has been added multiple times to the listener array for the specified event, then
  30. * off() must be called multiple times to remove each instance.
  31. * @returns this, so that calls can be chained
  32. */
  33. off(event, listener) {
  34. const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event];
  35. if (!listeners)
  36. return this;
  37. const index = listeners.findIndex((l) => l.listener === listener);
  38. if (index >= 0)
  39. listeners.splice(index, 1);
  40. return this;
  41. }
  42. /**
  43. * Adds a one-time listener function for the event. The next time the event is triggered,
  44. * this listener is removed and then invoked.
  45. * @returns this, so that calls can be chained
  46. */
  47. once(event, listener) {
  48. const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event] || (__classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event] = []);
  49. listeners.push({ listener, once: true });
  50. return this;
  51. }
  52. /**
  53. * This is similar to `.once()`, but returns a Promise that resolves the next time
  54. * the event is triggered, instead of calling a listener callback.
  55. * @returns a Promise that resolves the next time given event is triggered,
  56. * or rejects if an error is emitted. (If you request the 'error' event,
  57. * returns a promise that resolves with the error).
  58. *
  59. * Example:
  60. *
  61. * const message = await stream.emitted('message') // rejects if the stream errors
  62. */
  63. emitted(event) {
  64. return new Promise((resolve, reject) => {
  65. // TODO: handle errors
  66. this.once(event, resolve);
  67. });
  68. }
  69. _emit(event, ...args) {
  70. const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event];
  71. if (listeners) {
  72. __classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event] = listeners.filter((l) => !l.once);
  73. listeners.forEach(({ listener }) => listener(...args));
  74. }
  75. }
  76. _hasListener(event) {
  77. const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f")[event];
  78. return listeners && listeners.length > 0;
  79. }
  80. }
  81. exports.EventEmitter = EventEmitter;
  82. _EventEmitter_listeners = new WeakMap();
  83. //# sourceMappingURL=EventEmitter.js.map