The best way to control Claude Code, Codex and more from any computer or phone

main
ts 64 lines 1.92 KB
Raw
1 /**
2 * Browser helper for running a root Effect program.
3 *
4 * This module exports `runMain`, a browser version of the main Effect runner.
5 * It forwards runner options to the core runtime and adds a `pagehide`
6 * listener that interrupts the main fiber when the document is discarded.
7 *
8 * @stability unstable
9 * @since 4.0.0
10 */
11 import type * as Effect from "effect/Effect"
12 import { makeRunMain, type Teardown } from "effect/Runtime"
13
14 /**
15 * Runs an effect as the browser main program and interrupts its fiber when the document is discarded.
16 *
17 * **When to use**
18 *
19 * Use to launch a browser page, single-page application, demo, or browser test
20 * harness as a root Effect program.
21 *
22 * **Details**
23 *
24 * Supports both direct and curried call forms. Options are forwarded to
25 * `makeRunMain`, including `disableErrorReporting` and custom `teardown`
26 * behavior. A persisted `pagehide` event is ignored because the document is
27 * entering the back/forward cache and may be restored.
28 *
29 * **Gotchas**
30 *
31 * The `pagehide` interruption is best-effort. Browser teardown may prevent
32 * asynchronous finalizers, network work, timers, or prompts from completing.
33 *
34 * @stability unstable
35 * @category running
36 * @since 4.0.0
37 */
38 export const runMain: {
39 (
40 options?: {
41 readonly disableErrorReporting?: boolean | undefined
42 readonly teardown?: Teardown | undefined
43 }
44 ): <E, A>(effect: Effect.Effect<A, E>) => void
45 <E, A>(
46 effect: Effect.Effect<A, E>,
47 options?: {
48 readonly disableErrorReporting?: boolean | undefined
49 readonly teardown?: Teardown | undefined
50 }
51 ): void
52 } = makeRunMain(({ fiber, teardown }) => {
53 function onPageHide(event: PageTransitionEvent) {
54 if (!event.persisted) {
55 fiber.interruptUnsafe(fiber.id)
56 }
57 }
58
59 globalThis.addEventListener("pagehide", onPageHide)
60 fiber.addObserver((exit) => {
61 globalThis.removeEventListener("pagehide", onPageHide)
62 teardown(exit, () => {})
63 })
64 })