Skip to content

Commit c84a8e0

Browse files
committed
Ensuring dedicated worker is closed on client close call.
1 parent 61a460f commit c84a8e0

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/web/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type WrappedWorkerConnectionOptions<Config extends ResolvedWebSQLOpenOpti
1919
* Need a remote in order to keep a reference to the Proxied worker
2020
*/
2121
remote: Comlink.Remote<OpenAsyncDatabaseConnection<Config>>;
22+
onClose?: () => void;
2223
};
2324

2425
/**
@@ -59,6 +60,7 @@ export class WorkerWrappedAsyncDatabaseConnection<Config extends ResolvedWebSQLO
5960
async close(): Promise<void> {
6061
await this.baseConnection.close();
6162
this.options.remote[Comlink.releaseProxy]();
63+
this.options.onClose?.();
6264
}
6365

6466
execute(sql: string, params?: any[]): Promise<ProxiedQueryResult> {

packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
7777
flags: this.resolvedFlags,
7878
encryptionKey: encryptionKey
7979
}),
80-
identifier: this.options.dbFilename
80+
identifier: this.options.dbFilename,
81+
onClose: () => {
82+
if (workerPort instanceof Worker) {
83+
workerPort.terminate();
84+
} else {
85+
workerPort.close();
86+
}
87+
}
8188
});
8289
} else {
8390
// Don't use a web worker

0 commit comments

Comments
 (0)