隐式绑定


【Implicit binding】

如果域正在使用,那么所有新的 EventEmitter 对象(包括 Stream 对象、请求、响应等)将在创建时隐式绑定到活动域。

【If domains are in use, then all new EventEmitter objects (including Stream objects, requests, responses, etc.) will be implicitly bound to the active domain at the time of their creation.】

此外,传递给底层事件循环请求(例如 fs.open() 或其他使用回调的方法)的回调将自动绑定到活动域。如果它们抛出异常,域将捕获该错误。

【Additionally, callbacks passed to low-level event loop requests (such as to fs.open(), or other callback-taking methods) will automatically be bound to the active domain. If they throw, then the domain will catch the error.】

为了防止过度使用内存,Domain 对象本身不会被隐式添加为活动域的子对象。如果这样做,就很容易导致请求和响应对象无法被正确地垃圾回收。

【In order to prevent excessive memory usage, Domain objects themselves are not implicitly added as children of the active domain. If they were, then it would be too easy to prevent request and response objects from being properly garbage collected.】

要将 Domain 对象作为父 Domain 的子对象进行嵌套,必须显式添加它们。

【To nest Domain objects as children of a parent Domain they must be explicitly added.】

隐式绑定会将抛出的错误和 'error' 事件抛送到 Domain'error' 事件,但不会在 Domain 上注册 EventEmitter。隐式绑定仅处理抛出的错误和 'error' 事件。

【Implicit binding routes thrown errors and 'error' events to the Domain's 'error' event, but does not register the EventEmitter on the Domain. Implicit binding only takes care of thrown errors and 'error' events.】