EventEmitter.js 3.4 KB

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