Skip to content

Commit cff6e69

Browse files
committed
repl: add an internal "paused" mode
PR-URL: #15566 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 7ce6d23 commit cff6e69

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

lib/repl.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ function REPLServer(prompt,
169169

170170
eval_ = eval_ || defaultEval;
171171

172+
// Pause taking in new input, and store the keys in a buffer.
173+
const pausedBuffer = [];
174+
let paused = false;
175+
function pause() {
176+
paused = true;
177+
}
178+
function unpause() {
179+
if (!paused) return;
180+
paused = false;
181+
let entry;
182+
while (entry = pausedBuffer.shift()) {
183+
const [type, payload] = entry;
184+
switch (type) {
185+
case 'key': {
186+
const [d, key] = payload;
187+
self._ttyWrite(d, key);
188+
break;
189+
}
190+
case 'close':
191+
self.emit('exit');
192+
break;
193+
}
194+
if (paused) {
195+
break;
196+
}
197+
}
198+
}
199+
172200
function defaultEval(code, context, file, cb) {
173201
var err, result, script, wrappedErr;
174202
var wrappedCmd = false;
@@ -420,6 +448,10 @@ function REPLServer(prompt,
420448
'DEP0075');
421449

422450
self.on('close', function emitExit() {
451+
if (paused) {
452+
pausedBuffer.push(['close']);
453+
return;
454+
}
423455
self.emit('exit');
424456
});
425457

@@ -557,10 +589,14 @@ function REPLServer(prompt,
557589
}
558590
});
559591

560-
// Wrap readline tty to enable editor mode
592+
// Wrap readline tty to enable editor mode and pausing.
561593
const ttyWrite = self._ttyWrite.bind(self);
562594
self._ttyWrite = (d, key) => {
563595
key = key || {};
596+
if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) {
597+
pausedBuffer.push(['key', [d, key]]);
598+
return;
599+
}
564600
if (!self.editorMode || !self.terminal) {
565601
ttyWrite(d, key);
566602
return;

0 commit comments

Comments
 (0)