Skip to content

Commit

Permalink
close worker on finalizatino
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Nov 14, 2024
1 parent 89ac67a commit 5e49b86
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/sql-sqlite-wasm/src/OpfsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AccessHandlePoolVFS } from "wa-sqlite/src/examples/AccessHandlePoolVFS.
* @since 1.0.0
*/
export interface OpfsWorkerConfig {
readonly port: EventTarget & Pick<MessagePort, "postMessage">
readonly port: EventTarget & Pick<MessagePort, "postMessage" | "close">
readonly dbName: string
}

Expand All @@ -23,7 +23,7 @@ export interface OpfsWorkerConfig {
*/
export const run = (
options: OpfsWorkerConfig
): Effect.Effect<never, SqlError> =>
): Effect.Effect<void, SqlError> =>
Effect.gen(function*() {
const factory = yield* Effect.promise(() => SQLiteESMFactory())
const sqlite3 = WaSqlite.Factory(factory)
Expand All @@ -37,9 +37,13 @@ export const run = (
(db) => Effect.sync(() => sqlite3.close(db))
)

return yield* Effect.async<never>((_resume) => {
return yield* Effect.async<void>((resume) => {
const onMessage = async (event: any) => {
const [id, sql, params] = event.data as [number, string, Array<any>]
if (id === -1) {
options.port.close()
return resume(Effect.void)
}
try {
const results: Array<any> = []
let columns: Array<string> | undefined
Expand Down
10 changes: 7 additions & 3 deletions packages/sql-sqlite-wasm/src/SqliteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SqliteClient, SqlError, Scope.Scope> =>
export const make = (options: SqliteClientConfig): Effect.Effect<SqliteClient, SqlError, Scope.Scope> =>
Effect.gen(function*(_) {
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
const transformRows = Statement.defaultTransforms(
Expand Down Expand Up @@ -228,8 +226,11 @@ export const makeOpfs = (
const acquireWorker = Effect.gen(function*() {
const scope = yield* Effect.scope
const readyDeferred = yield* Deferred.make<void>()

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) {
Expand All @@ -246,19 +247,22 @@ 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(() => {
worker.removeEventListener("message", onMessage)
worker.removeEventListener("error", onError)
})
)

yield* Deferred.await(readyDeferred)
return port
})
Expand Down

0 comments on commit 5e49b86

Please sign in to comment.