UNPKG

@cloudroom/electron-rtcsdk

Version:

这是一款在线音视频SDK插件,您可以从README.md中获取演示demo

62 lines (56 loc) 1.28 kB
class Observer { constructor() { this.publisher = {}; } _on(event, func, type) { if (typeof event === "string" && typeof func === "function") { let pubs = this.publisher[event]; !pubs && (pubs = this.publisher[event] = []); pubs.push({ type, func }); } else { throw new Error(""); } } on(event, func) { this._on(event, func, "on"); } once(event, func) { this._on(event, func, "once"); } emit(event, ...payload) { const pubs = this.publisher[event]; if(pubs && pubs.length > 0){ pubs.forEach((subscriber) => { const { func, type } = subscriber; try { func(...payload); } catch (error) { console.error(error) } type === "once" && this.off(event, subscriber); }); }/* else { console.log(`${event} 未定义`) } */ } off(event, sub) { if (event === '*') { this.publisher = {}; } else { const pubs = this.publisher[event]; pubs && (this.publisher[event] = pubs.filter((subscriber) => subscriber.func !== sub)); } } } // export default Observer; // export const EventBus = (id) => { // if (!EventBus[id]) { // EventBus[id] = new Observer(); // } // return EventBus[id]; // }; module.exports = Observer;