React component refactoring
This commit is contained in:
@@ -7,71 +7,48 @@ export interface ReactComponent<P, E, R> {
|
|||||||
(props: P): Effect.Effect<React.ReactNode, E, R>
|
(props: P): Effect.Effect<React.ReactNode, E, R>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const nonReactiveTags = [Tracer.ParentSpan] as const
|
||||||
|
|
||||||
|
|
||||||
export const useFC: {
|
export const useFC: {
|
||||||
<P, E, R>(
|
<P, E, R>(
|
||||||
self: ReactComponent<P, E, R>,
|
self: ReactComponent<P, E, R>,
|
||||||
options?: ReactHook.ScopeOptions,
|
options?: ReactHook.ScopeOptions,
|
||||||
): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>>
|
): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>>
|
||||||
} = Effect.fn(function* useFC<P, E, R>(
|
} = Effect.fnUntraced(function* useFC<P, E, R>(
|
||||||
self: ReactComponent<P, E, R>,
|
self: ReactComponent<P, E, R>,
|
||||||
options?: ReactHook.ScopeOptions,
|
options?: ReactHook.ScopeOptions,
|
||||||
) {
|
) {
|
||||||
const runtime = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
const runtime = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
||||||
|
|
||||||
return React.useCallback((props: P) => Runtime.runSync(runtime)(
|
return React.useCallback((props: P) => {
|
||||||
self(props) as Effect.Effect<React.ReactNode, E, Exclude<R, Scope.Scope>>
|
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()
|
||||||
|
))
|
||||||
})
|
})
|
||||||
|
|
||||||
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.fn(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))
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const FC = <P, E, R>(
|
|
||||||
self: ReactComponent<P, E, R>,
|
|
||||||
runtime: Runtime.Runtime<R>,
|
|
||||||
props: P,
|
|
||||||
options?: ReactHook.ScopeOptions,
|
|
||||||
): React.ReactNode => {
|
|
||||||
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 React.useMemo(() => Runtime.runSync(runtime)(
|
|
||||||
Effect.provideService(self(props), Scope.Scope, scope)
|
|
||||||
), [
|
|
||||||
props,
|
|
||||||
...Array.from(Context.omit(Tracer.ParentSpan)(runtime.context).unsafeMap.values()),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
const makeScope = (options?: ReactHook.ScopeOptions) => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
|
const makeScope = (options?: ReactHook.ScopeOptions) => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
|
||||||
const closeScope = (
|
const closeScope = (
|
||||||
scope: Scope.CloseableScope,
|
scope: Scope.CloseableScope,
|
||||||
@@ -87,3 +64,17 @@ const closeScope = (
|
|||||||
break
|
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))
|
||||||
|
})
|
||||||
|
|||||||
@@ -15,9 +15,7 @@ function RouteComponent() {
|
|||||||
return <>
|
return <>
|
||||||
{runtime.runSync(ReactComponent.use(MyTestComponent, Component => (
|
{runtime.runSync(ReactComponent.use(MyTestComponent, Component => (
|
||||||
<Component />
|
<Component />
|
||||||
)).pipe(
|
)))}
|
||||||
Effect.scoped
|
|
||||||
))}
|
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,10 +28,10 @@ const MyTestComponent = Effect.fn(function* MyTestComponent(props?: { readonly v
|
|||||||
const [state, setState] = React.useState("value")
|
const [state, setState] = React.useState("value")
|
||||||
const effectValue = yield* Effect.succeed(`state: ${ state }`)
|
const effectValue = yield* Effect.succeed(`state: ${ state }`)
|
||||||
|
|
||||||
yield* ReactHook.useOnce(() => Effect.andThen(
|
yield* ReactHook.useEffect(() => Effect.andThen(
|
||||||
Effect.addFinalizer(() => Console.log("MyTestComponent umounted")),
|
Effect.addFinalizer(() => Console.log("MyTestComponent umounted")),
|
||||||
Console.log("MyTestComponent mounted"),
|
Console.log("MyTestComponent mounted"),
|
||||||
))
|
), [])
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<Text>{effectValue}</Text>
|
<Text>{effectValue}</Text>
|
||||||
|
|||||||
Reference in New Issue
Block a user