Skip to content

Commit b449bd0

Browse files
Merge pull request #230 from sqlitecloud/#225-close-enqueue-the-close-operation
fix(close): enqueue operation
2 parents ed3738a + 85f6b6f commit b449bd0

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

test/connection-ws.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
* connection-ws.test.ts - test connection via socket.io based gateway
33
*/
44

5-
import { SQLiteCloudError } from '../src/index'
65
import { SQLiteCloudConnection } from '../src/drivers/connection'
76
import { SQLiteCloudWebsocketConnection } from '../src/drivers/connection-ws'
7+
import { SQLiteCloudCommand } from '../src/drivers/types'
8+
import { SQLiteCloudError } from '../src/index'
89
import {
9-
//
10-
CHINOOK_DATABASE_URL,
11-
LONG_TIMEOUT,
10+
EXPECT_SPEED_MS,
1211
getChinookConfig,
1312
getChinookWebsocketConnection,
14-
WARN_SPEED_MS,
15-
EXPECT_SPEED_MS
13+
LONG_TIMEOUT,
14+
WARN_SPEED_MS
1615
} from './shared'
17-
import { SQLiteCloudCommand } from '../src/drivers/types'
1816

1917
describe('connection-ws', () => {
2018
let chinook: SQLiteCloudConnection

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('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)