refactor: modify `lib.announce` to class instance.

This commit is contained in:
Rintim 2023-11-15 22:43:18 +08:00
parent 72e6f7e076
commit a41ccbdc1a
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
1 changed files with 38 additions and 22 deletions

View File

@ -354,25 +354,39 @@
} }
}], }],
}, },
announce:{ /**
init(){ *
_status._announce=document.createElement("Announce"); */
_status._announce_cache=new Map(); announce:new class{
delete lib.announce.init; constructor(){
}, /**
//推送一个对象给所有监听了name的订阅者。 * @type {HTMLElement}
*/
this._announce=document.createElement("Announce");
/**
* @type {Map<string, any>}
*/
this._announce_cache=new Map();
}
/**
* 推送一个对象给所有监听了name的订阅者
*/
publish(name,values){ publish(name,values){
if(_status._announce) _status._announce.dispatchEvent(new CustomEvent(name,{ if(this._announce) this._announce.dispatchEvent(new CustomEvent(name,{
detail:values detail:values
})); }));
return values; return values;
}, }
//订阅name相关的事件。
/**
* 订阅name相关的事件
*/
subscribe(name,method){ subscribe(name,method){
if(_status._announce&&_status._announce_cache) { if(this._announce&&this._announce_cache) {
let subscribeFunction; let subscribeFunction;
if(_status._announce_cache.has(method)){ if(this._announce_cache.has(method)){
let records=_status._announce_cache.get(method); let records=this._announce_cache.get(method);
subscribeFunction=records.get("Listener"); subscribeFunction=records.get("Listener");
records.get("EventTargets").add(name); records.get("EventTargets").add(name);
} }
@ -381,21 +395,24 @@
let records=new Map(); let records=new Map();
records.set("Listener",subscribeFunction); records.set("Listener",subscribeFunction);
records.set("EventTargets",[name]); records.set("EventTargets",[name]);
_status._announce_cache.set(method,records); this._announce_cache.set(method,records);
} }
_status._announce.addEventListener(name,subscribeFunction); this._announce.addEventListener(name,subscribeFunction);
} }
return method; return method;
}, }
//取消对事件name的订阅
/**
* 取消对事件name的订阅
*/
unsubscribe(name,method){ unsubscribe(name,method){
if(_status._announce&&_status._announce_cache&&_status._announce_cache.has(method)){ if(this._announce&&this._announce_cache&&this._announce_cache.has(method)){
let records=_status._announce_cache.get(method); let records=this._announce_cache.get(method);
const listener=records.get("Listener"); const listener=records.get("Listener");
let eventTargets=records.get("EventTargets"); let eventTargets=records.get("EventTargets");
eventTargets.remove(name); eventTargets.remove(name);
if(eventTargets.length<=0) _status._announce_cache.remove(method); if(eventTargets.length<=0) this._announce_cache.remove(method);
_status._announce.removeEventListener(name,listener); this._announce.removeEventListener(name,listener);
} }
return method; return method;
} }
@ -9566,7 +9583,6 @@
delete _status.htmlbg; delete _status.htmlbg;
window.game=game; window.game=game;
lib.announce.init();
// node:path library alternative // node:path library alternative
if (typeof module!="object"||typeof module.exports!="object") lib.init.js(`${lib.assetURL}game`,"path",()=>{ if (typeof module!="object"||typeof module.exports!="object") lib.init.js(`${lib.assetURL}game`,"path",()=>{
lib.path=window._noname_path; lib.path=window._noname_path;