EventEmitter.mjs 3.3 KB

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