Skip to content

Commit d6461a8

Browse files
committed
chore: remove the need for unshift
1 parent 51de7d8 commit d6461a8

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

src/cmap/wire_protocol/responses.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,9 @@ function throwUnsupportedError() {
121121
}
122122

123123
export class CursorResponse extends MongoDBResponse {
124-
id: Long | null = null;
125-
ns: MongoDBNamespace | null = null;
126-
127-
documents: any | null = null;
128-
bufferForUnshift: any[] = [];
124+
public id: Long | null = null;
125+
public ns: MongoDBNamespace | null = null;
126+
public documents: any | null = null;
129127

130128
private batch: OnDemandDocument | null = null;
131129
private values: Generator<OnDemandDocument, void, void> | null = null;
@@ -161,22 +159,15 @@ export class CursorResponse extends MongoDBResponse {
161159
shift: {
162160
value: (options?: BSONSerializeOptions) => {
163161
this.iterated += 1;
164-
if (this.bufferForUnshift.length) return this.bufferForUnshift.pop();
165162
const r = this.values?.next();
166163
if (!r || r.done) return null;
167-
if (options.raw) {
164+
if (options?.raw) {
168165
return r.value.toBytes();
169166
} else {
170167
return r.value.toObject(options);
171168
}
172169
}
173170
},
174-
unshift: {
175-
value: (v: any) => {
176-
this.iterated -= 1;
177-
this.bufferForUnshift.push(v);
178-
}
179-
},
180171
clear: {
181172
value: () => {
182173
this.iterated = this.batchSize;

src/cursor/abstract_cursor.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ export abstract class AbstractCursor<
148148
[kDocuments]: {
149149
length: number;
150150
shift(bsonOptions?: any): TSchema | null;
151-
unshift(doc: TSchema): void;
152151
clear(): void;
153152
pushMany(many: Iterable<TSchema>): void;
154153
push(item: TSchema): void;
@@ -390,14 +389,7 @@ export abstract class AbstractCursor<
390389
return true;
391390
}
392391

393-
const doc = await next<TSchema>(this, { blocking: true, transform: false });
394-
395-
if (doc) {
396-
this[kDocuments].unshift(doc);
397-
return true;
398-
}
399-
400-
return false;
392+
return await next(this, { blocking: true, transform: false, hasNext: true });
401393
}
402394

403395
/** Get the next available document from the cursor, returns null if no more documents are available. */
@@ -406,7 +398,7 @@ export abstract class AbstractCursor<
406398
throw new MongoCursorExhaustedError();
407399
}
408400

409-
return await next(this, { blocking: true, transform: true });
401+
return await next(this, { blocking: true, transform: true, hasNext: false });
410402
}
411403

412404
/**
@@ -417,7 +409,7 @@ export abstract class AbstractCursor<
417409
throw new MongoCursorExhaustedError();
418410
}
419411

420-
return await next(this, { blocking: false, transform: true });
412+
return await next(this, { blocking: false, transform: true, hasNext: false });
421413
}
422414

423415
/**
@@ -726,13 +718,42 @@ async function next<T>(
726718
cursor: AbstractCursor<T>,
727719
{
728720
blocking,
729-
transform
721+
transform,
722+
hasNext
723+
}: {
724+
blocking: boolean;
725+
transform: boolean;
726+
hasNext: true;
727+
}
728+
): Promise<boolean>;
729+
730+
async function next<T>(
731+
cursor: AbstractCursor<T>,
732+
{
733+
blocking,
734+
transform,
735+
hasNext
736+
}: {
737+
blocking: boolean;
738+
transform: boolean;
739+
hasNext: false;
740+
}
741+
): Promise<T | null>;
742+
743+
async function next<T>(
744+
cursor: AbstractCursor<T>,
745+
{
746+
blocking,
747+
transform,
748+
hasNext
730749
}: {
731750
blocking: boolean;
732751
transform: boolean;
752+
hasNext: boolean;
733753
}
734-
): Promise<T | null> {
754+
): Promise<boolean | T | null> {
735755
if (cursor.closed) {
756+
if (hasNext) return false;
736757
return null;
737758
}
738759

@@ -743,6 +764,7 @@ async function next<T>(
743764
}
744765

745766
if (cursor[kDocuments].length !== 0) {
767+
if (hasNext) return true;
746768
const doc = cursor[kDocuments].shift(cursor[kOptions]);
747769

748770
if (doc != null && transform && cursor[kTransform]) {
@@ -767,6 +789,7 @@ async function next<T>(
767789
// cleanupCursor should never throw, but if it does it indicates a bug in the driver
768790
// and we should surface the error
769791
await cleanupCursor(cursor, {});
792+
if (hasNext) return false;
770793
return null;
771794
}
772795

@@ -811,10 +834,12 @@ async function next<T>(
811834
}
812835

813836
if (cursor[kDocuments].length === 0 && blocking === false) {
837+
if (hasNext) return false;
814838
return null;
815839
}
816840
} while (!cursor.isDead || cursor[kDocuments].length !== 0);
817841

842+
if (hasNext) return false;
818843
return null;
819844
}
820845

0 commit comments

Comments
 (0)