From e066cfcaed1f52b01fb4aa35a58e07b6fa05d216 Mon Sep 17 00:00:00 2001 From: kuangthree Date: Sat, 4 Nov 2023 13:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Elib.handler=EF=BC=8C=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E4=B8=BA=E4=BA=8B=E4=BB=B6=E5=90=8D=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?handler=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/game/game.js b/game/game.js index 6bddd9375..bd155c090 100644 --- a/game/game.js +++ b/game/game.js @@ -400,6 +400,52 @@ return method; } }, + handler:{ + _handlers:{}, + getHandler:function(name,type){ + if(!type)type = this.getDefaultHandlerType(name); + if(!this._handlers[name])return null; + if(!this._handlers[name][type])return null; + return this._handlers[name][type]; + }, + ensureHandlerList:function(name,type){ + if(!type)type = this.getDefaultHandlerType(name); + if(!this._handlers[name])this._handlers[name] = {}; + if(!this._handlers[name][type])this._handlers[name][type] = []; + return this._handlers[name][type]; + }, + removeHandler:function(name,type,func){ + var list = this.ensureHandlerList(name,type); + list.remove(func); + if(list.length == 0){ + delete this._handlers[name][type]; + if(Object.keys(this._handlers[name]) == 0){ + delete this._handlers[name]; + } + } + }, + pushHandler:function(name,type){ + let functions = (typeof type == 'string' ? arguments.slice(2):arguments.slice(1)); + type = (typeof type == 'string'?type:this.getDefaultHandlerType(name)); + this.ensureHandlerList(name,type).addArray(functions); + }, + getDefaultHandlerType:(name)=>{ + return `on${name[0].toUpperCase()}${name.slice(1)}`; + }, + addHandlerToEvent:function(event){ + if(typeof event.name == 'string'){ + let handlerMap = this._handlers[event.name]; + if(handlerMap){ + Object.keys(handlerMap).forEach((key)=>{ + let list = handlerMap[key]; + if(list){ + list.forEach(handler=>event.pushHandler(key,handler)); + } + }); + } + } + } + }, objectURL:new Map(), hookmap:{}, imported:{}, @@ -30966,6 +31012,7 @@ const type=`onNext${name[0].toUpperCase()}${name.slice(1)}`; if(gameEvent.hasHandler(type)) this.pushHandler(...gameEvent.getHandler(type)); } + lib.handler.addHandlerToEvent(this); } this.step=0; this.finished=false;