EventDispatcher.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import IEventDispatcher from "./IEventDispatcher";
  2. /**
  3. * 事件分发器
  4. */
  5. export class EventDispatcher implements IEventDispatcher {
  6. /**
  7. * 对象已经注册的处理器
  8. */
  9. private callerMap:Map<any,EventHandler[]>=new Map<any,EventHandler[]>();
  10. /**
  11. * 事件派发器上所监听的处理器
  12. */
  13. private keyMap:Map<string,EventHandler[]>=new Map<string,EventHandler[]>();
  14. /**
  15. * 添加事件
  16. * @param key
  17. * @param caller
  18. * @param func
  19. * @param priority 优先级(数字越小优先级越高)
  20. */
  21. AddEvent(key:string,caller:any,func:Function,priority:number=0):void{
  22. let handlerList:EventHandler[];
  23. let handler:EventHandler;
  24. if(this.keyMap.has(key)){
  25. handlerList=this.keyMap.get(key);
  26. for (const iterator of handlerList) {
  27. if(iterator.target==caller&&iterator.func==func){
  28. console.error("重复添加同一个事件监听:"+key+" "+caller+" "+func);
  29. return;
  30. }
  31. }
  32. }else{
  33. handlerList=[];
  34. this.keyMap.set(key,handlerList);
  35. }
  36. handler=new EventHandler(key,caller,func);
  37. handlerList.push(handler);
  38. //按照优先级排序
  39. handlerList.sort((a,b)=>a.priority - priority);
  40. //处理器关联处理
  41. if(this.callerMap.has(caller)){
  42. handlerList=this.callerMap.get(caller);
  43. for (const iterator of handlerList) {
  44. if(iterator.key==key&&iterator.func==func)
  45. {
  46. console.error("事件系统 处理器关联错误:"+key+" "+caller+" "+func);
  47. }
  48. }
  49. }else{
  50. handlerList=[];
  51. this.callerMap.set(caller,handlerList);
  52. }
  53. handlerList.push(handler);
  54. }
  55. /**
  56. * 删除事件监听
  57. * @param key
  58. * @param caller
  59. * @param func
  60. */
  61. RemoveEvent(key:string,caller:any,func:Function):void{
  62. if(this.keyMap.has(key)==false){
  63. return;
  64. }
  65. let handlerList:EventHandler[]=this.keyMap.get(key);
  66. let handler:EventHandler;
  67. let deleteHandler:EventHandler;
  68. //删除
  69. for (let index = 0; index < handlerList.length; index++) {
  70. handler = handlerList[index];
  71. if(handler.target==caller&&handler.func==func){
  72. deleteHandler=handler;
  73. handlerList.splice(index,1);
  74. break;
  75. }
  76. }
  77. handlerList=this.callerMap.get(caller);
  78. //删除
  79. for (let index = 0; index < handlerList.length; index++) {
  80. handler = handlerList[index];
  81. if(handler.key==key&&handler.func==func){
  82. deleteHandler=handler;
  83. handlerList.splice(index,1);
  84. break;
  85. }
  86. }
  87. //销毁处理器
  88. deleteHandler.Destroy();
  89. }
  90. /**
  91. * 删除指定对象所有的事件处理
  92. * @param caller
  93. */
  94. RemoveEventByCaller(caller:any):void{
  95. let handlerList:EventHandler[]=this.callerMap.get(caller);
  96. let handler:EventHandler;
  97. //逐个删除
  98. while (handlerList.length) {
  99. handler=handlerList[0];
  100. this.RemoveEvent(handler.key,handler.target,handler.func);
  101. }
  102. //删除空列表
  103. this.callerMap.delete(caller);
  104. }
  105. /**
  106. * 删除所有事件监听
  107. */
  108. RemoveAllEvent():void{
  109. this.keyMap.forEach(handlerList => {
  110. handlerList.forEach(handler => {
  111. handler.Destroy();
  112. });
  113. });
  114. this.keyMap.clear();
  115. this.callerMap.clear();
  116. }
  117. /**
  118. * 派发事件
  119. * @param key
  120. * @param data
  121. */
  122. DispatchEvent(key:string,data?:any):void{
  123. if(this.keyMap.has(key)==false){
  124. return;
  125. }
  126. let handlerList:EventHandler[]=this.keyMap.get(key);
  127. let handler:EventHandler;
  128. for (let index = 0; index < handlerList.length; index++) {
  129. handler = handlerList[index];
  130. handler.func.apply(handler.target,[data]);
  131. }
  132. }
  133. /**
  134. * 是否有事件监听
  135. * @param key
  136. */
  137. HasEvent(key:string):boolean{
  138. return this.keyMap.has(key);
  139. }
  140. /**
  141. * 是否包含指定函数事件监听
  142. * @param key
  143. * @param caller
  144. * @param func
  145. */
  146. HasEventHandler(key:string,caller:any,func:Function):boolean{
  147. if(this.keyMap.has(key)==false){
  148. return false;
  149. }
  150. let handlerList:EventHandler[]=this.keyMap.get(key);
  151. let handler:EventHandler;
  152. for (let index = 0; index < handlerList.length; index++) {
  153. handler = handlerList[index];
  154. if(handler.target==caller&&handler.func==func){
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. }
  161. class EventHandler{
  162. key:string;
  163. target:any;
  164. func:Function;
  165. priority:number;
  166. constructor(key:string,target:any,func:Function){
  167. this.key=key;
  168. this.target=target;
  169. this.func=func;
  170. }
  171. Destroy(){
  172. this.target=null;
  173. this.key=null;
  174. this.func=null;
  175. }
  176. }