bus.js 424 B

12345678910111213141516
  1. export default class Bus {
  2. constructor() {
  3. this.listeners = new Map();
  4. }
  5. on(evtName, listener) {
  6. const target = this.listeners.get(evtName) || [];
  7. target.push(listener);
  8. this.listeners.set(evtName, target);
  9. }
  10. emit(evtName) {
  11. const listeners = this.listeners.get(evtName);
  12. if (listeners) {
  13. listeners.forEach((func) => func());
  14. }
  15. }
  16. }