Skip to content

Instantly share code, notes, and snippets.

@brandonweis
Last active May 30, 2022 15:58
Show Gist options
  • Save brandonweis/0d802a174bb107daab3f10475310dabf to your computer and use it in GitHub Desktop.
Save brandonweis/0d802a174bb107daab3f10475310dabf to your computer and use it in GitHub Desktop.
/*
You have 2 different applications running in the webpage, one with the cart count and another is a list of products. When you add click on "Add to cart" in the project, the cart count needs to get updated.
Implement a eventBus / pub/Sub solution to allow communication between the two applications.
Write tests to cover common use cases.
*/
const EventBus = () => {
const listeners = {}
const subscribe = (eventName, callback) => {
if (typeof callback !== 'function') throw Error()
if (!(eventName in listeners)) {
listeners[eventName] = []
}
listeners[eventName].push(callback)
return () => {
unsubscribe(eventName, callback)
}
}
const once = (eventName, callback) => {
const callbackWrapper = (...args) => {
unsubscribe(eventName, callbackWrapper)
callback(...args)
}
return subscribe(eventName, callbackWrapper)
}
const publish = (eventName, ...args) => {
if (!(eventName in listeners)) return
listeners[eventName].forEach(cb => cb(...args))
}
const unsubscribe = (eventName, callback) => {
listeners[eventName] = listeners[eventName].filter(cb => callback !== cb)
}
return {
subscribe,
once,
publish,
}
}
const eventbus = EventBus()
const unsubscribe = eventbus.once('add-to-cart', (product, timestamp) => console.log(product, timestamp))
eventbus.publish('add-to-cart', {id:1}, 123123)
unsubscribe()
eventbus.publish('add-to-cart', {id:1}, 123123)
const pubsub = EventBus();
const fn1 = (data) => console.log("subscribe 2", data);
pubsub.subscribe("eventA", (data) => console.log("subscribe 1", data));
pubsub.subscribe("eventA", fn1);
pubsub.publish("eventA", {details: {}});
pubsub.unsubscribe("eventA", fn1)
pubsub.publish("eventA", {details: {}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment