@@ -148,8 +148,7 @@ export default class MongoMemoryReplSet extends events.EventEmitter {
148148 if ( this . _state !== 'stopped' ) {
149149 throw new Error ( `Already in 'init' or 'running' state. Use opts.debug = true for more info.` ) ;
150150 }
151- this . _state = 'init' ;
152- this . emit ( 'state' , 'init' ) ;
151+ this . emit ( ( this . _state = 'init' ) ) ;
153152 this . debug ( 'init' ) ;
154153 // Any servers defined within `opts.instanceOpts` should be started first as
155154 // the user could have specified a `dbPath` in which case we would want to perform
@@ -176,18 +175,21 @@ export default class MongoMemoryReplSet extends events.EventEmitter {
176175 if ( this . _state === 'stopped' ) return false ;
177176 const servers = this . servers ;
178177 this . servers = [ ] ;
179- this . _state = 'stopped' ;
180178 return Promise . all ( servers . map ( s => s . stop ( ) ) )
181- . then ( ( ) => true )
179+ . then ( ( ) => {
180+ this . emit ( ( this . _state = 'stopped' ) ) ;
181+ return true ;
182+ } )
182183 . catch ( err => {
183184 this . debug ( err ) ;
185+ this . emit ( ( this . _state = 'stopped' ) , err ) ;
184186 return false ;
185187 } ) ;
186188 }
187189
188190 async waitUntilRunning ( ) {
189191 if ( this . _state === 'running' ) return ;
190- await new Promise ( resolve => this . on ( 'state ', state => state === 'running' && resolve ( ) ) ) ;
192+ await new Promise ( resolve => this . once ( 'running ', ( ) => resolve ( ) ) ) ;
191193 }
192194
193195 /**
@@ -218,8 +220,7 @@ export default class MongoMemoryReplSet extends events.EventEmitter {
218220 await admin . command ( { replSetInitiate : rsConfig } ) ;
219221 this . debug ( 'Waiting for replica set to have a PRIMARY member.' ) ;
220222 await this . _waitForPrimary ( admin ) ;
221- this . _state = 'running' ;
222- this . emit ( 'state' , 'running' ) ;
223+ this . emit ( ( this . _state = 'running' ) ) ;
223224 this . debug ( 'running' ) ;
224225 } finally {
225226 await conn . close ( ) ;
0 commit comments