MCPcopy Create free account
hub / github.com/Effect-TS/effect / make

Function make

packages/sql/sqlite-wasm/src/SqliteClient.ts:310–474  ·  view source on GitHub ↗
(
  options: SqliteClientConfig
)

Source from the content-addressed store, hash-verified

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

Callers 1

layerFunction · 0.70

Calls 12

addFinalizerMethod · 0.80
assignMethod · 0.80
postMessageFunction · 0.70
sendFunction · 0.70
makeMethod · 0.65
addEventListenerMethod · 0.65
removeEventListenerMethod · 0.65
awaitMethod · 0.65
getMethod · 0.65
identityFunction · 0.50
syncMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected