在Node JS中注册相同的事件侦听器两次还是两个不同的侦听器?

时间:2020-08-27 22:29:39

标签: node.js mongodb mongoose eventemitter

我有一个带有四个连接事件(已连接/已断开/错误/过程结束)的Node JS Express应用程序。我在代码中两次为这些事件注册了侦听器:一次是在应用程序连接到mongoDB时开始(请参见以下方案中的foo1函数),一次是在将数据保存到DB中时(请参见函数foo2)结束时。为每个事件两次注册同一侦听器(下面的方案1)还是为每个事件注册两个不同的侦听器(方案2)更好?请注意,我还将在代码末尾删除侦听器,以防止内存泄漏。

场景1:

function connected_listener(){
  console.log("Mongoose is connected");
}
function disconnected_listener(){
  console.log("Mongoose is disconnected");
  process.exit(1);
}
function error_listener(err){
  console.log('Mongoose connection error: ', err);
  process.exit(1);
}
function gracefulExit() { //code executed when SIGINT fired
  mongoose.connection.close(function () {
    console.log("app is terminating");
  mongoose.connection.removeAllListeners();
  process.exit(0);
  });
}
function foo1(){ //code in the beginning when the app connects to mongoDB
  mongoose.connection.on('connected', connected_listener);
  mongoose.connection.on('disconnected', disconnected_listener);
  mongoose.connection.on('error', error_listener);
  process.on('SIGINT', gracefulExit);
  if (mongoose.connection.readyState !== 1){
    mongoose.connect(dbURI, {useUnifiedTopology: true ,useNewUrlParser: true},() => {
      console.log("Mongoose state: ", mongoose.connection.readyState);
    })
    .catch((e) => {
      console.log("catch error: ", e);
    });
  }
//additional code
}
// additional functions//
function foo2(){ //code at the end when the app saves data to mongoDB
  mongoose.connection.on('connected', connected_listener);
  mongoose.connection.on('disconnected', disconnected_listener);
  mongoose.connection.on('error', error_listener);
  process.on('SIGINT', gracefulExit);
  if (mongoose.connection.readyState !== 1){
    mongoose.connect(dbURI, {useUnifiedTopology: true ,useNewUrlParser: true},() => {
    console.log("Mongoose state: ", mongoose.connection.readyState);
    })
    .catch((e) => {
      console.log("catch error: ", e);
    });
  }
//code to remove mongoose listeners
mongoose.connection.removeListener('connected', connected_listener);
mongoose.connection.removeListener('connected', connected_listener);
mongoose.connection.removeListener('disconnected', disconnected_listener);
mongoose.connection.removeListener('disconnected', disconnected_listener);
mongoose.connection.removeListener('error', error_listener);
mongoose.connection.removeListener('error', error_listener);
mongoose.connection.removeListener('SIGINT', gracefulExit);
mongoose.connection.removeListener('SIGINT', gracefulExit);
//some additional code here to save to mongoDB
}

方案2:

function connected_listener1(){
  console.log("Mongoose is connected");
}
function connected_listener2(){
  console.log("Mongoose is connected");
}
function disconnected_listener1(){
  console.log("Mongoose is disconnected");
process.exit(1);
}
function disconnected_listener2(){
  console.log("Mongoose is disconnected");
  process.exit(1);
}
function error_listener1(err){
  console.log('Mongoose connection error: ', err);
  process.exit(1);
}
function error_listener2(err){
  console.log('Mongoose connection error: ', err);
  process.exit(1);
}
function gracefulExit1() { //code executed when SIGINT fired
  mongoose.connection.close(function () {
  console.log("app is terminating");
  mongoose.connection.removeAllListeners();
  process.exit(0);
  });
}
function gracefulExit2() { //code executed when SIGINT fired
  mongoose.connection.close(function () {
  console.log("app is terminating");
  mongoose.connection.removeAllListeners();
  process.exit(0);
  });
}
function foo1(){ //code in the beginning when the app connects to mongoDB
  mongoose.connection.on('connected', connected_listener1);
  mongoose.connection.on('disconnected', disconnected_listener1);
  mongoose.connection.on('error', error_listener1);
  process.on('SIGINT', gracefulExit1);
  if (mongoose.connection.readyState !== 1){
    mongoose.connect(dbURI, {useUnifiedTopology: true ,useNewUrlParser: true},() => {
      console.log("Mongoose state: ", mongoose.connection.readyState);
    })
    .catch((e) => {
    console.log("catch error: ", e);
    });
  }
//additional code
}
// additional functions//
function foo2(){ //code at the end when the app saves data to mongoDB
  mongoose.connection.on('connected', connected_listener2);
  mongoose.connection.on('disconnected', disconnected_listener2);
  mongoose.connection.on('error', error_listener2);
  process.on('SIGINT', gracefulExit2);
  if (mongoose.connection.readyState !== 1){
    mongoose.connect(dbURI, {useUnifiedTopology: true ,useNewUrlParser: true},() => {
    console.log("Mongoose state: ", mongoose.connection.readyState);
    })
    .catch((e) => {
      console.log("catch error: ", e);
    });
  }
//code to remove mongoose listeners
  mongoose.connection.removeListener('connected', connected_listener1);
  mongoose.connection.removeListener('connected', connected_listener2);
  mongoose.connection.removeListener('disconnected', disconnected_listener1);
  mongoose.connection.removeListener('disconnected', disconnected_listener2);
  mongoose.connection.removeListener('error', error_listener1);
  mongoose.connection.removeListener('error', error_listener2);
  mongoose.connection.removeListener('SIGINT', gracefulExit1);
  mongoose.connection.removeListener('SIGINT', gracefulExit2);
  //some additional code here to save to mongoDB
}

0 个答案:

没有答案