Scope refactoring
Lint / lint (push) Successful in 11s

This commit is contained in:
Julien Valverdé
2025-07-07 05:46:43 +02:00
parent 708c93ed83
commit 1f541b4234
3 changed files with 88 additions and 107 deletions
+3 -44
View File
@@ -1,5 +1,6 @@
import { Context, Effect, ExecutionStrategy, Exit, Function, Pipeable, Ref, Runtime, Scope, String, Tracer, type Types, type Utils } from "effect"
import { Context, Effect, ExecutionStrategy, Function, Pipeable, Runtime, Scope, String, Tracer, type Types, type Utils } from "effect"
import * as React from "react"
import * as Hook from "./Hook.js"
export interface Component<E, R, P extends {}> extends Pipeable.Pipeable {
@@ -68,7 +69,7 @@ export const useFC: {
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
return React.useMemo(() => function ScopeProvider(props: P) {
const scope = useScope(runtimeRef.current, self.options)
const scope = Runtime.runSync(runtimeRef.current)(Hook.useScope([], self.options))
const FC = React.useMemo(() => {
const f = (props: P) => Runtime.runSync(runtimeRef.current)(
@@ -84,48 +85,6 @@ export const useFC: {
))
})
const useScope = (
runtime: Runtime.Runtime<never>,
options: Options,
) => {
const [isInitialRun, initialScope] = React.useMemo(() => Runtime.runSync(runtime)(
Effect.all([Ref.make(true), Scope.make(options.finalizerExecutionStrategy)])
), [])
const [scope, setScope] = React.useState(initialScope)
React.useEffect(() => Runtime.runSync(runtime)(
Effect.if(isInitialRun, {
onTrue: () => Effect.as(
Ref.set(isInitialRun, false),
() => closeScope(scope, runtime, options),
),
onFalse: () => Scope.make(options.finalizerExecutionStrategy).pipe(
Effect.tap(scope => Effect.sync(() => setScope(scope))),
Effect.map(scope => () => closeScope(scope, runtime, options)),
),
})
), [])
return scope
}
const closeScope = (
scope: Scope.CloseableScope,
runtime: Runtime.Runtime<never>,
options: Options,
) => {
switch (options.finalizerExecutionMode) {
case "sync":
Runtime.runSync(runtime)(Scope.close(scope, Exit.void))
break
case "fork":
Runtime.runFork(runtime)(Scope.close(scope, Exit.void))
break
}
}
export const use: {
<E, R, P extends {}>(
self: Component<E, R, P>,