forked from ondras/my-mind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.js
42 lines (37 loc) · 1.12 KB
/
mm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
if (!Function.prototype.bind) {
Function.prototype.bind = function(thisObj) {
var fn = this;
var args = Array.prototype.slice.call(arguments, 1);
return function() {
return fn.apply(thisObj, args.concat(Array.prototype.slice.call(arguments)));
}
}
};
var MM = {
_subscribers: {},
publish: function(message, publisher, data) {
var subscribers = this._subscribers[message] || [];
subscribers.forEach(function(subscriber) {
subscriber.handleMessage(message, publisher, data);
});
},
subscribe: function(message, subscriber) {
if (!(message in this._subscribers)) {
this._subscribers[message] = [];
}
var index = this._subscribers[message].indexOf(subscriber);
if (index == -1) { this._subscribers[message].push(subscriber); }
},
unsubscribe: function(message, subscriber) {
var index = this._subscribers[message].indexOf(subscriber);
if (index > -1) { this._subscribers[message].splice(index, 1); }
},
generateId: function() {
var str = "";
for (var i=0;i<8;i++) {
var code = Math.floor(Math.random()*26);
str += String.fromCharCode("a".charCodeAt(0) + code);
}
return str;
}
};