MCPcopy Create free account
hub / github.com/effect-app/libs / make

Function make

repos/effect/packages/sql/sqlite-wasm/src/SqliteClient.ts:299–454  ·  view source on GitHub ↗
(
  options: SqliteClientConfig
)

Source from the content-addressed store, hash-verified

297 * @category constructors
298 * @since 4.0.0
299 */
300export 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, "Failed to execute statement", "execute") })
338 )
339 )
340 } else {
341 resume(Exit.succeed(results))
342 }
343 }
344 }
345 port.addEventListener("message", onMessage)
346 if ("start" in port) {
347 port.start()
348 }
349
350 function onError(cause: Event) {
351 const exit = Exit.fail(
352 new SqlError({ reason: classifyError(cause, "SQLite WASM worker failed", "worker") })
353 )
354 const requests = Array.from(pending.values())
355 pending.clear()
356 for (const resume of requests) {

Callers 1

layerFunction · 0.70

Calls 13

identityFunction · 0.85
addFinalizerMethod · 0.80
syncMethod · 0.80
addEventListenerMethod · 0.80
removeEventListenerMethod · 0.80
getUnsafeMethod · 0.80
postMessageFunction · 0.70
sendFunction · 0.70
makeMethod · 0.65
withPermitsMethod · 0.65
getMethod · 0.65
takeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…