( options: SqliteClientConfig )
| 264 | * @since 1.0.0 |
| 265 | */ |
| 266 | export const make = ( |
| 267 | options: SqliteClientConfig |
| 268 | ): Effect.Effect<SqliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 269 | Effect.gen(function*() { |
| 270 | const reactivity = yield* Reactivity.Reactivity |
| 271 | const compiler = Statement.makeCompilerSqlite(options.transformQueryNames) |
| 272 | const transformRows = options.transformResultNames ? |
| 273 | Statement.defaultTransforms(options.transformResultNames).array : |
| 274 | undefined |
| 275 | const pending = new Map<number, (effect: Exit.Exit<any, SqlError>) => void>() |
| 276 | |
| 277 | const makeConnection = Effect.gen(function*() { |
| 278 | let currentId = 0 |
| 279 | const scope = yield* Effect.scope |
| 280 | const readyDeferred = yield* Deferred.make<void>() |
| 281 | |
| 282 | const worker = yield* options.worker |
| 283 | const port = "port" in worker ? worker.port : worker |
| 284 | const postMessage = (message: OpfsWorkerMessage, transferables?: ReadonlyArray<any>) => |
| 285 | port.postMessage(message, transferables as any) |
| 286 | |
| 287 | yield* Scope.addFinalizer(scope, Effect.sync(() => postMessage(["close"]))) |
| 288 | |
| 289 | const onMessage = (event: any) => { |
| 290 | const [id, error, results] = event.data |
| 291 | if (id === "ready") { |
| 292 | Deferred.unsafeDone(readyDeferred, Exit.void) |
| 293 | return |
| 294 | } else if (id === "update_hook") { |
| 295 | reactivity.unsafeInvalidate({ [error]: [results] }) |
| 296 | return |
| 297 | } else { |
| 298 | const resume = pending.get(id) |
| 299 | if (!resume) return |
| 300 | pending.delete(id) |
| 301 | if (error) { |
| 302 | resume(Exit.fail(new SqlError({ cause: error as string, message: "Failed to execute statement" }))) |
| 303 | } else { |
| 304 | resume(Exit.succeed(results)) |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | port.addEventListener("message", onMessage) |
| 309 | |
| 310 | function onError() { |
| 311 | Effect.runFork(ScopedRef.set(connectionRef, makeConnection)) |
| 312 | } |
| 313 | if ("onerror" in worker) { |
| 314 | worker.addEventListener("error", onError) |
| 315 | } |
| 316 | |
| 317 | yield* Scope.addFinalizer( |
| 318 | scope, |
| 319 | Effect.sync(() => { |
| 320 | worker.removeEventListener("message", onMessage) |
| 321 | worker.removeEventListener("error", onError) |
| 322 | }) |
| 323 | ) |
no test coverage detected
searching dependent graphs…