自动包装任何node.js回调

Wrapping any node.js callback automatically

本文关键字:js 回调 node 任何 包装      更新时间:2023-09-26

是否可以编写代码,用我自己的函数包装任何node.js i/o回调?(当我说I/o回调函数时,我指的是在一些I/o操作后被调用的函数)

为什么我需要这样的东西?示例:我有一个socket.io服务器我有一堆全局函数,我实际上是作为输入得到的(我不能更改它,甚至不能阅读它,我只是把它原样粘贴在我的代码中)

socket.io.connect的回调(socket,…callback)与这些全局函数交互。现在,在新连接之后,我们在全局级别设置对当前套接字的访问。编写这些全局函数的程序员知道currentSocket变量并使用它。

问题是,在调用一些异步i/o函数后,其他一些用户/套接字可能会连接并更改currentSocket,稍后当异步i/o函数执行时,currentSocket将等于不同的用户/套接字,而不是调用该异步函数的原始用户/套接字

我想也许我可以以某种方式自动包装I/o回调并关闭currentSocket变量,但我不知道如何做到。。。知道吗?

var currentSocket = undefined;
io.sockets.on('connection', function (socket) {
  currentSocket = socket;
  socket.on('msg', function (data) {
    global[data.functionToInvoke];
  });
});
//global function that I can't change or even analyze/read
//========================================================
function g1(){
   //might use currentSocket
   //might invoke async stuff like
   fs.readfile(...cb)
}
function g2(){
   //might use currentSocket
   //might invoke async stuff like
   redisClient.get(...cb)
}

如果您的函数都共享currentSocket全局变量(如果您不能更改它们),则不可能有多个连接(只要函数是异步的)。若您可以更改它们,最好只是将套接字作为第一个参数传递给您的函数。另一种方法是将currentSocket的定义和函数移动到io.sockets connection回调中。在这种情况下,currentSocket变量形成了一个闭包,一切都应该按预期工作:

io.sockets.on('connection', function (socket) {
  var currentSocket = socket;
  socket.on('msg', function (data) {
    global[data.functionToInvoke];
  });
  //global function that I can't change or even analyze/read
  //========================================================
  function g1(){
     //might use currentSocket
     //might invoke async stuff like
     fs.readfile(...cb)
  }
  function g2(){
     //might use currentSocket
     //might invoke async stuff like
     redisClient.get(...cb)
  }
});

如果我正确理解你的问题,一种方法可以通过别名方法链来完成。您可以将原始回调传递到匿名函数表达式中,该表达式将返回新的函数表达式:

cb = (function (original) {
  return function () {
    // do something with original callback
    original();
    // add more functionality 
  }
})(cb); 

如果你想在JS中包装一个已建立的方法,方法是重命名&更换。

例如

 Array.prototype._toString = Array.prototype.toString;
 Array.prototype.toString = function(){ return "wrapped["+this._toString()+"]"; };
 console.log(["1","2","3"].toString());

我建议将其应用于您的用例,在这种情况下,可以是io.sockets对象,也可以是更深层次的对象,也许一直到EventEmitter.emit()方法。