mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
singleton NotificationCenter
This commit is contained in:
parent
b8e6a967ee
commit
55505106e4
1 changed files with 22 additions and 19 deletions
41
app/Game/Server/NotificationCenter.js
Executable file → Normal file
41
app/Game/Server/NotificationCenter.js
Executable file → Normal file
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue