mirror of
https://github.com/exane/not-gwent-online
synced 2025-08-10 04:57:29 +00:00
add simple pubsub test
This commit is contained in:
80
test/src/Battle.js
Normal file
80
test/src/Battle.js
Normal file
@@ -0,0 +1,80 @@
|
||||
var Battle = (function(){
|
||||
var Battle = function(){
|
||||
if(!(this instanceof Battle)){
|
||||
return (new Battle());
|
||||
}
|
||||
/**
|
||||
* constructor here
|
||||
*/
|
||||
|
||||
this.events = {};
|
||||
|
||||
|
||||
};
|
||||
var r = Battle.prototype;
|
||||
/**
|
||||
* methods && properties here
|
||||
* r.property = null;
|
||||
* r.getProperty = function() {...}
|
||||
*/
|
||||
|
||||
r.events = null;
|
||||
|
||||
r.runEvent = function(eventid, ctx, args){
|
||||
ctx = ctx || this;
|
||||
args = args || [];
|
||||
var event = "on" + eventid;
|
||||
|
||||
if(!this.events[event]){
|
||||
return;
|
||||
}
|
||||
this.events[event].forEach(function(e){
|
||||
var obj = e;
|
||||
obj.cb = obj.cb.bind(ctx)
|
||||
obj.cb.apply(ctx, obj.onArgs.concat(args));
|
||||
});
|
||||
}
|
||||
|
||||
r.on = function(eventid, cb, ctx, args){
|
||||
ctx = ctx || null;
|
||||
args = args || [];
|
||||
var event = "on" + eventid;
|
||||
|
||||
var obj = {};
|
||||
if(!ctx) {
|
||||
obj.cb = cb;
|
||||
} else {
|
||||
obj.cb = cb.bind(ctx);
|
||||
}
|
||||
obj.onArgs = args;
|
||||
|
||||
if(!(event in this.events)){
|
||||
this.events[event] = [];
|
||||
}
|
||||
|
||||
if(typeof cb !== "function"){
|
||||
throw new Error("cb not a function");
|
||||
}
|
||||
|
||||
if(args){
|
||||
this.events[event].push(obj);
|
||||
}
|
||||
|
||||
else {
|
||||
this.events[event].push(obj);
|
||||
}
|
||||
}
|
||||
|
||||
r.off = function(eventid){
|
||||
var event = "on" + eventid;
|
||||
this.events[event].forEach(function(e){
|
||||
e = null;
|
||||
});
|
||||
delete this.events[event];
|
||||
}
|
||||
|
||||
|
||||
return Battle;
|
||||
})();
|
||||
|
||||
module.exports = Battle;
|
Reference in New Issue
Block a user