|
@@ -8,7 +8,7 @@ class XPrefix {
|
|
|
this.prefix = prefix || "";
|
|
|
}
|
|
|
on(name,cb) {
|
|
|
- this.event.on(this.ctx,this.prefix + ":" + name,cb);
|
|
|
+ this.event.on(this.ctx,this.prefix + ":" + name, cb);
|
|
|
return this;
|
|
|
}
|
|
|
}
|
|
@@ -47,6 +47,7 @@ class XEventEmitter {
|
|
|
|
|
|
//this.listeners = new Map();
|
|
|
this.listeners = [];
|
|
|
+
|
|
|
this.auto = {};
|
|
|
this.stat = {
|
|
|
emitted:0,
|
|
@@ -62,27 +63,7 @@ class XEventEmitter {
|
|
|
} else {
|
|
|
[ctx,evtName,cb,order] = args;
|
|
|
}
|
|
|
-
|
|
|
- // Manipulate the evtName as in:
|
|
|
- //
|
|
|
-
|
|
|
- /*var re = /[^\\]{(.*?)}/;
|
|
|
- var paramMap = [];
|
|
|
- do {
|
|
|
- var res = evtName.match(re)
|
|
|
- if(res != null) {
|
|
|
- paramMap.push(res[1]);
|
|
|
- // The match + first char of the match
|
|
|
- evtName = evtName.replace(re,res[0][0]+"(.*)");
|
|
|
- }
|
|
|
- }while(res != null);
|
|
|
- console.log("Resulting evtName: " + evtName);*/
|
|
|
-
|
|
|
var listener = new XEventListener(ctx,evtName,cb,order);
|
|
|
- //listener.paramMap = paramMap;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
this.listeners.push(listener);
|
|
|
//this.listeners.push(entry);
|
|
|
|
|
@@ -91,7 +72,8 @@ class XEventEmitter {
|
|
|
Object.keys(this.auto).map((v) => {
|
|
|
var match = v.match(re);
|
|
|
if(v.match(re)) {
|
|
|
- this.process([{match:match,listener:listener}],this.auto[v]);
|
|
|
+ var tocall = [{match:match,listener:listener}];
|
|
|
+ this.process(tocall,this.auto[v]); // No callback
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -114,18 +96,21 @@ class XEventEmitter {
|
|
|
}
|
|
|
|
|
|
// 1 arg only
|
|
|
- toggleOn(name,arg) {
|
|
|
- this.auto[name]= arg;
|
|
|
- this.emit(name,arg);
|
|
|
+ toggleOn(name,...args) {
|
|
|
+ // Should be contextual if we remove context toggle will remain
|
|
|
+ this.auto[name] = args; // should store array
|
|
|
+ this.emit(name,...args);
|
|
|
}
|
|
|
|
|
|
toggleOff(name) {
|
|
|
delete this.auto[name];
|
|
|
}
|
|
|
|
|
|
- process(tocall,args,cb) {
|
|
|
+ process(tocall,nargs,cb) {
|
|
|
var self=this;
|
|
|
-
|
|
|
+
|
|
|
+ var args = nargs || []; // or empty
|
|
|
+ // Common for all for now
|
|
|
var evt = new XEvent(next,args);
|
|
|
|
|
|
this.stat.emitted++;
|
|
@@ -145,7 +130,7 @@ class XEventEmitter {
|
|
|
// Stack overflow risk? maybe put this in process.tick();
|
|
|
var v = iter.next();
|
|
|
if(v.done) {
|
|
|
- if(cb != undefined) cb(evt);
|
|
|
+ if(cb != undefined) cb(evt); // Done callback with evt only
|
|
|
return;
|
|
|
}
|
|
|
// Create evt here with some global event chaining iteraction
|
|
@@ -154,7 +139,7 @@ class XEventEmitter {
|
|
|
evt.match = entry.match;
|
|
|
evt.args = args;
|
|
|
self.stat.called++;
|
|
|
- entry.listener.callback(args,evt);
|
|
|
+ entry.listener.callback(...args,evt);
|
|
|
evt.count++;
|
|
|
}catch(e) {
|
|
|
console.log(e);
|
|
@@ -171,6 +156,7 @@ class XEventEmitter {
|
|
|
var ret = [];
|
|
|
this.listeners.forEach((v) => {
|
|
|
var re = new RegExp( "^" + v.name+ "$" );
|
|
|
+ //var re = v.name;
|
|
|
var matchres = name.match(re);
|
|
|
if(matchres && (ctx == undefined || ctx == v.ctx) ) {
|
|
|
ret.push({
|
|
@@ -182,35 +168,33 @@ class XEventEmitter {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- emit(name,args) {
|
|
|
+ // Multiple argument
|
|
|
+ // Should work like emit("event",1,2,3,4,5);
|
|
|
+ emit(name,...args) {
|
|
|
this.stat.tried++;
|
|
|
var tocall = this.search(name);
|
|
|
+
|
|
|
+ var evt,callback,dotrigger = 0;
|
|
|
+ function trigger(ievt) {
|
|
|
+ evt = ievt; // Will call
|
|
|
+ if(callback)callback(evt);
|
|
|
+ }
|
|
|
// Mem leak???
|
|
|
var ret = {
|
|
|
- param: null,
|
|
|
- callback: null,
|
|
|
- // Setup callback
|
|
|
- trigger: function(args,evt) {
|
|
|
- this.param = [args,evt];
|
|
|
- this.dotrigger = 1;
|
|
|
- if(this.callback) {this.callback(args,evt);}
|
|
|
- },
|
|
|
done: function(cb) {
|
|
|
- if(this.dotrigger) {
|
|
|
- cb(...param);
|
|
|
- }
|
|
|
- this.callback = cb;
|
|
|
+ if(evt) { cb(evt); }
|
|
|
+ callback = cb;
|
|
|
}
|
|
|
}
|
|
|
// Test
|
|
|
- this.process(tocall,args,(evt) => { ret.trigger(args,evt) });
|
|
|
+ this.process(tocall,args, trigger );
|
|
|
return ret ;
|
|
|
}
|
|
|
// Why?
|
|
|
- emitTo(ctx,name,args) {
|
|
|
+ /*emitTo(ctx,name,args) {
|
|
|
var tocall = this.search(name,ctx)
|
|
|
this.process(tocall,arg,cb);
|
|
|
- }
|
|
|
+ }*/
|
|
|
// Alias
|
|
|
on(ctx,match,cb) {
|
|
|
this.addListener(ctx,match,cb);
|
|
@@ -220,7 +204,6 @@ class XEventEmitter {
|
|
|
this.addListener(ctx,match,cb,1000);
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
prefix(ctx,prefix) {
|
|
|
return new XPrefix(ctx,this,prefix);
|
|
|
}
|