Cleanup
All checks were successful
Lint / lint (push) Successful in 15s

This commit is contained in:
Julien Valverdé
2025-06-29 19:35:43 +02:00
parent 6fa73ee33f
commit 2b78d4dc49
2 changed files with 65 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
import { Context, Effect, ExecutionStrategy, Exit, Ref, Runtime, Scope, Tracer } from "effect"
import { Context, Effect, Runtime, Tracer } from "effect"
import * as React from "react"
import * as ReactHook from "./ReactHook.js"
@@ -14,67 +14,78 @@ export const useFC: {
<P, E, R>(
self: ReactComponent<P, E, R>,
options?: ReactHook.ScopeOptions,
): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>>
): Effect.Effect<React.FC<P>, never, R>
} = Effect.fnUntraced(function* useFC<P, E, R>(
self: ReactComponent<P, E, R>,
options?: ReactHook.ScopeOptions,
self: ReactComponent<P, E, R>
) {
const runtime = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
const runtime = yield* Effect.runtime<R>()
return React.useCallback((props: P) => {
const [isInitialRun, initialScope] = React.useMemo(() => Runtime.runSync(runtime)(
Effect.all([Ref.make(true), makeScope(options)])
), [])
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: () => makeScope(options).pipe(
Effect.tap(scope => Effect.sync(() => setScope(scope))),
Effect.map(scope => () => closeScope(scope, runtime, options)),
),
})
), [])
return Runtime.runSync(runtime)(
Effect.provideService(self(props), Scope.Scope, scope)
)
}, Array.from(
return React.useCallback((props: P) => Runtime.runSync(runtime)(self(props)), Array.from(
Context.omit(...nonReactiveTags)(runtime.context).unsafeMap.values()
))
})
const makeScope = (options?: ReactHook.ScopeOptions) => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
const closeScope = (
scope: Scope.CloseableScope,
runtime: Runtime.Runtime<never>,
options?: ReactHook.ScopeOptions,
) => {
switch (options?.finalizerExecutionMode ?? "sync") {
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: {
<P, E, R>(
self: ReactComponent<P, E, R>,
fn: (Component: React.FC<P>) => React.ReactNode,
options?: ReactHook.ScopeOptions,
): Effect.Effect<React.ReactNode, never, Exclude<R, Scope.Scope>>
} = Effect.fnUntraced(function* use<P, E, R>(
self: ReactComponent<P, E, R>,
fn: (Component: React.FC<P>) => React.ReactNode,
options?: ReactHook.ScopeOptions,
) {
return fn(yield* useFC(self, options))
): Effect.Effect<React.ReactNode, never, R>
} = Effect.fnUntraced(function* use(self, fn) {
return fn(yield* useFC(self))
})
// export const useFC: {
// <P, E, R>(
// self: ReactComponent<P, E, R>,
// options?: ReactHook.ScopeOptions,
// ): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>>
// } = Effect.fnUntraced(function* useFC<P, E, R>(
// self: ReactComponent<P, E, R>,
// options?: ReactHook.ScopeOptions,
// ) {
// const runtime = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
// return React.useCallback((props: P) => {
// const [isInitialRun, initialScope] = React.useMemo(() => Runtime.runSync(runtime)(
// Effect.all([Ref.make(true), makeScope(options)])
// ), [])
// 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: () => makeScope(options).pipe(
// Effect.tap(scope => Effect.sync(() => setScope(scope))),
// Effect.map(scope => () => closeScope(scope, runtime, options)),
// ),
// })
// ), [])
// return Runtime.runSync(runtime)(
// Effect.provideService(self(props), Scope.Scope, scope)
// )
// }, Array.from(
// Context.omit(...nonReactiveTags)(runtime.context).unsafeMap.values()
// ))
// })
// const makeScope = (options?: ReactHook.ScopeOptions) => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
// const closeScope = (
// scope: Scope.CloseableScope,
// runtime: Runtime.Runtime<never>,
// options?: ReactHook.ScopeOptions,
// ) => {
// switch (options?.finalizerExecutionMode ?? "sync") {
// case "sync":
// Runtime.runSync(runtime)(Scope.close(scope, Exit.void))
// break
// case "fork":
// Runtime.runFork(runtime)(Scope.close(scope, Exit.void))
// break
// }
// }