Giter Club home page Giter Club logo

javascript-design-patterns's People

Contributors

babzcraig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javascript-design-patterns's Issues

Question on The Publisher/Subscriber Pattern

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.