@@ -22,6 +22,7 @@ function ZongJi(dsn) {
2222 this . ctrlCallbacks = [ ] ;
2323 this . tableMap = { } ;
2424 this . ready = false ;
25+ this . stopped = false ;
2526 this . useChecksum = false ;
2627
2728 this . _establishConnection ( dsn ) ;
@@ -192,6 +193,9 @@ ZongJi.prototype.start = function (options = {}) {
192193 this . _filters ( options ) ;
193194
194195 const testChecksum = ( resolve , reject ) => {
196+ if ( this . stopped ) {
197+ resolve ( ) ;
198+ }
195199 this . _isChecksumEnabled ( ( err , checksumEnabled ) => {
196200 if ( err ) {
197201 reject ( err ) ;
@@ -203,6 +207,9 @@ ZongJi.prototype.start = function (options = {}) {
203207 } ;
204208
205209 const findBinlogEnd = ( resolve , reject ) => {
210+ if ( this . stopped ) {
211+ resolve ( ) ;
212+ }
206213 this . _findBinlogEnd ( ( err , result ) => {
207214 if ( err ) {
208215 return reject ( err ) ;
@@ -240,9 +247,8 @@ ZongJi.prototype.start = function (options = {}) {
240247 this . emit ( 'binlog' , event ) ;
241248 this . connection . resume ( ) ;
242249 } ) ;
243- return ;
244250 }
245- break ;
251+ return ;
246252 }
247253 case 'Rotate' :
248254 if ( this . options . filename !== event . binlogName ) {
@@ -263,25 +269,41 @@ ZongJi.prototype.start = function (options = {}) {
263269 Promise . all ( promises )
264270 . then ( ( ) => {
265271 this . BinlogClass = initBinlogClass ( this ) ;
266- this . ready = true ;
267- this . emit ( 'ready' ) ;
268-
269- this . connection . _protocol . _enqueue ( new this . BinlogClass ( binlogHandler ) ) ;
272+ if ( ! this . stopped ) {
273+ this . connection . _protocol . _enqueue ( new this . BinlogClass ( binlogHandler ) ) ;
274+ this . ready = true ;
275+ this . emit ( 'ready' ) ;
276+ }
270277 } )
271278 . catch ( ( err ) => {
272279 this . emit ( 'error' , err ) ;
273280 } ) ;
274281} ;
275282
276283ZongJi . prototype . stop = function ( ) {
277- // Binary log connection does not end with destroy()
278- this . connection . destroy ( ) ;
279- this . ctrlConnection . query ( 'KILL ' + this . connection . threadId , ( ) => {
280- if ( this . ctrlConnectionOwner ) {
281- this . ctrlConnection . destroy ( ) ;
282- }
283- this . emit ( 'stopped' ) ;
284- } ) ;
284+ if ( ! this . stopped ) {
285+ this . stopped = true ;
286+ // Binary log connection does not end with destroy()
287+ this . connection . destroy ( ) ;
288+ this . ctrlConnection . query ( 'KILL ' + this . connection . threadId , ( err , result ) => {
289+ if ( this . ctrlConnectionOwner ) {
290+ this . ctrlConnection . destroy ( ) ;
291+ }
292+ this . emit ( 'stopped' ) ;
293+ } ) ;
294+ }
295+ } ;
296+
297+ ZongJi . prototype . pause = function ( ) {
298+ if ( ! this . stopped ) {
299+ this . connection . pause ( ) ;
300+ }
301+ } ;
302+
303+ ZongJi . prototype . resume = function ( ) {
304+ if ( ! this . stopped ) {
305+ this . connection . resume ( ) ;
306+ }
285307} ;
286308
287309// It includes every events by default.
@@ -312,3 +334,4 @@ ZongJi.prototype._skipSchema = function (database, table) {
312334} ;
313335
314336module . exports = ZongJi ;
337+ module . exports . ZongJi = ZongJi ;
0 commit comments