Merge pull request #474 from nofficalfs/PR-Branch
[Enhancement] `lib.announce`机制改变
This commit is contained in:
commit
5ca8769a1e
80
game/game.js
80
game/game.js
|
@ -337,47 +337,50 @@
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
announce:{
|
announce:{
|
||||||
//推送一个对象给所有监听了key的订阅者。
|
init(){
|
||||||
publish:function(key,obj){
|
_status._announce=document.createElement("Announce");
|
||||||
if(!_status.announce)return;
|
_status._announce_cache=new Map();
|
||||||
if(!_status.announce[key])return;
|
delete lib.announce.init;
|
||||||
for(let subscriber of _status.announce[key]){
|
|
||||||
if(subscriber.onReceive){
|
|
||||||
subscriber.onReceive(key,obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
//订阅key相关的事件。
|
//推送一个对象给所有监听了name的订阅者。
|
||||||
subscribe:function(key,subscriber){
|
publish(name,values){
|
||||||
if(typeof subscriber === 'function'){
|
if(_status._announce) _status._announce.dispatchEvent(new CustomEvent(name,{
|
||||||
let subs = {
|
detail:values
|
||||||
onReceive:subscriber,
|
}));
|
||||||
priority:0,
|
return values;
|
||||||
};
|
|
||||||
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;
|
|
||||||
},
|
},
|
||||||
//取消对事件key的订阅,subscriber需要为上面lib.announce.subscribe返回的值。
|
//订阅name相关的事件。
|
||||||
unsubscribe:function(key,subscriber){
|
subscribe(name,method){
|
||||||
if(!_status.announce)return;
|
if(_status._announce&&_status._announce_cache) {
|
||||||
if(!_status.announce[key])return;
|
let subscribeFunction;
|
||||||
_status.announce[key].remove(subscriber);
|
if(_status._announce_cache.has(method)){
|
||||||
|
let records=_status._announce_cache.get(method);
|
||||||
|
subscribeFunction=records.get("Listener");
|
||||||
|
records.get("EventTargets").add(name);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return method;
|
||||||
},
|
},
|
||||||
|
//取消对事件name的订阅
|
||||||
|
unsubscribe(name,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;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
objectURL:new Map(),
|
objectURL:new Map(),
|
||||||
hookmap:{},
|
hookmap:{},
|
||||||
|
@ -9183,6 +9186,7 @@
|
||||||
|
|
||||||
window.game=game;
|
window.game=game;
|
||||||
game.dynamicStyle.init();
|
game.dynamicStyle.init();
|
||||||
|
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;
|
||||||
|
|
Loading…
Reference in New Issue