Skip to content

Commit

Permalink
feat(NODE-4683): make ChangeStream an async iterable (mongodb#3454)
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina authored and ZLY201 committed Nov 5, 2022
1 parent 1407847 commit b9d6940
Show file tree
Hide file tree
Showing 3 changed files with 393 additions and 25 deletions.
20 changes: 20 additions & 0 deletions src/change_stream.ts
Expand Up @@ -737,6 +737,26 @@ export class ChangeStream<
}, callback);
}

async *[Symbol.asyncIterator](): AsyncGenerator<TChange, void, void> {
if (this.closed) {
return;
}

try {
// Change streams run indefinitely as long as errors are resumable
// So the only loop breaking condition is if `next()` throws
while (true) {
yield await this.next();
}
} finally {
try {
await this.close();
} catch {
// we're not concerned with errors from close()
}
}
}

/** Is the cursor closed */
get closed(): boolean {
return this[kClosed] || this.cursor.closed;
Expand Down

0 comments on commit b9d6940

Please sign in to comment.