Skip to content

Commit e79db9b

Browse files
refactor: remove unnecessary variables from ReadableCursorStream
the _initialized and _needToClose variables were unnecessary. - _initialized existed only to conditionally set _needToClose when the stream was read from for the first time - _needToClose is unnecessary, because cursor.close is a no-op if the cursor is already closed.
1 parent 4c37cb1 commit e79db9b

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,7 @@ export function assertUninitialized(cursor: AbstractCursor): void {
860860

861861
class ReadableCursorStream extends Readable {
862862
private _cursor: AbstractCursor;
863-
864-
private _initialized = false;
865-
private _reading = false;
866-
private _needToClose = true; // NOTE: we must close the cursor if we never read from it, use `_construct` in future node versions
863+
private _readInProgress = false;
867864

868865
constructor(cursor: AbstractCursor) {
869866
super({
@@ -876,30 +873,18 @@ class ReadableCursorStream extends Readable {
876873

877874
// eslint-disable-next-line @typescript-eslint/no-unused-vars
878875
override _read(size: number): void {
879-
if (this._initialized === false) {
880-
this._needToClose = false;
881-
this._initialized = true;
882-
}
883-
884-
if (!this._reading) {
885-
this._reading = true;
876+
if (!this._readInProgress) {
877+
this._readInProgress = true;
886878
this._readNext();
887879
}
888880
}
889881

890882
override _destroy(error: Error | null, callback: (error?: Error | null) => void): void {
891-
if (this._needToClose) {
892-
this._cursor.close(err => process.nextTick(callback, err || error));
893-
} else {
894-
callback(error);
895-
}
883+
this._cursor.close(err => process.nextTick(callback, err || error));
896884
}
897885

898886
private _readNext() {
899-
this._needToClose = false;
900887
next(this._cursor, true, (err, result) => {
901-
this._needToClose = err ? !this._cursor.closed : result != null;
902-
903888
if (err) {
904889
// NOTE: This is questionable, but we have a test backing the behavior. It seems the
905890
// desired behavior is that a stream ends cleanly when a user explicitly closes
@@ -931,7 +916,7 @@ class ReadableCursorStream extends Readable {
931916
return this._readNext();
932917
}
933918

934-
this._reading = false;
919+
this._readInProgress = false;
935920
}
936921
});
937922
}

0 commit comments

Comments
 (0)