EventEmitter.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright 2022 Google LLC.
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import { type EventType, type Handler, type WildcardHandler } from 'mitt';
  18. export declare class EventEmitter<Events extends Record<EventType, unknown>> {
  19. #private;
  20. /**
  21. * Binds an event listener to fire when an event occurs.
  22. * @param event The event type you'd like to listen to. Can be a string or symbol.
  23. * @param handler The function to be called when the event occurs.
  24. * @return `this` to enable chaining method calls.
  25. */
  26. on(type: '*', handler: WildcardHandler<Events>): this;
  27. on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): this;
  28. /**
  29. * Like `on` but the listener will only be fired once and then it will be removed.
  30. * @param event The event you'd like to listen to
  31. * @param handler The handler function to run when the event occurs
  32. * @return `this` to enable chaining method calls.
  33. */
  34. once(event: EventType, handler: Handler): this;
  35. /**
  36. * Removes an event listener from firing.
  37. * @param event The event type you'd like to stop listening to.
  38. * @param handler The function that should be removed.
  39. * @return `this` to enable chaining method calls.
  40. */
  41. off(type: '*', handler: WildcardHandler<Events>): this;
  42. off<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): EventEmitter<Events>;
  43. /**
  44. * Emits an event and call any associated listeners.
  45. *
  46. * @param event The event to emit.
  47. * @param eventData Any data to emit with the event.
  48. * @return `true` if there are any listeners, `false` otherwise.
  49. */
  50. emit<Key extends keyof Events>(event: Key, eventData: Events[Key]): void;
  51. }