( options: SqliteClientConfig )
| 298 | * @since 4.0.0 |
| 299 | */ |
| 300 | export const make = ( |
| 301 | options: SqliteClientConfig |
| 302 | ): Effect.Effect<SqliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 303 | Effect.gen(function*() { |
| 304 | const reactivity = yield* Reactivity.Reactivity |
| 305 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 306 | const transformRows = options.transformResultNames ? |
| 307 | Statement.defaultTransforms(options.transformResultNames).array : |
| 308 | undefined |
| 309 | const makeConnection = Effect.gen(function*() { |
| 310 | let currentId = 0 |
| 311 | const pending = new Map<number, (effect: Exit.Exit<any, SqlError>) => void>() |
| 312 | const scope = yield* Effect.scope |
| 313 | const readyDeferred = yield* Deferred.make<void>() |
| 314 | |
| 315 | const worker = yield* options.worker |
| 316 | const port = "port" in worker ? worker.port : worker |
| 317 | const postMessage = (message: OpfsWorkerMessage, transferables?: ReadonlyArray<any>) => |
| 318 | port.postMessage(message, transferables as any) |
| 319 | |
| 320 | yield* Scope.addFinalizer(scope, Effect.sync(() => postMessage(["close"]))) |
| 321 | |
| 322 | const onMessage = (event: any) => { |
| 323 | const [id, error, results] = event.data |
| 324 | if (id === "ready") { |
| 325 | Deferred.doneUnsafe(readyDeferred, Exit.void) |
| 326 | return |
| 327 | } else if (id === "update_hook") { |
| 328 | reactivity.invalidateUnsafe({ [error]: [results] }) |
| 329 | return |
| 330 | } else { |
| 331 | const resume = pending.get(id) |
| 332 | if (!resume) return |
| 333 | pending.delete(id) |
| 334 | if (error) { |
| 335 | resume( |
| 336 | Exit.fail( |
| 337 | new SqlError({ reason: classifyError(error as string, "Failed to execute statement", "execute") }) |
| 338 | ) |
| 339 | ) |
| 340 | } else { |
| 341 | resume(Exit.succeed(results)) |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | port.addEventListener("message", onMessage) |
| 346 | |
| 347 | function onError(cause: Event) { |
| 348 | const exit = Exit.fail( |
| 349 | new SqlError({ reason: classifyError(cause, "SQLite WASM worker failed", "worker") }) |
| 350 | ) |
| 351 | const requests = Array.from(pending.values()) |
| 352 | pending.clear() |
| 353 | for (const resume of requests) { |
| 354 | resume(exit) |
| 355 | } |
| 356 | Effect.runFork(ScopedRef.set(connectionRef, makeConnection)) |
| 357 | } |
no test coverage detected