@@ -96,6 +96,7 @@ export class Database extends EventEmitter {
9696 } )
9797 . catch ( error => {
9898 this . handleError ( error , callback )
99+ this . close ( )
99100 done ( error )
100101 } )
101102 } )
@@ -116,6 +117,7 @@ export class Database extends EventEmitter {
116117 } )
117118 . catch ( error => {
118119 this . handleError ( error , callback )
120+ this . close ( )
119121 done ( error )
120122 } )
121123 } )
@@ -128,21 +130,20 @@ export class Database extends EventEmitter {
128130
129131 // we don't wont to silently open a new connection after a disconnession
130132 if ( this . connection && this . connection . connected ) {
131- this . connection . sendCommands ( command , callback )
133+ this . connection . sendCommands ( command , ( error , results ) => {
134+ callback ?. call ( this , error , results )
135+ done ( error )
136+ } )
132137 } else {
133138 error = new SQLiteCloudError ( 'Connection unavailable. Maybe it got disconnected?' , { errorCode : 'ERR_CONNECTION_NOT_ESTABLISHED' } )
134- this . handleError ( error , callback )
139+ callback ?. call ( this , error , null )
140+ done ( error )
135141 }
136-
137- done ( error )
138142 } )
139143 }
140144
141145 /** Handles an error by closing the connection, calling the callback and/or emitting an error event */
142146 private handleError ( error : Error , callback ?: ConnectionCallback ) : void {
143- // an errored connection is thrown out
144- this . connection ?. close ( )
145-
146147 if ( callback ) {
147148 callback . call ( this , error )
148149 } else {
@@ -382,11 +383,15 @@ export class Database extends EventEmitter {
382383 * parameters is emitted, regardless of whether a callback was provided or not.
383384 */
384385 public close ( callback ?: ConnectionCallback ) : void {
385- this . operations . clear ( )
386- this . connection ?. close ( )
386+ this . operations . enqueue ( done => {
387+ this . connection ?. close ( )
388+
389+ callback ?. call ( this , null )
390+ this . emitEvent ( 'close' )
387391
388- callback ?. call ( this , null )
389- this . emitEvent ( 'close' )
392+ this . operations . clear ( )
393+ done ( null )
394+ } )
390395 }
391396
392397 /**
0 commit comments