Skip to content

Commit 07d3880

Browse files
fix: reset worker database reference after close (#638)
Follow up on the null-reset proposed by WilliamStephens in #216; preserve the explicit reopening behavior added in #379. Assisted-by: Hermes Signed-off-by: sergioperezcheco <checo520@outlook.com>
1 parent 0568ac9 commit 07d3880

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎src/worker.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function onModuleReady(SQL) {
7171
case "close":
7272
if (db) {
7373
db.close();
74+
db = null;
7475
}
7576
return postMessage({
7677
id: data["id"]

‎test/test_worker.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ exports.test = async function test(SQL, assert) {
134134
for (let i = 0; i < header.length; i++) actual += String.fromCharCode(data.buffer[i]);
135135
assert.equal(actual, header, 'Data returned is an SQLite database file');
136136

137+
// Closing returns the worker to its initial, lazily initialized state.
138+
await worker.postMessage({ action: "close" });
139+
await worker.postMessage({ action: "close" });
140+
data = await worker.postMessage({
141+
action: "exec",
142+
sql: "SELECT count(*) AS count FROM sqlite_master WHERE name = 'test'"
143+
});
144+
assert.deepEqual(data.results, [{ columns: ['count'], values: [[0]] }],
145+
'exec after close creates a fresh database');
146+
await worker.postMessage({ action: "close" });
147+
data = await worker.postMessage({ action: "each", sql: "SELECT 7 AS value" });
148+
assert.deepEqual(data.row, { value: 7 },
149+
'each after close creates a fresh database');
150+
137151
// test worker properly opens db after closing
138152
await worker.postMessage({ action: "close" });
139153
await worker.postMessage({ action: "open" });

0 commit comments

Comments
 (0)