events.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Events allow for applications to use the observer pattern, which
  3. * allows subscribing and publishing events, outside the normal
  4. * execution paths.
  5. *
  6. * @_section api/utils/events:Events [about-events]
  7. */
  8. import { defineProperties } from "./properties.js";
  9. /**
  10. * When an [[EventEmitterable]] triggers a [[Listener]], the
  11. * callback always ahas one additional argument passed, which is
  12. * an **EventPayload**.
  13. */
  14. export class EventPayload {
  15. /**
  16. * The event filter.
  17. */
  18. filter;
  19. /**
  20. * The **EventEmitterable**.
  21. */
  22. emitter;
  23. #listener;
  24. /**
  25. * Create a new **EventPayload** for %%emitter%% with
  26. * the %%listener%% and for %%filter%%.
  27. */
  28. constructor(emitter, listener, filter) {
  29. this.#listener = listener;
  30. defineProperties(this, { emitter, filter });
  31. }
  32. /**
  33. * Unregister the triggered listener for future events.
  34. */
  35. async removeListener() {
  36. if (this.#listener == null) {
  37. return;
  38. }
  39. await this.emitter.off(this.filter, this.#listener);
  40. }
  41. }
  42. //# sourceMappingURL=events.js.map