@@ -3,21 +3,25 @@ import { EventEmitter } from 'events';
3
3
import { MongoError , AnyError , isResumableError } from './error' ;
4
4
import { Cursor , CursorOptions , CursorStream , CursorStreamOptions } from './cursor/cursor' ;
5
5
import { AggregateOperation , AggregateOptions } from './operations/aggregate' ;
6
- import { loadCollection , loadDb , loadMongoClient } from './dynamic_loaders' ;
7
6
import {
8
7
relayEvents ,
9
8
maxWireVersion ,
10
9
calculateDurationInMs ,
11
10
now ,
12
11
maybePromise ,
13
12
MongoDBNamespace ,
14
- Callback
13
+ Callback ,
14
+ getTopology
15
15
} from './utils' ;
16
16
import type { ReadPreference } from './read_preference' ;
17
17
import type { Timestamp , Document } from './bson' ;
18
18
import type { Topology } from './sdam/topology' ;
19
19
import type { OperationParent } from './operations/command' ;
20
20
import type { CollationOptions } from './cmap/wire_protocol/write_command' ;
21
+ import { MongoClient } from './mongo_client' ;
22
+ import { Db } from './db' ;
23
+ import { Collection } from './collection' ;
24
+
21
25
const kResumeQueue = Symbol ( 'resumeQueue' ) ;
22
26
const kCursorStream = Symbol ( 'cursorStream' ) ;
23
27
@@ -172,10 +176,9 @@ export class ChangeStreamStream extends CursorStream {
172
176
export class ChangeStream extends EventEmitter {
173
177
pipeline : Document [ ] ;
174
178
options : ChangeStreamOptions ;
175
- parent : OperationParent ;
179
+ parent : MongoClient | Db | Collection ;
176
180
namespace : MongoDBNamespace ;
177
181
type : symbol ;
178
- topology : Topology ;
179
182
cursor ?: ChangeStreamCursor ;
180
183
closed : boolean ;
181
184
streamOptions ?: CursorStreamOptions ;
@@ -212,31 +215,23 @@ export class ChangeStream extends EventEmitter {
212
215
) {
213
216
super ( ) ;
214
217
215
- const Collection = loadCollection ( ) ;
216
- const Db = loadDb ( ) ;
217
- const MongoClient = loadMongoClient ( ) ;
218
-
219
218
this . pipeline = pipeline ;
220
219
this . options = options ;
221
220
222
- this . parent = parent ;
223
- this . namespace = parent . s . namespace ;
224
221
if ( parent instanceof Collection ) {
225
222
this . type = CHANGE_DOMAIN_TYPES . COLLECTION ;
226
- this . topology = parent . s . db . s . topology ;
227
223
} else if ( parent instanceof Db ) {
228
224
this . type = CHANGE_DOMAIN_TYPES . DATABASE ;
229
- this . topology = parent . s . topology ;
230
225
} else if ( parent instanceof MongoClient ) {
231
226
this . type = CHANGE_DOMAIN_TYPES . CLUSTER ;
232
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
233
- this . topology = parent . topology ! ;
234
227
} else {
235
228
throw new TypeError (
236
229
'parent provided to ChangeStream constructor is not an instance of Collection, Db, or MongoClient'
237
230
) ;
238
231
}
239
232
233
+ this . parent = parent ;
234
+ this . namespace = parent . s . namespace ;
240
235
if ( ! this . options . readPreference && parent . readPreference ) {
241
236
this . options . readPreference = parent . readPreference ;
242
237
}
@@ -483,7 +478,7 @@ function createChangeStreamCursor(
483
478
const pipeline = [ { $changeStream : changeStreamStageOptions } as Document ] . concat ( self . pipeline ) ;
484
479
const cursorOptions = applyKnownOptions ( { } , options , CURSOR_OPTIONS ) ;
485
480
const changeStreamCursor = new ChangeStreamCursor (
486
- self . topology ,
481
+ getTopology ( self . parent ) ,
487
482
new AggregateOperation ( self . parent , pipeline , options ) ,
488
483
cursorOptions
489
484
) ;
@@ -593,7 +588,7 @@ function processNewChange(
593
588
}
594
589
595
590
function processError ( changeStream : ChangeStream , error : AnyError , callback ?: Callback ) {
596
- const topology = changeStream . topology ;
591
+ const topology = getTopology ( changeStream . parent ) ;
597
592
const cursor = changeStream . cursor ;
598
593
599
594
// If the change stream has been closed explicitly, do not process error.
0 commit comments