Publish-subscribe is a messaging pattern where publishers send messages to a message broker, which then delivers the message to subscribed consumers. This pattern allows for decoupling between the publisher and consumer, as the publisher does not nee...

Markdown supported · ⌘↵ to post
sokaku
This was quite helpful when I was looking for a sub-pub model in vanilla ES6. However, it led to a bit of confusion when I expanded on it due to it using an array of arrays internally with a named index. It works, but "topic 1" & "topic 2" subscriptions are not elements of the subscription array, they are stored as properties on the array instance. If you access subscriptions.length you get 0, not 2. Subscribers should instead be a Map() with get() & set() with the subscription name. While Array() does work and handles access through the [] accessor, this is pretty misleading as the callbacks aren't elements of the array, but rather separate named properties on the instance.