From 6d90b838407a68831f811aea17d11bbfde13466f Mon Sep 17 00:00:00 2001 From: Rintim Date: Thu, 5 Oct 2023 19:43:41 +0800 Subject: [PATCH 1/3] modify announce patch. --- game/game.js | 63 +++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/game/game.js b/game/game.js index 3c5b5f585..419f3b901 100644 --- a/game/game.js +++ b/game/game.js @@ -337,47 +337,33 @@ }], }, announce:{ - //推送一个对象给所有监听了key的订阅者。 - publish:function(key,obj){ - if(!_status.announce)return; - if(!_status.announce[key])return; - for(let subscriber of _status.announce[key]){ - if(subscriber.onReceive){ - subscriber.onReceive(key,obj); - } - } + init(){ + _status._announce=document.createElement("Announce"); + _status._announce_cache=new Map(); + delete lib.announce.init; }, - //订阅key相关的事件。 - subscribe:function(key,subscriber){ - if(typeof subscriber === 'function'){ - let subs = { - onReceive:subscriber, - priority:0, - }; - subscriber = subs; - } - if(!_status.announce)_status.announce = {}; - if(!Array.isArray(_status.announce[key]))_status.announce[key] = []; - var inserted = false; - for(let i=0;i<_status.announce[key].length;i++){ - let pri = _status.announce[key][i].priority; - if(pri <= subscriber.priority){ - _status.announce[key].splice(i,0,subscriber); - inserted = true; - break; - } - } - if(!inserted){ - _status.announce[key].push(subscriber); - } - return subscriber; + //推送一个对象给所有监听了name的订阅者。 + publish(name,values){ + if(_status._announce) _status._announce.dispatchEvent(new CustomEvent(name,{ + detail:values + })); + return values; }, - //取消对事件key的订阅,subscriber需要为上面lib.announce.subscribe返回的值。 - unsubscribe:function(key,subscriber){ - if(!_status.announce)return; - if(!_status.announce[key])return; - _status.announce[key].remove(subscriber); + //订阅name相关的事件。 + subscribe(name,method){ + if(_status._announce&&_status._announce_cache) { + const subscribeFunction=event=>method(event.detail); + _status._announce_cache.set(method,subscribeFunction); + _status._announce.addEventListener(name,subscribeFunction); + } + return method; }, + //取消对事件name的订阅 + unsubscribe(name,method){ + if(_status._announce&&_status._announce_cache&&_status._announce_cache.has(method)) + _status._announce.removeEventListener(name,_status._announce_cache.get(method)); + return method; + } }, objectURL:new Map(), hookmap:{}, @@ -9183,6 +9169,7 @@ window.game=game; game.dynamicStyle.init(); + lib.announce.init(); // node:path library alternative if (typeof module!="object"||typeof module.exports!="object") lib.init.js(`${lib.assetURL}game`,"path.min",()=>{ lib.path=window._noname_path; From 608f396d71df5acee43fdc936c9f4e37056cf229 Mon Sep 17 00:00:00 2001 From: Rintim Date: Thu, 5 Oct 2023 19:58:41 +0800 Subject: [PATCH 2/3] add announce cache. --- game/game.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/game/game.js b/game/game.js index 419f3b901..276342d2b 100644 --- a/game/game.js +++ b/game/game.js @@ -339,7 +339,7 @@ announce:{ init(){ _status._announce=document.createElement("Announce"); - _status._announce_cache=new Map(); + _status._announce_cache=new WeakMap(); delete lib.announce.init; }, //推送一个对象给所有监听了name的订阅者。 @@ -350,10 +350,19 @@ return values; }, //订阅name相关的事件。 - subscribe(name,method){ + subscribe(name,method,once){ if(_status._announce&&_status._announce_cache) { - const subscribeFunction=event=>method(event.detail); - _status._announce_cache.set(method,subscribeFunction); + let subscribeFunction; + if(_status._announce_cache.has(method)){ + subscribeFunction=_status._announce_cache.get(method); + } + else{ + subscribeFunction=event=>{ + method(event.detail); + if(once) _status._announce.removeEventListener(subscribeFunction); + }; + _status._announce_cache.set(method,subscribeFunction); + } _status._announce.addEventListener(name,subscribeFunction); } return method; From e50f757d775823857a3a21b5328dd67275b4fed7 Mon Sep 17 00:00:00 2001 From: Rintim Date: Thu, 5 Oct 2023 20:10:06 +0800 Subject: [PATCH 3/3] fix announce cache memory collection. --- game/game.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/game/game.js b/game/game.js index 276342d2b..a93c94f19 100644 --- a/game/game.js +++ b/game/game.js @@ -339,7 +339,7 @@ announce:{ init(){ _status._announce=document.createElement("Announce"); - _status._announce_cache=new WeakMap(); + _status._announce_cache=new Map(); delete lib.announce.init; }, //推送一个对象给所有监听了name的订阅者。 @@ -350,18 +350,20 @@ return values; }, //订阅name相关的事件。 - subscribe(name,method,once){ + subscribe(name,method){ if(_status._announce&&_status._announce_cache) { let subscribeFunction; if(_status._announce_cache.has(method)){ - subscribeFunction=_status._announce_cache.get(method); + let records=_status._announce_cache.get(method); + subscribeFunction=records.get("Listener"); + records.get("EventTargets").add(name); } else{ - subscribeFunction=event=>{ - method(event.detail); - if(once) _status._announce.removeEventListener(subscribeFunction); - }; - _status._announce_cache.set(method,subscribeFunction); + subscribeFunction=event=>method(event.detail); + let records=new Map(); + records.set("Listener",subscribeFunction); + records.set("EventTargets",[name]); + _status._announce_cache.set(method,records); } _status._announce.addEventListener(name,subscribeFunction); } @@ -369,8 +371,14 @@ }, //取消对事件name的订阅 unsubscribe(name,method){ - if(_status._announce&&_status._announce_cache&&_status._announce_cache.has(method)) - _status._announce.removeEventListener(name,_status._announce_cache.get(method)); + if(_status._announce&&_status._announce_cache&&_status._announce_cache.has(method)){ + let records=_status._announce_cache.get(method); + const listener=records.get("Listener"); + let eventTargets=records.get("EventTargets"); + eventTargets.remove(name); + if(eventTargets.length<=0) _status._announce_cache.remove(method); + _status._announce.removeEventListener(name,listener); + } return method; } },