mscststs-EventBus

自用,eventbus库

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/373588/639557/mscststs-EventBus.js

  1. /*
  2. 以事件监听方式进行 部署,所有功能均需运行在eve接口之上。
  3. eve接口暴露了三个方法,使用on方法可以绑定一个事件和回调函数,index参数用于确认回调函数的优先级,index越大,越优先,相同的以先绑定的优先
  4. 使用off方法可以解绑一个事件,如果绑定时使用了index,则解绑时也需要使用index,否则无法匹配
  5. 使用emit可以触发一个事件,在回调函数中返回false将会阻止 index 较小的参数的继续回调
  6.  
  7. 例如:回调函数a和b同时绑定了“foo”,事件,a index为10,b index 为11,若b返回false,则 a 不会被回调
  8.  
  9.  
  10. */
  11.  
  12. var eve = new class{
  13. constructor(){
  14. this.handles={};
  15. }
  16. on(event,callback,index=0){
  17. if(!this.handles[event]){
  18. this.handles[event]=[];
  19. }
  20. this.handles[event].push({callback:callback,index:index});
  21. this.handles[event].sort((a,b)=>{return b.index-a.index});
  22. }
  23. emit(event,...data){
  24. if(this.handles[event]){
  25. for(let i of this.handles[event]){
  26. if(i.callback(...data)===false) break;
  27. }
  28. }
  29. }
  30. off(event,callback,index=0){
  31. if(this.handles[event]){
  32. let s = [];
  33. for(let i of this.handles[event]){
  34. if(i.callback!=callback && i.index != index){s.push(i)};
  35. }
  36. this.handles[event] = s;
  37. }
  38. }
  39. }();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址