babzcraig / javascript-design-patterns Goto Github PK
View Code? Open in Web Editor NEWA collection of examples of various design patterns written in Javascript
License: MIT License
A collection of examples of various design patterns written in Javascript
License: MIT License
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.