mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
implemented a basic NotificationCenter
This commit is contained in:
parent
c7898319c3
commit
fe7205648d
2 changed files with 51 additions and 3 deletions
|
|
@ -1,10 +1,12 @@
|
|||
define(["Chuck/ServerGame"], function(ServerGame) {
|
||||
define(["Chuck/ServerGame", "Server/Factory"], function(ServerGame, Factory) {
|
||||
|
||||
function Channel(name) {
|
||||
this.factory = new Factory();
|
||||
|
||||
this.name = name;
|
||||
this.users = {};
|
||||
this.serverGame = new ServerGame(this);
|
||||
this.serverGame.loadLevel("default.json")
|
||||
this.serverGame = this.factory.new(ServerGame, this);
|
||||
this.serverGame.loadLevel("default.json");
|
||||
}
|
||||
|
||||
Channel.validateName = function(name){
|
||||
|
|
|
|||
|
|
@ -1,7 +1,53 @@
|
|||
define(function() {
|
||||
|
||||
function NotificationCenter() {
|
||||
this.topics = {};
|
||||
this.subUid = -1;
|
||||
}
|
||||
|
||||
NotificationCenter.prototype.trigger = function(topic, args) {
|
||||
if (!this.topics[topic]) {
|
||||
throw "No such topic " + topic + ". Could not trigger.";
|
||||
}
|
||||
|
||||
var subscribers = this.topics[topic];
|
||||
var len = subscribers ? subscribers.length : 0;
|
||||
|
||||
while (len--) {
|
||||
subscribers[len].func(topic, args);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
NotificationCenter.prototype.on = function(topic, func) {
|
||||
if (!this.topics[topic]) {
|
||||
this.topics[topic] = [];
|
||||
}
|
||||
|
||||
var token = ( ++this.subUid ).toString();
|
||||
this.topics[topic].push({
|
||||
token: token,
|
||||
func: func
|
||||
});
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
NotificationCenter.prototype.off = function(token) {
|
||||
|
||||
for(var m in this.topics) {
|
||||
if (this.topics[m]) {
|
||||
for(var i = 0, j = this.topics[m].length; i < j; i++) {
|
||||
if (this.topics[m][i].token === token) {
|
||||
this.topics[m].splice(i, 1);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
return NotificationCenter;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue