3
3
const ChangeStream = require ( './change_stream' ) ;
4
4
const Db = require ( './db' ) ;
5
5
const EventEmitter = require ( 'events' ) . EventEmitter ;
6
- const executeOperation = require ( './operations/execute_operation' ) ;
7
6
const inherits = require ( 'util' ) . inherits ;
8
7
const MongoError = require ( './core' ) . MongoError ;
9
8
const deprecate = require ( 'util' ) . deprecate ;
10
9
const WriteConcern = require ( './write_concern' ) ;
11
10
const MongoDBNamespace = require ( './utils' ) . MongoDBNamespace ;
12
11
const ReadPreference = require ( './core/topologies/read_preference' ) ;
13
-
14
- // Operations
15
- const ConnectOperation = require ( './operations/connect' ) ;
16
- const CloseOperation = require ( './operations/close' ) ;
12
+ const maybePromise = require ( './utils' ) . maybePromise ;
13
+ const NativeTopology = require ( './topologies/native_topology' ) ;
14
+ const connect = require ( './operations/connect' ) . connect ;
15
+ const validOptions = require ( './operations/connect' ) . validOptions ;
17
16
18
17
/**
19
18
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
@@ -214,9 +213,16 @@ MongoClient.prototype.connect = function(callback) {
214
213
throw new TypeError ( '`connect` only accepts a callback' ) ;
215
214
}
216
215
217
- const operation = new ConnectOperation ( this ) ;
216
+ const client = this ;
217
+ return maybePromise ( callback , cb => {
218
+ const err = validOptions ( client . s . options ) ;
219
+ if ( err ) return cb ( err ) ;
218
220
219
- return executeOperation ( this , operation , callback ) ;
221
+ connect ( client , client . s . url , client . s . options , err => {
222
+ if ( err ) return cb ( err ) ;
223
+ cb ( null , client ) ;
224
+ } ) ;
225
+ } ) ;
220
226
} ;
221
227
222
228
MongoClient . prototype . logout = deprecate ( function ( options , callback ) {
@@ -232,9 +238,41 @@ MongoClient.prototype.logout = deprecate(function(options, callback) {
232
238
* @return {Promise } returns Promise if no callback passed
233
239
*/
234
240
MongoClient . prototype . close = function ( force , callback ) {
235
- if ( typeof force === 'function' ) ( callback = force ) , ( force = false ) ;
236
- const operation = new CloseOperation ( this , force ) ;
237
- return executeOperation ( this , operation , callback ) ;
241
+ if ( typeof force === 'function' ) {
242
+ callback = force ;
243
+ force = false ;
244
+ }
245
+
246
+ const client = this ;
247
+ return maybePromise ( callback , cb => {
248
+ const completeClose = err => {
249
+ client . emit ( 'close' , client ) ;
250
+
251
+ if ( ! ( client . topology instanceof NativeTopology ) ) {
252
+ for ( const item of client . s . dbCache ) {
253
+ item [ 1 ] . emit ( 'close' , client ) ;
254
+ }
255
+ }
256
+
257
+ client . removeAllListeners ( 'close' ) ;
258
+ cb ( err ) ;
259
+ } ;
260
+
261
+ if ( client . topology == null ) {
262
+ completeClose ( ) ;
263
+ return ;
264
+ }
265
+
266
+ client . topology . close ( force , err => {
267
+ const autoEncrypter = client . topology . s . options . autoEncrypter ;
268
+ if ( ! autoEncrypter ) {
269
+ completeClose ( err ) ;
270
+ return ;
271
+ }
272
+
273
+ autoEncrypter . teardown ( force , err2 => completeClose ( err || err2 ) ) ;
274
+ } ) ;
275
+ } ) ;
238
276
} ;
239
277
240
278
/**
0 commit comments