Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

singleton NotificationCenter

  • Loading branch information...
commit 55505106e43367b265089603b21d2c11e5cd44e0 1 parent b8e6a96
@jeena jeena authored
Showing with 22 additions and 19 deletions.
  1. +22 −19 app/Game/Server/NotificationCenter.js
View
41 app/Game/Server/NotificationCenter.js 100755 → 100644
@@ -1,30 +1,32 @@
-define(
+define(function() {
- var NotificationCenter = {
- topics: {},
- subUid: -1
- };
+ function NotificationCenter() {
+ this.topics = {};
+ this.subUid = -1;
+ }
- NotificationCenter.trigger = function(topic, args) {
- if (!NotificationCenter.topics[topic]) {
+ NotificationCenter.prototype.trigger = function(topic, args) {
+ if (!this.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger.";
}
- var subscribers = NotificationCenter.topics[topic];
+ var subscribers = this.topics[topic];
var len = subscribers ? subscribers.length : 0;
while (len--) {
subscribers[len].func(topic, args);
}
+
+ return this;
}
- NotificationCenter.on = function(topic, func) {
- if (!NotificationCenter.topics[topic]) {
- NotificationCenter.topics[topic] = [];
+ NotificationCenter.prototype.on = function(topic, func) {
+ if (!this.topics[topic]) {
+ this.topics[topic] = [];
}
- var token = ( ++NotificationCenter.subUid ).toString();
- NotificationCenter.topics[topic].push({
+ var token = ( ++this.subUid ).toString();
+ this.topics[topic].push({
token: token,
func: func
});
@@ -32,18 +34,19 @@ define(
return token;
}
- NotificationCenter.off = function(token) {
+ NotificationCenter.prototype.off = function(token) {
- for(var m in NotificationCenter.topics) {
- if (NotificationCenter.topics[m]) {
- for(var i = 0, j = NotificationCenter.topics[m].length; i < j; i++) {
- if (NotificationCenter.topics[m][i].token === token) {
- NotificationCenter.topics[m].splice(i, 1);
+ 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;
Please sign in to comment.
Something went wrong with that request. Please try again.