What exactly does domain.dispose () do in nodejs? Are there any hooks?

Reading the documentation at http://nodejs.org/api/domain.html makes it a little vague: "makes every effort to clear all IOs associated with the domain". It mentions that the timers are disabled, which is not entirely true. It would be really nice to know the complete list of things domain.dispose does. Does anyone have this list?

Also, is there a way to connect to this function — that is, to allow some custom cleansing code to be called when domain.dispose () is run?

+6
source share
1 answer

The dispose function calls the exit and delete functions, deletes all listeners, deletes all error handlers, and attempts to kill all domain members. The function checks if the domain has a parent, and if it does, it is removed from the domain. Then the domain is set to collect garbage and marked as located.

From the Node documentation:

Once the domain is deleted, the dispose event will occur.

I would delve into this topic, but the Node source is already beautifully annotated.

The timer you are talking about will be here where the domain members are repeated.

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

Here from Node 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 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 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; }; 
+5
source

All Articles