eventMgr.js 824 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module.exports = {
  2. eventList:{},
  3. //
  4. sendEvent:function (name,...args){
  5. let events = this.eventList[name];
  6. if(!events){
  7. return
  8. }
  9. for (let i = events.length-1; i >= 0; i--) {
  10. const func = events[i];
  11. try {
  12. func(...args)
  13. } catch (error) {
  14. Editor.error(error);
  15. }
  16. }
  17. },
  18. listenEvent:function (name,func){
  19. let events = this.eventList[name] = this.eventList[name] || [];
  20. events.push(func);
  21. return func
  22. },
  23. removeEvent:function (func){
  24. for (const key in this.eventList) {
  25. const events = this.eventList[key];
  26. for (let i = 0; i < events.length; i++) {
  27. const func_ = events[i];
  28. if(func_ == func){
  29. events.splice(i,1);
  30. return true
  31. }
  32. }
  33. }
  34. },
  35. removeAllEvent:function (){
  36. this.eventList = {}
  37. },
  38. merge: function(obj){
  39. Object.assign(obj,this)
  40. },
  41. }