The Domain class encapsulates the functionality of routing errors and uncaught exceptions to the active Domain object.
Variables
read onlymembers:Array<EitherType<IEventEmitter, Timeout>>
An array of timers and event emitters that have been explicitly added to the domain.
Methods
add(emitter:IEventEmitter):Void
add(emitter:Timeout):Void
Explicitly adds an emitter
to the domain.
If any event handlers called by the emitter throw an error, or if the emitter emits an error event, it will be routed to the domain's error event, just like with implicit binding.
This also works with timers that are returned from setInterval
and setTimeout
.
If their callback function throws, it will be caught by the domain 'error' handler.
If the Timer or EventEmitter was already bound to a domain, it is removed from that one, and bound to this one instead.
bind<T>(callback:T):T
The returned function will be a wrapper around the supplied callback
function.
When the returned function is called, any errors that are thrown will be routed to the domain's error event.
dispose():Void
The dispose
method destroys a domain, and makes a best effort attempt
to clean up any and all IO that is associated with the domain.
Streams are aborted, ended, closed, and/or destroyed. Timers are cleared. Explicitly bound callbacks are no longer called.
Any error events that are raised as a result of this are ignored.
The intention of calling dispose
is generally to prevent cascading errors when a critical part of
the Domain context is found to be in an error state.
Once the domain is disposed the 'dispose' event will emit.
Note that IO might still be performed. However, to the highest degree possible, once a domain is disposed, further errors from the emitters in that set will be ignored. So, even if some remaining actions are still in flight, Node.js will not communicate further about them.
enter():Void
The enter
method is plumbing used by the run
, bind
, and intercept
methods to set the active domain.
It sets domain.active
and process.domain
to the domain, and implicitly pushes the domain onto
the domain stack managed by the domain module (see exit
for details on the domain stack).
The call to enter
delimits the beginning of a chain of asynchronous calls and I/O operations bound to a domain.
Calling enter
changes only the active domain, and does not alter the domain itself.
Enter and exit can be called an arbitrary number of times on a single domain.
If the domain on which enter
is called has been disposed, enter
will return without setting the domain.
exit():Void
The exit
method exits the current domain, popping it off the domain stack.
Any time execution is going to switch to the context of a different chain of asynchronous calls,
it's important to ensure that the current domain is exited. The call to exit
delimits either the end of
or an interruption to the chain of asynchronous calls and I/O operations bound to a domain.
If there are multiple, nested domains bound to the current execution context,
exit
will exit any domains nested within this domain.
Calling exit
changes only the active domain, and does not alter the domain itself.
Enter and exit can be called an arbitrary number of times on a single domain.
If the domain on which exit
is called has been disposed, exit
will return without exiting the domain.
intercept<T>(callback:T):T
This method is almost identical to bind
. However, in addition to catching thrown errors, it will also
intercept Error
objects sent as the first argument to the function.
In this way, the common if (er != null) return callback(er); pattern can be replaced with a single error handler in a single place.