Skip to content

Commit ce391ed

Browse files
Fishrock123rvagg
authored andcommitted
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: #2356 PR-URL: #3435 Reviewed By: Evan Lucas <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent b4f4c24 commit ce391ed

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/repl.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const Stream = require('stream');
2727
const vm = require('vm');
2828
const path = require('path');
2929
const fs = require('fs');
30-
const rl = require('readline');
30+
const Interface = require('readline').Interface;
3131
const Console = require('console').Console;
3232
const domain = require('domain');
3333
const debug = util.debuglog('repl');
@@ -229,7 +229,7 @@ function REPLServer(prompt,
229229
self.complete(text, callback);
230230
}
231231

232-
rl.Interface.call(this, {
232+
Interface.call(this, {
233233
input: self.inputStream,
234234
output: self.outputStream,
235235
completer: complete,
@@ -453,7 +453,7 @@ function REPLServer(prompt,
453453

454454
self.displayPrompt();
455455
}
456-
inherits(REPLServer, rl.Interface);
456+
inherits(REPLServer, Interface);
457457
exports.REPLServer = REPLServer;
458458

459459
exports.REPL_MODE_SLOPPY = Symbol('repl-sloppy');
@@ -479,6 +479,20 @@ exports.start = function(prompt,
479479
return repl;
480480
};
481481

482+
REPLServer.prototype.close = function replClose() {
483+
if (this.terminal && this._flushing && !this._closingOnFlush) {
484+
this._closingOnFlush = true;
485+
this.once('flushHistory', () =>
486+
Interface.prototype.close.call(this)
487+
);
488+
489+
return;
490+
}
491+
process.nextTick(() =>
492+
Interface.prototype.close.call(this)
493+
);
494+
};
495+
482496
REPLServer.prototype.createContext = function() {
483497
var context;
484498
if (this.useGlobal) {

0 commit comments

Comments
 (0)