Skip to content

Commit

Permalink
fixup! readline: introduce promise-based API
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Sep 5, 2021
1 parent 1e348f2 commit 689982b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ was passed to the constructor.
added: REPLACEME
-->

* Returns: {Promise}
* Returns: this

The `rl.rollback` methods clears the internal list of pending actions without
sending it to the associated `stream`.
Expand Down
22 changes: 19 additions & 3 deletions lib/internal/readline/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
ArrayPrototypeJoin,
ArrayPrototypePush,
Promise,
PromiseResolve,
} = primordials;

const { CSI } = require('internal/readline/utils');
Expand Down Expand Up @@ -38,6 +37,7 @@ class Readline {
* Moves the cursor to the x and y coordinate on the given stream.
* @param {integer} x
* @param {integer} [y]
* @returns {Readline} this
*/
cursorTo(x, y = undefined) {
validateInteger(x, 'x');
Expand All @@ -54,6 +54,7 @@ class Readline {
* Moves the cursor relative to its current location.
* @param {integer} dx
* @param {integer} dy
* @returns {Readline} this
*/
moveCursor(dx, dy) {
if (dx || dy) {
Expand All @@ -80,10 +81,12 @@ class Readline {
}

/**
* Clears the current line the cursor is on:
* Clears the current line the cursor is on.
* @param {-1|0|1} dir Direction to clear:
* -1 for left of the cursor
* +1 for right of the cursor
* 0 for the entire line
* @returns {Readline} this
*/
clearLine(dir) {
validateInteger(dir, 'dir', -1, 1);
Expand All @@ -99,6 +102,7 @@ class Readline {

/**
* Clears the screen from the current position of the cursor down.
* @returns {Readline} this
*/
clearScreenDown() {
if (this.#autoCommit) {
Expand All @@ -109,15 +113,27 @@ class Readline {
return this;
}

/**
* Sends all the pending actions to the associated `stream` and clears the
* internal list of pending actions.
* @returns {Promise<void>} Resolves when all pending actions have been
* flushed to the associated `stream`.
*/
commit() {
return new Promise((resolve) => {
this.#stream.write(ArrayPrototypeJoin(this.#todo, ''), resolve);
this.#todo = [];
});
}

/**
* Clears the internal list of pending actions without sending it to the
* associated `stream`.
* @returns {Readline} this
*/
rollback() {
this.#todo = [];
return PromiseResolve();
return this;
}
}

Expand Down

0 comments on commit 689982b

Please sign in to comment.