// Asked in Coursera recently
// this._subscriptions = new Map();
// // Function to subscribe to an event
// subscribe(name, callback) {
// // Implement subscription logic
// // Function to subscribe to an event once
// subscribeOnce(name, callback) {
// // Implement one-time subscription logic
// // Function to subscribe to an event once asynchronously
// async subscribeOnceAsync(name) {
// // Implement async one-time subscription logic
// // Function to publish an event
// // Implement event publishing logic
// // Function to publish all events
// // Implement logic for publishing all events
// const events = new Events();
// const newUserNewsSubscription = events.subscribe("new-user",
// console.log(`Sending Q1 News to: ${payload}`);
// events.publish("new-user", "Jhon");
// console.log("--------");
// // Adding another subscription
// const newUserNewsSubscription2 = events.subscribe("new-user",
// console.log(`Sending Q2 News to: ${payload}`);
// events.publish("new-user", "Doe");
// console.log("--------");
// // Removing a subscription
// newUserNewsSubscription.remove();
// events.publish("new-user", "Foo");
// console.log("--------");
// events.publishAll("FooBar");
// console.log("--------");
// events.subscribeOnce("new-user", function (payload) {
// console.log(`I am invoked once ${payload}`);
// events.publish("new-user", "Foo Once");
// console.log("--------");
// events.publish("new-user", "Foo Twice");
// console.log("--------");
// // Subscribe once asynchronously
// events.subscribeOnceAsync("new-user").then(function (payload) {
// console.log(`I am invoked once asynchronously: ${payload}`);
// events.publish("new-user", "Foo Once Async");
// console.log("--------");
// Sending Q1 News to: Jhon
// Sending Q1 News to: Doe
// Sending Q2 News to: Doe
// Sending Q2 News to: Foo
// Sending Q2 News to: FooBar
// Sending Q2 News to: Foo Once
// I am invoked once Foo Once
// Sending Q2 News to: Foo Twice
// Sending Q2 News to: Foo Once Async
// I am invoked once asynchronously: Foo Once Async
this._subscriptions = new Map(); // Stores all event subscriptions.
// 🚨 dont create differnt map to handle subscribeOnce & subscribeOnceAsync
subscribe(name, callback) {
if (typeof callback !== "function") {
throw new TypeError("Callback should be a function");
if (!this._subscriptions.has(name)) {
this._subscriptions.set(name, []);
const subscription = { callback };
this._subscriptions.get(name).push(subscription);
const eventSubscriptions = this._subscriptions.get(name);
const index = eventSubscriptions.findIndex(
(sub) => sub.callback === callback
eventSubscriptions.splice(index, 1);
// Remove the subscription
subscribeOnce(name, callback) {
const removeOnce = this.subscribe(name, (payload) => {
removeOnce.remove(); // Remove the subscription after it runs
async subscribeOnceAsync(name) {
return new Promise((resolve) => {
const removeOnce = this.subscribe(name, (payload) => {
removeOnce.remove(); // Remove the subscription after it runs
const eventSubscriptions = this._subscriptions.get(name);
if (!eventSubscriptions) {
eventSubscriptions.forEach((sub) => sub.callback(data));
this._subscriptions.forEach((eventSubscriptions) => {
eventSubscriptions.forEach((sub) => sub.callback(data));