admin管理员组文章数量:1357682
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)
本文标签: javascriptCode efficiency in Nodejs for multiple events listenersStack Overflow
版权声明:本文标题:javascript - Code efficiency in Node.js for multiple events listeners - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744016925a2576498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论