|
| 1 | +# Union Svelte + Effect Library |
| 2 | + |
| 3 | +> \[!CAUTION\] |
| 4 | +> This package is experimental and subject to breaking changes. |
| 5 | +
|
| 6 | +## Modules |
| 7 | + |
| 8 | +- `Runtime`: Provides management of Effect runtime in Svelte components and reactive rune-like Effect execution. |
| 9 | +- `Snippets`: Provides Svelte snippets to perform matching on common Effect ADTs (e.g. `Option`, `Either`, `Exit`, etc.) |
| 10 | +- `SvelteConfigProvider`: Provides [`ConfigProvider`](https://effect-ts.github.io/effect/effect/ConfigProvider.ts.html#configprovider) for common SvelteKit environment patterns. |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +In lieu of a known alterative, it is suggested to initialize the runtime as a module singleton and to trigger construction in a client hook. |
| 15 | + |
| 16 | +### Runtime Definition |
| 17 | + |
| 18 | +Define layers and re-export helper functions. |
| 19 | + |
| 20 | +`src/lib/runtime.ts` |
| 21 | + |
| 22 | +```ts |
| 23 | +import { Runtime } from "@unionlabs/effect-svelte" |
| 24 | +import { Layer, ManagedRuntime, Match, Predicate, pipe } from "effect" |
| 25 | + |
| 26 | +/** |
| 27 | + * Ensure "vitest/importMeta" is defined in `tsconfig.json` "types". |
| 28 | + */ |
| 29 | +const IS_VITEST = Predicate.isNotUndefined(import.meta.vitest) |
| 30 | + |
| 31 | +type AppLayer = Layer.Layer<never, never, never> |
| 32 | +export type AppContext = Layer.Layer.Success<AppLayer> |
| 33 | + |
| 34 | +const make = async () => { |
| 35 | + const AppLayer = ( |
| 36 | + await pipe( |
| 37 | + Match.value(IS_VITEST), |
| 38 | + Match.when(true, () => import("$lib/layers/test.js")), |
| 39 | + Match.when(false, () => import("$lib/layers/live.js")), |
| 40 | + Match.exhaustive |
| 41 | + ) |
| 42 | + ).default satisfies AppLayer |
| 43 | + |
| 44 | + const { |
| 45 | + runFork, |
| 46 | + runPromise, |
| 47 | + runPromiseExit, |
| 48 | + runSync, |
| 49 | + runSyncExit, |
| 50 | + runtime: _runtime |
| 51 | + } = ManagedRuntime.make(AppLayer) |
| 52 | + |
| 53 | + const runtime = await _runtime() |
| 54 | + |
| 55 | + const runFork$ = Runtime.runForkWithRuntime(runtime) |
| 56 | + const runPromiseExit$ = Runtime.runPromiseExitWithRuntime(runtime) |
| 57 | + |
| 58 | + return { |
| 59 | + runFork$, |
| 60 | + runFork, |
| 61 | + runPromise, |
| 62 | + runPromiseExit$, |
| 63 | + runPromiseExit, |
| 64 | + runSync, |
| 65 | + runSyncExit, |
| 66 | + } as const |
| 67 | +} |
| 68 | + |
| 69 | +type Runtime = Awaited<ReturnType<typeof make>> |
| 70 | + |
| 71 | +/** @public */ |
| 72 | +export let runFork$: Runtime["runFork$"] |
| 73 | +/** @public */ |
| 74 | +export let runFork: Runtime["runFork"] |
| 75 | +/** @public */ |
| 76 | +export let runPromise: Runtime["runPromise"] |
| 77 | +/** @public */ |
| 78 | +export let runPromiseExit$: Runtime["runPromiseExit$"] |
| 79 | +/** @public */ |
| 80 | +export let runPromiseExit: Runtime["runPromiseExit"] |
| 81 | +/** @public */ |
| 82 | +export let runSync: Runtime["runSync"] |
| 83 | +/** @public */ |
| 84 | +export let runSyncExit: Runtime["runSyncExit"] |
| 85 | + |
| 86 | +/** @public */ |
| 87 | +export const __init = async () => { |
| 88 | + const runtime = await make() |
| 89 | + ;({ |
| 90 | + runFork$, |
| 91 | + runFork, |
| 92 | + runPromise, |
| 93 | + runPromiseExit$, |
| 94 | + runPromiseExit, |
| 95 | + runSync, |
| 96 | + runSyncExit |
| 97 | + } = runtime) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### Runtime Initialization |
| 102 | + |
| 103 | +This client hook guarantees that the Effect runtime is initalized as early as possbile. |
| 104 | + |
| 105 | +`src/hooks.client.ts`: |
| 106 | + |
| 107 | +```ts |
| 108 | +import type { ClientInit } from "@sveltejs/kit" |
| 109 | + |
| 110 | +export const init: ClientInit = async () => { |
| 111 | + await import("$lib/runtime.js").then((_) => _.__init()) |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Usage |
| 116 | + |
| 117 | +See [test cases](https://github.com/unionlabs/union/blob/main/effect-svelte/lib/test/Runtime.test.ts) for inspiration. |
0 commit comments