0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

repl: add an internal "paused" mode

PR-URL: https://github.com/nodejs/node/pull/15566
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Timothy Gu 2017-09-23 00:09:09 -07:00
parent 7ce6d23387
commit cff6e69057
No known key found for this signature in database
GPG Key ID: 7FE6B095B582B0D4

View File

@ -169,6 +169,34 @@ function REPLServer(prompt,
eval_ = eval_ || defaultEval; eval_ = eval_ || defaultEval;
// Pause taking in new input, and store the keys in a buffer.
const pausedBuffer = [];
let paused = false;
function pause() {
paused = true;
}
function unpause() {
if (!paused) return;
paused = false;
let entry;
while (entry = pausedBuffer.shift()) {
const [type, payload] = entry;
switch (type) {
case 'key': {
const [d, key] = payload;
self._ttyWrite(d, key);
break;
}
case 'close':
self.emit('exit');
break;
}
if (paused) {
break;
}
}
}
function defaultEval(code, context, file, cb) { function defaultEval(code, context, file, cb) {
var err, result, script, wrappedErr; var err, result, script, wrappedErr;
var wrappedCmd = false; var wrappedCmd = false;
@ -420,6 +448,10 @@ function REPLServer(prompt,
'DEP0075'); 'DEP0075');
self.on('close', function emitExit() { self.on('close', function emitExit() {
if (paused) {
pausedBuffer.push(['close']);
return;
}
self.emit('exit'); self.emit('exit');
}); });
@ -557,10 +589,14 @@ function REPLServer(prompt,
} }
}); });
// Wrap readline tty to enable editor mode // Wrap readline tty to enable editor mode and pausing.
const ttyWrite = self._ttyWrite.bind(self); const ttyWrite = self._ttyWrite.bind(self);
self._ttyWrite = (d, key) => { self._ttyWrite = (d, key) => {
key = key || {}; key = key || {};
if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) {
pausedBuffer.push(['key', [d, key]]);
return;
}
if (!self.editorMode || !self.terminal) { if (!self.editorMode || !self.terminal) {
ttyWrite(d, key); ttyWrite(d, key);
return; return;