dispose函数调用exit和dispose函数,删除所有侦听器,删除所有错误处理程序,并尝试杀死域的所有成员。该函数检查域是否有父项,如果有,则将其从域中删除。然后将该域设置为垃圾收集,并标记为已处理。
从节点文档:
一旦域设置在所述处置事件会发出。
我会更深入地讨论这个话题,但是Node源已经很好地注释了。
你正在谈论的计时器会在这里,域的成员正在迭代。
this.members.forEach(function(m) {
// if it's a timeout or interval, cancel it.
clearTimeout(m);
});
下面是从节点source:
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;
};
所以看起来像“处置”事件可能是我想知道的钩。看起来很奇怪,他们在使用clearTimeout之前不检查成员是否超时。它们似乎错了,它们隐藏了它们运行的endMethods中未捕获到的异常。什么是成为“域名成员”的一组东西?有没有办法让自定义对象成为域成员?来源不明确。 – 2013-04-10 07:03:33
是的,这些方法看起来不太干净,虽然我不是一个非常有经验的编码器,所以我不知道更好。我确实有一个问题 - 你在谈论什么类型的自定义对象?成员是任何类型的'EventEmitter',因为域专门用于处理多个I/O操作作为单个组。 – hexacyanide 2013-04-10 23:49:37
我在说的自定义对象实际上是你能想到的任何东西。一个例子是某些专用协议处理TCP通信的模块。如果在通过dispose()死亡之前想要发送错误,则可能会有一些协议期望的特定消息。能够确保您的自定义内容与核心IO功能一样完全一次性就好了。 – 2013-04-11 03:40:34