From ecab7a6bee6857a07855bcbf749c75c68502ab49 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Tue, 6 Oct 2015 23:05:53 -0700 Subject: [PATCH] repl: event ordering: delay 'close' until 'flushHistory' Emitting 'close' before the history has flushed is somewhat incorrect and rather confusing. This also makes the 'close' event always asynchronous for consistency. Refs: /~https://github.com/nodejs/node/pull/2356 PR-URL: /~https://github.com/nodejs/node/pull/3435 Reviewed By: Evan Lucas Reviewed-By: Trevor Norris --- lib/repl.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 46288db5639c59..ed3df92e08526b 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -27,7 +27,7 @@ const Stream = require('stream'); const vm = require('vm'); const path = require('path'); const fs = require('fs'); -const rl = require('readline'); +const Interface = require('readline').Interface; const Console = require('console').Console; const domain = require('domain'); const debug = util.debuglog('repl'); @@ -229,7 +229,7 @@ function REPLServer(prompt, self.complete(text, callback); } - rl.Interface.call(this, { + Interface.call(this, { input: self.inputStream, output: self.outputStream, completer: complete, @@ -453,7 +453,7 @@ function REPLServer(prompt, self.displayPrompt(); } -inherits(REPLServer, rl.Interface); +inherits(REPLServer, Interface); exports.REPLServer = REPLServer; exports.REPL_MODE_SLOPPY = Symbol('repl-sloppy'); @@ -479,6 +479,20 @@ exports.start = function(prompt, return repl; }; +REPLServer.prototype.close = function replClose() { + if (this.terminal && this._flushing && !this._closingOnFlush) { + this._closingOnFlush = true; + this.once('flushHistory', () => + Interface.prototype.close.call(this) + ); + + return; + } + process.nextTick(() => + Interface.prototype.close.call(this) + ); +}; + REPLServer.prototype.createContext = function() { var context; if (this.useGlobal) {