1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /** Concept each channel can have child channels ??**/
- var EventualChannel = require('./eventual-channel');
- class EventualContext2 extends EventualChannel {
- constructor(eventual) {
- super(eventual);
- }
- broadcast(name,...args) {
- // Special send to all eventual contexts
- var tocall = [];
- this.eventual._events.ctx.forEach((c) => {
- tocall.push(...c.search(name));
- });
- return this.process(tocall,args)
- }
- state(name,...args) {
- this.eventual.state(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);
- }
- toString() {
- var out = [];
- this.eventual._events.ctx.forEach((c) => {
- var cur = c;
- // First level
- function listChild(cur,prefix) {
- for( var c in cur.childs) {
- var name = (prefix)?prefix + ":" + c:c;
- out.push(name);
- listChild(cur.childs[c], name);
- }
- }
- listChild(cur,"");
- });
- return out.join("\n");
- }
- }
- /** Old and deprecated **/
- 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 = EventualContext2;
|