77} = primordials ;
88
99const { CSI } = require ( 'internal/readline/utils' ) ;
10- const { validateInteger } = require ( 'internal/validators' ) ;
10+ const { validateBoolean , validateInteger } = require ( 'internal/validators' ) ;
1111const { isWritable } = require ( 'internal/streams/utils' ) ;
1212const { codes : { ERR_INVALID_ARG_TYPE } } = require ( 'internal/errors' ) ;
1313
@@ -19,13 +19,18 @@ const {
1919} = CSI ;
2020
2121class Readline {
22+ #autoCommit = false ;
2223 #stream;
2324 #todo = [ ] ;
2425
25- constructor ( stream ) {
26+ constructor ( stream , options = undefined ) {
2627 if ( ! isWritable ( stream ) )
2728 throw new ERR_INVALID_ARG_TYPE ( 'stream' , 'Writable' , stream ) ;
2829 this . #stream = stream ;
30+ if ( options ?. autoCommit != null ) {
31+ validateBoolean ( options . autoCommit , 'options.autoCommit' ) ;
32+ this . #autoCommit = options . autoCommit ;
33+ }
2934 }
3035
3136 /**
@@ -38,10 +43,9 @@ class Readline {
3843 validateInteger ( x , 'x' ) ;
3944 if ( y != null ) validateInteger ( y , 'y' ) ;
4045
41- ArrayPrototypePush (
42- this . #todo,
43- y == null ? CSI `${ x + 1 } G` : CSI `${ y + 1 } ;${ x + 1 } H`
44- ) ;
46+ const data = y == null ? CSI `${ x + 1 } G` : CSI `${ y + 1 } ;${ x + 1 } H` ;
47+ if ( this . #autoCommit) process . nextTick ( ( ) => this . #stream. write ( data ) ) ;
48+ else ArrayPrototypePush ( this . #todo, data ) ;
4549
4650 return this ;
4751 }
@@ -70,7 +74,8 @@ class Readline {
7074 } else if ( dy > 0 ) {
7175 data += CSI `${ dy } B` ;
7276 }
73- ArrayPrototypePush ( this . #todo, data ) ;
77+ if ( this . #autoCommit) process . nextTick ( ( ) => this . #stream. write ( data ) ) ;
78+ else ArrayPrototypePush ( this . #todo, data ) ;
7479 }
7580 return this ;
7681 }
@@ -86,10 +91,12 @@ class Readline {
8691 clearLine ( dir ) {
8792 validateInteger ( dir , 'dir' , - 1 , 1 ) ;
8893
89- ArrayPrototypePush (
90- this . #todo,
91- dir < 0 ? kClearToLineBeginning : dir > 0 ? kClearToLineEnd : kClearLine
92- ) ;
94+ const data =
95+ dir < 0 ? kClearToLineBeginning :
96+ dir > 0 ? kClearToLineEnd :
97+ kClearLine ;
98+ if ( this . #autoCommit) process . nextTick ( ( ) => this . #stream. write ( data ) ) ;
99+ else ArrayPrototypePush ( this . #todo, data ) ;
93100 return this ;
94101 }
95102
@@ -98,7 +105,11 @@ class Readline {
98105 * @returns {Readline } this
99106 */
100107 clearScreenDown ( ) {
101- ArrayPrototypePush ( this . #todo, kClearScreenDown ) ;
108+ if ( this . #autoCommit) {
109+ process . nextTick ( ( ) => this . #stream. write ( kClearScreenDown ) ) ;
110+ } else {
111+ ArrayPrototypePush ( this . #todo, kClearScreenDown ) ;
112+ }
102113 return this ;
103114 }
104115
0 commit comments