Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4683): make ChangeStream an async iterable #3454

Merged
merged 22 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
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