最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Code efficiency in Node.js for multiple events listeners - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 5

You 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)
发布评论

评论列表(0)

  1. 暂无评论