,
context: React.Context>,
) => function WithRuntime(props: P) {
return React.createElement(
Effect.runSyncWith(React.useContext(context))(self.use) as React.FC,
props,
)
})
export declare namespace useScope {
export interface Options {
readonly finalizerExecutionStrategy?: "sequential" | "parallel"
readonly finalizerExecutionDebounce?: Duration.Input
}
}
export const useScope = Effect.fnUntraced(function* (
deps: React.DependencyList,
options?: useScope.Options,
): Effect.fn.Return {
const context = yield* Effect.context()
const contextRef = React.useRef(context)
contextRef.current = context
const scope = React.useMemo(
() => Scope.makeUnsafe(options?.finalizerExecutionStrategy ?? defaultOptions.finalizerExecutionStrategy),
// biome-ignore lint/correctness/useExhaustiveDependencies: caller controls scope lifetime
deps,
)
React.useEffect(() => {
const pending = scopeCleanupTimers.get(scope)
if (pending !== undefined) clearTimeout(pending)
return () => {
const timer = setTimeout(() => {
Effect.runSyncWith(contextRef.current)(Scope.close(scope, Exit.succeed(undefined)))
scopeCleanupTimers.delete(scope)
}, durationMillis(options?.finalizerExecutionDebounce ?? defaultOptions.finalizerExecutionDebounce))
scopeCleanupTimers.set(scope, timer)
}
}, [scope, options?.finalizerExecutionDebounce])
return scope
})
const scopeCleanupTimers = new WeakMap