1 function Publisher(name) {
2 this.name = name;
3 this.subscribers = [];
4 }
5 Publisher.prototype.deliver = function (data, scope) {
6 scope = scope || window;
7
8 var self = this;
9
10 this.subscribers.forEach(
11 function (value) {
12 value.call(scope, self.name, data);
13 }
14 );
15
16 return this;
17 };
18 Function.prototype.subscribe = function (publisher) {
19 var self = this,
20 isExistent = publisher.subscribers.some(
21 function (value) {
22 return (value === self);
23 }
24 );
25
26 if (!isExistent) {
27 publisher.subscribers.push(this);
28 }
29
30 return this;
31 };
32 Function.prototype.unsubscribe = function (publisher) {
33 var self = this;
34
35 publisher.subscribers = publisher.subscribers.filter(
36 function(value) {
37 if (value !== self) {
38 return value;
39 }
40 }
41 );
42
43 return this;
44 };