domain.dispose()在nodejs中做了什么? 有没有钩子?

阅读http://nodejs.org/api/domain.html上的文档使其有点含糊:“ 尽最大的努力去清除与域相关的任何和所有的IO ”。 它提到定时器被closures,这不完全是IO。 知道domain.dispose所做的全面的列表将是非常好的。 有没有人有这个名单?

此外,有没有办法挂钩到该function – 即允许一些自定义清理代码时,domain.dispose()运行?

dispose函数调用exit和dispose函数,删除所有侦听器,删除所有error handling程序,并尝试杀死域的所有成员。 该函数检查域是否有父项,如果有,则将其从域中删除。 然后将该域设置为垃圾收集,并标记为已处理。

从Node文档:

一旦域被处置,处置事件就会发出。

我会更深入地讨论这个话题,但是Node源已经被很好的注释了。

你正在谈论的计时器就在这里,域的成员正在被迭代。

this.members.forEach(function(m) { // if it's a timeout or interval, cancel it. clearTimeout(m); }); 

来自Node的来源 :

 Domain.prototype.dispose = function() { if (this._disposed) return; // if we're the active domain, then get out now. this.exit(); this.emit('dispose'); // remove error handlers. this.removeAllListeners(); this.on('error', function() {}); // try to kill all the members. // XXX There should be more consistent ways // to shut down things! this.members.forEach(function(m) { // if it's a timeout or interval, cancel it. clearTimeout(m); // drop all event listeners. if (m instanceof EventEmitter) { m.removeAllListeners(); // swallow errors m.on('error', function() {}); } // Be careful! // By definition, we're likely in error-ridden territory here, // so it's quite possible that calling some of these methods // might cause additional exceptions to be thrown. endMethods.forEach(function(method) { if (typeof m[method] === 'function') { try { m[method](); } catch (er) {} } }); }); // remove from parent domain, if there is one. if (this.domain) this.domain.remove(this); // kill the references so that they can be properly gc'ed. this.members.length = 0; // finally, mark this domain as 'no longer relevant' // so that it can't be entered or activated. this._disposed = true; };