import { Effect, ExecutionStrategy, Runtime, Scope } from "effect" import * as React from "react" import { closeScope } from "./internal.js" import type { ScopeOptions } from "./ScopeOptions.js" export const useFork: { ( effect: () => Effect.Effect, deps?: React.DependencyList, options?: Runtime.RunForkOptions & ScopeOptions, ): Effect.Effect> } = Effect.fn("useFork")(function* ( effect: () => Effect.Effect, deps?: React.DependencyList, options?: Runtime.RunForkOptions & ScopeOptions, ) { const runtime = yield* Effect.runtime>() React.useEffect(() => { const scope = Runtime.runSync(runtime)(options?.scope ? Scope.fork(options.scope, options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential) : Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential) ) Runtime.runFork(runtime)(Effect.provideService(effect(), Scope.Scope, scope), { ...options, scope }) return () => closeScope(scope, runtime, { ...options, finalizerExecutionMode: options?.finalizerExecutionMode ?? "fork", }) }, deps) })