Skip to content

Commit 689982b

Browse files
committed
fixup! readline: introduce promise-based API
1 parent 1e348f2 commit 689982b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

doc/api/readline.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ was passed to the constructor.
682682
added: REPLACEME
683683
-->
684684

685-
* Returns: {Promise}
685+
* Returns: this
686686

687687
The `rl.rollback` methods clears the internal list of pending actions without
688688
sending it to the associated `stream`.

lib/internal/readline/promises.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
ArrayPrototypeJoin,
55
ArrayPrototypePush,
66
Promise,
7-
PromiseResolve,
87
} = primordials;
98

109
const { CSI } = require('internal/readline/utils');
@@ -38,6 +37,7 @@ class Readline {
3837
* Moves the cursor to the x and y coordinate on the given stream.
3938
* @param {integer} x
4039
* @param {integer} [y]
40+
* @returns {Readline} this
4141
*/
4242
cursorTo(x, y = undefined) {
4343
validateInteger(x, 'x');
@@ -54,6 +54,7 @@ class Readline {
5454
* Moves the cursor relative to its current location.
5555
* @param {integer} dx
5656
* @param {integer} dy
57+
* @returns {Readline} this
5758
*/
5859
moveCursor(dx, dy) {
5960
if (dx || dy) {
@@ -80,10 +81,12 @@ class Readline {
8081
}
8182

8283
/**
83-
* Clears the current line the cursor is on:
84+
* Clears the current line the cursor is on.
85+
* @param {-1|0|1} dir Direction to clear:
8486
* -1 for left of the cursor
8587
* +1 for right of the cursor
8688
* 0 for the entire line
89+
* @returns {Readline} this
8790
*/
8891
clearLine(dir) {
8992
validateInteger(dir, 'dir', -1, 1);
@@ -99,6 +102,7 @@ class Readline {
99102

100103
/**
101104
* Clears the screen from the current position of the cursor down.
105+
* @returns {Readline} this
102106
*/
103107
clearScreenDown() {
104108
if (this.#autoCommit) {
@@ -109,15 +113,27 @@ class Readline {
109113
return this;
110114
}
111115

116+
/**
117+
* Sends all the pending actions to the associated `stream` and clears the
118+
* internal list of pending actions.
119+
* @returns {Promise<void>} Resolves when all pending actions have been
120+
* flushed to the associated `stream`.
121+
*/
112122
commit() {
113123
return new Promise((resolve) => {
114124
this.#stream.write(ArrayPrototypeJoin(this.#todo, ''), resolve);
115125
this.#todo = [];
116126
});
117127
}
128+
129+
/**
130+
* Clears the internal list of pending actions without sending it to the
131+
* associated `stream`.
132+
* @returns {Readline} this
133+
*/
118134
rollback() {
119135
this.#todo = [];
120-
return PromiseResolve();
136+
return this;
121137
}
122138
}
123139

0 commit comments

Comments
 (0)