/** Concept each channel can have child channels ??**/ const chs = ':'; class EventualChannel { constructor(name,parent) { this.name = name; this.parent = parent; this.callbacks = [], // Local callbacks this.childs = {} this.listeners = []; } // createChannel based on name // getChannel(name,create) { // Should return listeners to mess up var [child, ...sub]= name.split(chs); if(this.childs[child] == undefined) { if(create==undefined) return []; // Maybe empty? this.childs[child] = new EventualChannel(this); } if(sub.length != 0) { // We could do this without recursion return this.childs[child].getChannel(sub.join(chs),create); } return this.childs[child].listeners; } getChannel2(name,create) { var nodes = name.split(chs); var cur = this; nodes.forEach((v) => { if( cur.childs[v] == undefined) { if(create == undefined) return []; // Empty cur.childs[v] = new EventualChannel(name,this); } cur = cur.childs[v]; // next; }); return cur.listeners; } search(name) { return this.getChannel2(name); } // Registers on(name,cb) { var listeners = this.getChannel2(name,true); listeners.push(cb); } emit(name,args) { var listeners = this.search(name); listeners.forEach((l) => { l(args); }); // Something to process callback as the xevents } } class EventualContext2 extends EventualChannel { constructor(eventual) { super(); this.eventual = eventual; } broadcast(name,...args) { this.eventual._events.ctx.forEach((c) => { c.emit(name,...args); }); } destroy() { this.childs = null; this.listeners = null; var i = this.eventual._events.ctx.indexOf(this); if(i!=-1) this.eventual._events.ctx.splice(i,1); } } class EventualContext extends EventualChannel { constructor(eventual) { super(); this.eventual = eventual; // Contextual listeners, it will invalidate regexp? this.listeners = []; } process(tocall,args,donecb) { tocall.forEach((l) => { l.callback(...args); }); } broadcast(name,...args) { this.eventual._events.ctx.forEach((c) => { c.emit(name,...args); }); } /** flat model **/ on(name,cb) { // Any listener will be placed here this.listeners.push({ name: name, callback:cb }); return this; } emit(name,...args) { // We need to go trought all contexts in Eventual var tocall = []; this.eventual._events.ctx.forEach((c) => { c.listeners.forEach((l) => { if(l.name == name) { tocall.push(l); } }) }); this.process(tocall,name,args); }/**/ destroy() { var i = this.eventual._events.ctx.indexOf(this); if(i!=-1) this.eventual._events.ctx.splice(i,1); this.listeners = null; } } module.exports = EventualContext;