Skip to content

Commit

Permalink
fix: initialization of cursors with values
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Oct 21, 2022
1 parent 8ef1fe6 commit c431fe7
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/cursor/abstract_cursor.ts
Expand Up @@ -681,7 +681,7 @@ export abstract class AbstractCursor<
* a significant refactor.
*/
[kInit](callback: Callback<TSchema | null>): void {
this._initialize(this[kSession], (err, state) => {
this._initialize(this[kSession], (error, state) => {
if (state) {
const response = state.response;
this[kServer] = state.server;
Expand Down Expand Up @@ -713,8 +713,12 @@ export abstract class AbstractCursor<
// the cursor is now initialized, even if an error occurred or it is dead
this[kInitialized] = true;

if (err || cursorIsDead(this)) {
return cleanupCursor(this, { error: err }, () => callback(err, nextDocument(this)));
if (error) {
return cleanupCursor(this, { error }, () => callback(error, undefined));
}

if (cursorIsDead(this)) {
return cleanupCursor(this, undefined, () => callback());
}

callback();
Expand Down Expand Up @@ -775,14 +779,8 @@ export function next<T>(

if (cursorId == null) {
// All cursors must operate within a session, one must be made implicitly if not explicitly provided
cursor[kInit]((err, value) => {
cursor[kInit](err => {
if (err) return callback(err);
// Intentional strict null check, because users can map cursors to falsey values.
// We allow mapping to all values except for null.
// eslint-disable-next-line no-restricted-syntax
if (value !== null) {
return callback(undefined, value);
}
return next(cursor, blocking, callback);
});

Expand Down

0 comments on commit c431fe7

Please sign in to comment.