I have a code that emits and handles events in same .js file. Emitting events can be of type "hello", "world", "user", etc as an example. All the event handler functions (.on) have similar approach to handle events inside core functions of error and callbacks (exactly same). Is there a suggestion to refactor and improve the redundant code.
localEvents.on("hello", function(request , reply){
console.log("inside event for hello");
hello.intialize() {
// handle initialise error and callback //
};
});
localEvents.on("world", function(request , reply){
console.log("inside event for world");
world.initialise() {
// handle initialise error and callback //
};
});
localEvents.on("user", function(request , reply){
console.log("inside event for user");
user.initialise() {
// handle initialise error and callback //
};
});
localEvents.emit(task, request, reply);
I have a code that emits and handles events in same .js file. Emitting events can be of type "hello", "world", "user", etc as an example. All the event handler functions (.on) have similar approach to handle events inside core functions of error and callbacks (exactly same). Is there a suggestion to refactor and improve the redundant code.
localEvents.on("hello", function(request , reply){
console.log("inside event for hello");
hello.intialize() {
// handle initialise error and callback //
};
});
localEvents.on("world", function(request , reply){
console.log("inside event for world");
world.initialise() {
// handle initialise error and callback //
};
});
localEvents.on("user", function(request , reply){
console.log("inside event for user");
user.initialise() {
// handle initialise error and callback //
};
});
localEvents.emit(task, request, reply);
Share
Improve this question
edited Nov 15, 2019 at 22:04
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Aug 16, 2015 at 4:50
user3601166user3601166
1272 silver badges13 bronze badges
2
- Can you tell me what is "request" and "reply" in the function parameters? – Kushal Commented Aug 16, 2015 at 4:59
- request and reply are hapi server objects, like req and rep in express. – user3601166 Commented Aug 16, 2015 at 5:14
2 Answers
Reset to default 5You could create a helper function, like:
function addHandler(evName, obj) {
localEvents.on(evName, function(request, reply) {
console.log("inside event for %s", evName);
obj.initialise(function(err) {
// handle initialise error and callback
});
});
}
Then just call it like:
addHandler('hello', hello);
addHandler('world', world);
addHandler('user', user);
Or if you really want to cut down on even the repetition in the arguments you might create a helper like:
function addHandler(evName) {
var obj = eval(evName);
localEvents.on(evName, function(request, reply) {
console.log("inside event for %s", evName);
obj.initialise(function(err) {
// handle initialise error and callback
});
});
}
Allowing you to then to simply do:
addHandler('hello');
addHandler('world');
addHandler('user');
A simple way of doing this - This way will be more flexible to any changes that you might need to make in the events callback functions
var eventsList = {
"hello": function(request, reply) {
console.log("inside event for hello");
hello.initialise() {
// handle initialise error and callback //
};
},
"world": function(request, reply) {
console.log("inside event for world");
world.initialise() {
// handle initialise error and callback //
};
},
"user": function(request, reply) {
console.log("inside event for user");
user.initialise() {
// handle initialise error and callback //
};
}
}
function generateEvents(events){
for(var e in events){
localEvents.on(e, events.e)
}
}
generateEvents(eventsList)