Hello Babs,
Thank you for your explanation of Pub/Sub pattern on https://medium.com/@thebabscraig/javascript-design-patterns-part-2-the-publisher-subscriber-pattern-8fe07e157213.
I find your explanation is useful but I have a question about the unsubscribe()
At the pubsub module, the data structure of subscribers should be like this:
{
event1: [ callback1 ],
event2: [ callback2 ],
event3: [ callback3 ]
}
the code snippet:
subscribe(event, callback) {
let index;
if (!subscribers[event]) {
subscribers[event] = [];
}
index = subscribers[event].push(callback) - 1;
return {
unsubscribe() {
subscribers[event].splice(index, 1);
}
};
}
I think the subscribers[event].splice(index, 1) only remove the callback at the array which is subscribers[event] instead of the entry property of subscribers which is subscribers[event]
Maybe we can create a brand new unsubscribe() method to remove the entire
subscribers[event] property like:
unsubscribe (event) {
if (!subscribers[event]) return;
delete subscribers[event]
}
Also, I am confused with the usage of return statement at the subscribe.
I don't understand the variable environment or scope at :
const pubSub = require("./pubsub");
let subscription;
subscription = pubSub.subscribe("anEvent", data => {
console.log(
"anEvent", was published with this data: "${data.msg}"
);
subscription.unsubscribe();
});
pubSub.subscribe return an object with an unsubscribe() method and we store it at the variable called subscription/ but why it can immediately call this return value at the pubSub.subscribe and inside its callback method? I rarely read this pattern.
Thank you for your attention
Best,
Dennis
Hello Babs,
Thank you for your explanation of Pub/Sub pattern on https://medium.com/@thebabscraig/javascript-design-patterns-part-2-the-publisher-subscriber-pattern-8fe07e157213.
I find your explanation is useful but I have a question about the unsubscribe()
At the pubsub module, the data structure of subscribers should be like this:
{
event1: [ callback1 ],
event2: [ callback2 ],
event3: [ callback3 ]
}
the code snippet:
subscribe(event, callback) {
let index;
if (!subscribers[event]) {
subscribers[event] = [];
}
index = subscribers[event].push(callback) - 1;
}
I think the subscribers[event].splice(index, 1) only remove the callback at the array which is subscribers[event] instead of the entry property of subscribers which is subscribers[event]
Maybe we can create a brand new unsubscribe() method to remove the entire
subscribers[event] property like:
unsubscribe (event) {
if (!subscribers[event]) return;
delete subscribers[event]
}
Also, I am confused with the usage of return statement at the subscribe.
I don't understand the variable environment or scope at :
const pubSub = require("./pubsub");
let subscription;
subscription = pubSub.subscribe("anEvent", data => {
console.log(
"anEvent", was published with this data: "${data.msg}");
subscription.unsubscribe();
});
pubSub.subscribe return an object with an unsubscribe() method and we store it at the variable called subscription/ but why it can immediately call this return value at the pubSub.subscribe and inside its callback method? I rarely read this pattern.
Thank you for your attention
Best,
Dennis