在效率和优雅方面,最好是添加和删除事件监听器或使用全局/高级变量来控制发射器事件逻辑? [英] In terms of efficiency and elegance, is it better to add and remove event listeners or use global/high-level variables to control emitter event logic?

查看:108
本文介绍了在效率和优雅方面,最好是添加和删除事件监听器或使用全局/高级变量来控制发射器事件逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了澄清我的问题,这里有两个javascript-esque伪代码的例子。
这里是相同的例子,但是有一些更多的代码将它们进行上下文化: http://pastebin.com/fRjW5qp6

To clarify my question, here are two examples in javascript-esque pseudocode. Here are the same examples but with a bit more code to contextualize them: http://pastebin.com/fRjW5qp6

查看我如何使用条件来防止套接字在触发重要事件逻辑后触发它一次(例如我将socket.loginBuilt设置为事件侦听器被触发后为true)。

See how I'm using conditionals to prevent sockets from triggering the important event logic after they've already triggered it once (e.g. I set socket.loginBuilt to true after the event listener has been triggered).

socket.on('login screen built', socketLoginScreenBuilt);

  function socketLoginScreenBuilt() {
    // See how I'm using this conditional to prevent sockets from triggering the important event logic after they've already triggered it once (since I set socket.loginBuilt to true in the logic)
    if (!socket.loginBuilt) {
      numberOfSocketsWithBuiltLoginScreens++;
      socket.loginBuilt = true;

      if (numberOfSocketsWithBuiltLoginScreens === io.sockets.sockets.length) {
        app.emit('all login screens built')
      }
    }
  }

  socket.on('login credentials', socketLoginCredentials);

  function socketLoginCredentials(credentials) {
    if (readyForLoginCredentials) {
      socket.username = credentials.username;
      socket.password = credentials.password;

      socket.emit('user stored data', socket.userData);
    }
  }
});

-------------------或 - ------------------

------------------- OR -------------------

注意我不使用上面使用的条件,因为我删除了监听器功能第一次运行后。这样我就确定套接字不会多次触发重要的事件逻辑。

Notice how I'm not using the conditionals I used above because I remove the listeners after the functions run for the first time. In that way I'll be certain that a socket won't trigger the important event logic multiple times.

socket.on('login screen built', socketLoginScreenBuilt);  

function socketLoginScreenBuilt() {
  // Notice how I'm not using a conditional here because I remove the 'login screen built' listener after this function is first ran. In that way I'll be certain that a socket won't trigger the important event logic multiple times
  numberOfSocketsWithBuiltLoginScreens++;
  socket.loginBuilt = true;

  if (numberOfSocketsWithBuiltLoginScreens === io.sockets.sockets.length) {
    app.emit('all login screens built')
  }

  socket.removeListener('login screen built', socketLoginScreenBuilt);
}

socket.on('login credentials', socketLoginCredentials);

function socketLoginCredentials(credentials) {

  socket.username = credentials.username;
  socket.password = credentials.password;

  socket.emit('user stored data', socket.userData);
  socket.removeListener('login credentials', socketLoginCredentials);
}


推荐答案

我想澄清2件事:


  • Node.js是事件驱动的。

  • 全局变量是 evil (有一个 C / C ++ 标签,但它们使点)

  • Node.js is event-driven.
  • Global variables are evil (there's a C/C++ tag but they make the point)

所以,为了保持你的代码干净,而且要达到标准,你应该使用事件驱动方法而不是全局变量。

So, in order to keep your code clean and, well, up-to-standards, you should use event-driven approach rather than global variables.

BTW在这两种情况下,您使用全局变量 numberOfSocketsWithBuiltLoginScreens

BTW in both cases you do use global variable numberOfSocketsWithBuiltLoginScreens.

这篇关于在效率和优雅方面,最好是添加和删除事件监听器或使用全局/高级变量来控制发射器事件逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆