mitt.ts 502 B

123456789101112131415161718192021222324
  1. class EventBus {
  2. emits: Map<string, Function> = new Map<string, Function>();
  3. on(name: string, callback: Function) {
  4. this.emits.set(name, callback);
  5. };
  6. off(name: string) {
  7. this.emits.delete(name);
  8. };
  9. exists(name: string) {
  10. return this.emits.has(name);
  11. }
  12. emit(name: string, ...arg: any[]) {
  13. if (this.emits.has(name)) {
  14. let cb = this.emits.get(name)
  15. return cb!(...arg)
  16. }
  17. };
  18. }
  19. export default EventBus