event.ts 410 B

1234567891011121314151617181920
  1. export class Event {
  2. private listeners: Function[] = []
  3. addListener (callback: Function) {
  4. this.listeners.push(callback);
  5. }
  6. removeListener (callback: Function) {
  7. const index = this.listeners.indexOf(callback);
  8. if (index !== -1) {
  9. this.listeners.splice(index, 1);
  10. }
  11. }
  12. emit (...args: any[]) {
  13. for (const listener of this.listeners) {
  14. listener(...args);
  15. }
  16. }
  17. }