EventSystem.js 462 B

12345678910111213141516171819202122232425262728293031
  1. export default class EventSystem {
  2. constructor(){
  3. this.callbacks = {};
  4. }
  5. on(e,cb){
  6. if(!this.callbacks[e]){
  7. this.callbacks[e] = [];
  8. }
  9. this.callbacks[e].push(cb)
  10. return this;
  11. }
  12. off(e,cb){
  13. let index = this.callbacks[e].indexOf(cb)
  14. if( index> -1) {
  15. this.callbacks[e].splice(index,1);
  16. }
  17. return this;
  18. }
  19. emit(eventName,data){
  20. this.callbacks[eventName] && this.callbacks[eventName].forEach((cb) => cb(data));
  21. return this;
  22. }
  23. }