Skip to content

Instantly share code, notes, and snippets.

@dev-ashishk
Last active June 25, 2019 13:40
Show Gist options
  • Save dev-ashishk/98240f8ee6c975ca63d7f1d60fa65475 to your computer and use it in GitHub Desktop.
Save dev-ashishk/98240f8ee6c975ca63d7f1d60fa65475 to your computer and use it in GitHub Desktop.
Priority Event Bus to trigger events in the order of priority
//===================================================================================================================================
//
// ##### ##### ## ##### ##### ## ###### ## ## ##### ## ## ##### ## ## ###### ##### ## ## ####
// ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ## ## ##
// ##### ##### ## ## ## ##### ## ## #### ##### ## ## ##### ## ## ## ## ##### ## ## ###
// ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ## ##
// ## ## ## ## ##### ## ## ## ## ## ##### ### ##### ## ## ## ##### ##### ####
//
//===================================================================================================================================
/**
* @author Ashish Kumar
* Singleton Instance of PriorityEventBus
*/
var PriorityEventBus = (function () {
// for singleton instance
var instance;
function Init() {
var events = {};
var broadCastStackTree = {
"0": {
name: "low",
stack: []
}
};
/**
* @description Register Priority Queues
* @param {String} name
* @param {Number} weight
*/
var registerPriorityQueue = function (name, weight) {
if (!broadCastStackTree.hasOwnProperty(weight)) {
broadCastStackTree[weight] = {
name: name,
stack: []
};
}
};
var broadcast = function (event, data, priority) {
priority = priority || "0";
broadCastStackTree[priority].stack.push({
event: event,
data: data
});
(function (_broadCastStackTree) {
setTimeout(function () {
var tree = _broadCastStackTree
var keys = Object.keys(tree);
keys.sort(function (a, b) {
return b - a;
});
for (var i = 0; i < keys.length; i++) {
executeBroadCastOnPriority(keys[i]);
}
}, 0);
})(broadCastStackTree);
};
var executeBroadCastOnPriority = function (priority) {
var obj = broadCastStackTree[priority];
var priorityBroadCastList = obj.stack;
for (var i = 0; i < priorityBroadCastList.length; i++) {
var evnt = priorityBroadCastList[i];
var eventHandlerArray = events[evnt.event];
if (eventHandlerArray) {
for (var j = 0; j < eventHandlerArray.length; j++) {
eventHandlerArray[j](evnt.data, {
name: obj.name,
weight: priority
});
}
}
}
broadCastStackTree[priority].stack = [];
};
/*
* @private Method
* @Register Handler for events
*/
var subscribe = function (event, handler) {
var eventArray = events[event];
if (!eventArray) {
events[event] = [handler];
} else {
events[event] = eventArray.push(handler);
}
};
var publicMethods = {
subscribe : subscribe,
broadcast : broadcast,
registerQueue : registerPriorityQueue
};
if (!instance) {
instance = publicMethods;
}
return instance;
}
// check if instance already exists
Init.getInstance = function () {
return instance || new Init();
}
return Init;
})();
var a = new PriorityEventBus();
var b = new PriorityEventBus();
var c = new PriorityEventBus();
a.registerQueue("medium",1);
c.registerQueue("high",2);
b.registerQueue("critical", 3);
a.subscribe("event1", function (data, priority) {
console.log("event1 => " + data + " --" + priority.name + " --" + priority.weight);
});
b.subscribe("event2", function (data, priority) {
console.log("event2 => " + data + " --" + priority.name + " --" + priority.weight);
});
c.subscribe("event3", function (data, priority) {
console.log("event3 => " + data + " --" + priority.name + " --" + priority.weight);
});
a.broadcast("event1", "hello", 1);
a.broadcast("event2", "hello1", 2);
a.broadcast("event3", "hello2", 2);
a.broadcast("event3", "hello3", 1);
a.broadcast("event2", "hello4", 3);
a.broadcast("event1", "hello5", 1);
a.broadcast("event1", "hello6", 3);
a.broadcast("event2", "hello7", 2);
a.broadcast("event1", "hello8");
a.broadcast("event1", "hello", 1);
a.broadcast("event2", "hello1", 2);
a.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
b.broadcast("event2", "hello7", 2);
b.broadcast("event1", "hello8");
b.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
b.broadcast("event2", "hello7", 2);
b.broadcast("event1", "hello8");
b.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
c.broadcast("event1", "hello5", 1);
c.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
c.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
b.broadcast("event2", "hello7", 2);
b.broadcast("event1", "hello8");
b.broadcast("event1", "hello", 1);
c.broadcast("event2", "hello1", 2);
c.broadcast("event3", "hello2", 2);
c.broadcast("event3", "hello3", 1);
c.broadcast("event2", "hello4", 3);
c.broadcast("event1", "hello5", 1);
c.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
c.broadcast("event1", "hello", 1);
c.broadcast("event2", "hello1", 2);
c.broadcast("event3", "hello2", 2);
c.broadcast("event3", "hello3", 1);
c.broadcast("event2", "hello4", 3);
c.broadcast("event1", "hello5", 1);
c.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
c.broadcast("event1", "hello", 1);
c.broadcast("event2", "hello1", 2);
c.broadcast("event3", "hello2", 2);
a.broadcast("event3", "hello3", 1);
a.broadcast("event2", "hello4", 3);
a.broadcast("event1", "hello5", 1);
a.broadcast("event1", "hello6", 3);
a.broadcast("event2", "hello7", 2);
a.broadcast("event1", "hello8");
a.broadcast("event1", "hello", 1);
a.broadcast("event2", "hello1", 2);
a.broadcast("event3", "hello2", 2);
a.broadcast("event3", "hello3", 1);
a.broadcast("event2", "hello4", 3);
a.broadcast("event1", "hello5", 1);
a.broadcast("event1", "hello6", 3);
a.broadcast("event2", "hello7", 2);
b.broadcast("event1", "hello8");
b.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
b.broadcast("event2", "hello7", 2);
b.broadcast("event1", "hello8");
b.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
c.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
c.broadcast("event1", "hello", 1);
c.broadcast("event2", "hello1", 2);
c.broadcast("event3", "hello2", 2);
c.broadcast("event3", "hello3", 1);
c.broadcast("event2", "hello4", 3);
c.broadcast("event1", "hello5", 1);
c.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
c.broadcast("event1", "hello", 1);
c.broadcast("event2", "hello1", 2);
c.broadcast("event3", "hello2", 2);
c.broadcast("event3", "hello3", 1);
c.broadcast("event2", "hello4", 3);
c.broadcast("event1", "hello5", 1);
c.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
c.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
b.broadcast("event2", "hello7", 2);
b.broadcast("event1", "hello8");
b.broadcast("event1", "hello", 1);
b.broadcast("event2", "hello1", 2);
b.broadcast("event3", "hello2", 2);
b.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
b.broadcast("event2", "hello7", 2);
a.broadcast("event1", "hello8");
a.broadcast("event1", "hello", 1);
a.broadcast("event2", "hello1", 2);
a.broadcast("event3", "hello2", 2);
a.broadcast("event3", "hello3", 1);
b.broadcast("event2", "hello4", 3);
b.broadcast("event1", "hello5", 1);
b.broadcast("event1", "hello6", 3);
c.broadcast("event2", "hello7", 2);
c.broadcast("event1", "hello8");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment