diff --git a/lib/tmp.js b/lib/tmp.js index 05a65fb..fee55dd 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -39,8 +39,6 @@ const EXIT = 'exit', - SIGINT = 'SIGINT', - // this will hold the objects need to be removed on exit _removeObjects = [], @@ -498,6 +496,7 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall * @private */ function _garbageCollector() { +console.log(_gracefulCleanup); /* istanbul ignore else */ if (!_gracefulCleanup) return; @@ -578,96 +577,8 @@ function _getTmpDir() { return os.tmpdir(); } -/** - * If there are multiple different versions of tmp in place, make sure that - * we recognize the old listeners. - * - * @param {Function} listener - * @private - * @returns {Boolean} true whether listener is a legacy listener - */ -function _is_legacy_listener(listener) { - return (listener.name === '_exit' || listener.name === '_uncaughtExceptionThrown') - && listener.toString().indexOf('_garbageCollector();') > -1; -} - -/** - * Safely install SIGINT listener. - * - * NOTE: this will only work on OSX and Linux. - * - * @private - */ -function _safely_install_sigint_listener() { - - const listeners = process.listeners(SIGINT); - const existingListeners = []; - for (let i = 0, length = listeners.length; i < length; i++) { - const lstnr = listeners[i]; - /* istanbul ignore else */ - if (lstnr.name === '_tmp$sigint_listener') { - existingListeners.push(lstnr); - process.removeListener(SIGINT, lstnr); - } - } - process.on(SIGINT, function _tmp$sigint_listener(doExit) { - for (let i = 0, length = existingListeners.length; i < length; i++) { - // let the existing listener do the garbage collection (e.g. jest sandbox) - try { - existingListeners[i](false); - } catch (err) { - // ignore - } - } - try { - // force the garbage collector even it is called again in the exit listener - _garbageCollector(); - } finally { - if (!!doExit) { - process.exit(0); - } - } - }); -} - -/** - * Safely install process exit listener. - * - * @private - */ -function _safely_install_exit_listener() { - const listeners = process.listeners(EXIT); - - // collect any existing listeners - const existingListeners = []; - for (let i = 0, length = listeners.length; i < length; i++) { - const lstnr = listeners[i]; - /* istanbul ignore else */ - // TODO: remove support for legacy listeners once release 1.0.0 is out - if (lstnr.name === '_tmp$safe_listener' || _is_legacy_listener(lstnr)) { - // we must forget about the uncaughtException listener, hopefully it is ours - if (lstnr.name !== '_uncaughtExceptionThrown') { - existingListeners.push(lstnr); - } - process.removeListener(EXIT, lstnr); - } - } - // TODO: what was the data parameter good for? - process.addListener(EXIT, function _tmp$safe_listener(data) { - for (let i = 0, length = existingListeners.length; i < length; i++) { - // let the existing listener do the garbage collection (e.g. jest sandbox) - try { - existingListeners[i](data); - } catch (err) { - // ignore - } - } - _garbageCollector(); - }); -} - -_safely_install_exit_listener(); -_safely_install_sigint_listener(); +// Install process exit listener +process.addListener(EXIT, _garbageCollector); /** * Configuration options.