Skip to content

Commit ab4ab8a

Browse files
author
Daniele Briggi
committed
fix(close): enqueue operation
1 parent ed3738a commit ab4ab8a

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.437",
3+
"version": "1.0.438",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/database.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,17 @@ export class Database extends EventEmitter {
128128

129129
// we don't wont to silently open a new connection after a disconnession
130130
if (this.connection && this.connection.connected) {
131-
this.connection.sendCommands(command, callback)
131+
this.connection.sendCommands(command, (error, results) => {
132+
callback?.call(this, null, results)
133+
done(error)
134+
})
132135
} else {
133136
error = new SQLiteCloudError('Connection unavailable. Maybe it got disconnected?', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' })
134-
this.handleError(error, callback)
137+
this.handleError(error, (error: Error | null) => {
138+
callback?.call(this, error, null)
139+
done(error)
140+
})
135141
}
136-
137-
done(error)
138142
})
139143
}
140144

@@ -382,11 +386,15 @@ export class Database extends EventEmitter {
382386
* parameters is emitted, regardless of whether a callback was provided or not.
383387
*/
384388
public close(callback?: ConnectionCallback): void {
385-
this.operations.clear()
386-
this.connection?.close()
389+
this.operations.enqueue(done => {
390+
this.connection?.close()
391+
392+
callback?.call(this, null)
393+
this.emitEvent('close')
387394

388-
callback?.call(this, null)
389-
this.emitEvent('close')
395+
this.operations.clear()
396+
done(null)
397+
})
390398
}
391399

392400
/**

test/database.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,33 @@ describe('Database.get', () => {
246246
})
247247
})
248248
})
249+
250+
it.only('close() is executed after the previous commands', done => {
251+
// the database enqueue the close command
252+
const chinook = getChinookDatabase()
253+
chinook.get('SELECT * FROM tracks', (err: Error, row?: SQLiteCloudRow) => {
254+
expect(err).toBeNull()
255+
expect(row).toBeDefined()
256+
expect(row).toMatchObject({
257+
AlbumId: 1,
258+
Bytes: 11170334,
259+
Composer: 'Angus Young, Malcolm Young, Brian Johnson',
260+
GenreId: 1,
261+
MediaTypeId: 1,
262+
Milliseconds: 343719,
263+
Name: 'For Those About To Rock (We Salute You)',
264+
TrackId: 1,
265+
UnitPrice: 0.99
266+
})
267+
})
268+
269+
// call close() right after the execution
270+
// of the query not in its callback
271+
chinook.close(error => {
272+
expect(error).toBeNull()
273+
done()
274+
})
275+
})
249276
})
250277

251278
describe('Database.each', () => {

0 commit comments

Comments
 (0)