add announce cache.

This commit is contained in:
Rintim 2023-10-05 19:58:41 +08:00
parent 6d90b83840
commit 608f396d71
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
1 changed files with 13 additions and 4 deletions

View File

@ -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;