Open clavin opened 2 years ago
Reopened because of https://github.com/sindresorhus/ora/issues/211
Just to document it, here's another possible solution I tried that ended up a dead end:
Since readline
pauses the input
stream it's given, I thought why not try giving it a proxy stream of stdin that we can dispose of at the end of StdinDiscarder
's lifetime, i.e. using pipe
to flow stdin
through a PassThrough
stream and providing that as the input
to readline
.
[input proxy]
+-------+ +-------------+
| stdin | ------> | PassThrough |
+-------+ +-------------+
|
v
input
+----------+
| readline |
+----------+
output
|
v
+--------+ +------------------+
| stdout | <-- | BufferListStream |
+--------+ +------------------+
That sort of works, but there's an important caveat: when the proxy stream is discarded, either by unpipe
ing it from stdin
or by calling destroy
on it, the upstream stdin
can become paused if there are no other pipe
s attached to it. That is, if you have an on('data', ...)
event handler on stdin
, stdin
will become paused; however, if you are still pipe
ing stdin
to another stream and reading from that, stdin
will not become paused. The practical difference between unpipe
and destory
here is that unpipe
will try to pause immediately, whereas destroy
will try to pause on the next tick.
Ultimately, I decided this solution was too finicky to contribute as it is not significantly better than just calling resume
on stdin
in user code.
When ora uses its
StdinDiscarder
,stdin
will be paused when the discarder is stopped (on all platforms except windows). This is due to a historic, undocumented implementation detail ofnode:readline
, where callingreadline.close()
causes the input stream (stdin
) to be paused. Thus, this propagates up toStdinDiscarder
when it callsclose
on itsreadline
handle.I feel like this behavior seems like a bug in
ora
, unintentionally present due to the side-effect fromnode:readline
. It feels inconsistent thatstdin
would be paused at the very end ofora
's lifecycle only on the condition of using a tty and theStdinDiscarder
. I would expectstdin
to remain unpaused at the end ofora
's lifecycle regardless of those conditions.