EventEmitter.mjs 3.7 KB

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