Publish/Subscribe to notification after published
I am trying to understand the Observable/Observer design pattern.
This is the code I have so far (code from Javascript Patterns book):
var publisher = {
subscribers: {
any: [] // event type: subscribers
},
publications: {
any: []
},
subscribe: function (fn, type) {
type = type || 'any';
if (typeof this.subscribers[type] === "undefined") {
this.subscribers[type] = [];
}
this.subscribers[type].push(fn);
if(typeof this.publications[type] === "undefined"){
return;
}
var pubs = this.publications[type],
i,
max = pubs.length
for(i = 0;i<max;i++){
this.subscribers[type][i](pubs[i]);
}
},
unsubscribe: function (fn, type) {
this.visitSubscribers('unsubscribe', fn, type);
},
publish: function (publication, type) {
var pubtype = type || 'any';
this.visitSubscribers('publish', publication, type);
if(typeof this.publications[pubtype] === "undefined") {
this.publications[pubtype] = [];
}
this.publications[pubtype].push(publication);
},
visitSubscribers: function (action, arg, type) {
var pubtype = type || 'any',
subscribers = this.subscribers[pubtype],
i,
max;
if(typeof subscribers === 'undefined') {
return;
}
max = subscribers.length;
for (i = 0; i < max; i += 1) {
if (action === 'publish') {
subscribers[i](arg);
} else {
if (subscribers[i] === arg) {
subscribers.splice(i, 1);
开发者_高级运维 }
}
}
}
};
function makePublisher(o) {
var i;
for (i in publisher) {
if (publisher.hasOwnProperty(i) && typeof publisher[i] === "function") {
o[i] = publisher[i];
}
}
o.subscribers = {any: []};
o.publications = {any: []};
}
var paper = {
daily: function () {
this.publish("big news today");
},
monthly: function () {
this.publish("interesting analysis", "monthly");
},
yearly: function () {
this.publish("every year","yearly");
}
};
makePublisher(paper);
var joe = {
drinkCoffee: function (paper) {
console.log('Just read ' + paper);
},
sundayPreNap: function (monthly) {
console.log('About to fall asleep reading this ' + monthly);
},
onHolidays: function(yearly) {
console.log('Holidays!'+yearly);
}
};
paper.daily();
paper.monthly();
paper.yearly();
paper.subscribe(joe.drinkCoffee);
paper.subscribe(joe.onHolidays,'yearly');
paper.subscribe(joe.sundayPreNap, 'monthly');
I wonder if it is possible somehow to allow clients to receive the notifications even if they registered themselves after such notifications had been broadcast.
Should I modify the publisher.subscribe and make it check if undefined and if yes publish the event type?
Thanks in advance.
*EDIT 1 *
I've added a publications object to save the publications in the publish function. I also check if there are subscribers for the publication type and if not I call return. Now I need to figure out how to notify them for older publication on subscribe.
*EDIT 2 *
New version of a working script added. Is it thought correct or the best way to do it?
If you want to allow subscribers to get the notifications after they are sent, what you need to do is just
For each different publication type, keep a list of all notifications you get for it. (When publishing, add the
publication
to the appropriate list)Change the subscribe function so that it also sends all the appropriate stored notifications to the late-arriving subscriber.
2.1 Perhaps you should also create a separate
send_notification_to(arg, type, subscriber)
method, to avoid code duplicatio withvisitSubscribers
精彩评论