@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user