From 5e49b8658b3ea4dd3bd656cf2c38841d9b638e3c Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 14 Nov 2024 09:35:59 +0100 Subject: [PATCH] close worker on finalizatino --- packages/sql-sqlite-wasm/src/OpfsWorker.ts | 10 +++++++--- packages/sql-sqlite-wasm/src/SqliteClient.ts | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/sql-sqlite-wasm/src/OpfsWorker.ts b/packages/sql-sqlite-wasm/src/OpfsWorker.ts index 4cbd4d2766..6c2241f638 100644 --- a/packages/sql-sqlite-wasm/src/OpfsWorker.ts +++ b/packages/sql-sqlite-wasm/src/OpfsWorker.ts @@ -13,7 +13,7 @@ import { AccessHandlePoolVFS } from "wa-sqlite/src/examples/AccessHandlePoolVFS. * @since 1.0.0 */ export interface OpfsWorkerConfig { - readonly port: EventTarget & Pick + readonly port: EventTarget & Pick readonly dbName: string } @@ -23,7 +23,7 @@ export interface OpfsWorkerConfig { */ export const run = ( options: OpfsWorkerConfig -): Effect.Effect => +): Effect.Effect => Effect.gen(function*() { const factory = yield* Effect.promise(() => SQLiteESMFactory()) const sqlite3 = WaSqlite.Factory(factory) @@ -37,9 +37,13 @@ export const run = ( (db) => Effect.sync(() => sqlite3.close(db)) ) - return yield* Effect.async((_resume) => { + return yield* Effect.async((resume) => { const onMessage = async (event: any) => { const [id, sql, params] = event.data as [number, string, Array] + if (id === -1) { + options.port.close() + return resume(Effect.void) + } try { const results: Array = [] let columns: Array | undefined diff --git a/packages/sql-sqlite-wasm/src/SqliteClient.ts b/packages/sql-sqlite-wasm/src/SqliteClient.ts index 5425b012b2..d8eab78a43 100644 --- a/packages/sql-sqlite-wasm/src/SqliteClient.ts +++ b/packages/sql-sqlite-wasm/src/SqliteClient.ts @@ -94,9 +94,7 @@ const registered = globalValue("@effect/sql-sqlite-wasm/registered", () => new S * @category constructor * @since 1.0.0 */ -export const make = ( - options: SqliteClientConfig -): Effect.Effect => +export const make = (options: SqliteClientConfig): Effect.Effect => Effect.gen(function*(_) { const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) const transformRows = Statement.defaultTransforms( @@ -228,8 +226,11 @@ export const makeOpfs = ( const acquireWorker = Effect.gen(function*() { const scope = yield* Effect.scope const readyDeferred = yield* Deferred.make() + const worker = yield* options.worker const port = "port" in worker ? worker.port : worker + yield* Scope.addFinalizer(scope, Effect.sync(() => port.postMessage([-1]))) + const onMessage = (event: any) => { const [id, error, results] = event.data if (id === -1) { @@ -246,12 +247,14 @@ export const makeOpfs = ( } } port.addEventListener("message", onMessage) + function onError() { Effect.runFork(ScopedRef.set(workerRef, acquireWorker)) } if ("onerror" in worker) { worker.addEventListener("error", onError) } + yield* Scope.addFinalizer( scope, Effect.sync(() => { @@ -259,6 +262,7 @@ export const makeOpfs = ( worker.removeEventListener("error", onError) }) ) + yield* Deferred.await(readyDeferred) return port })