Refactoring
All checks were successful
Lint / lint (push) Successful in 16s

This commit is contained in:
Julien Valverdé
2025-06-30 21:44:29 +02:00
parent 37d9400ada
commit 78a3735038
5 changed files with 112 additions and 53 deletions

View File

@@ -0,0 +1,29 @@
import { Effect, type Layer, ManagedRuntime, Runtime } from "effect"
import * as React from "react"
export interface ReactManagedRuntime<R, ER> {
readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
readonly context: React.Context<Runtime.Runtime<R>>
}
export const make = <R, ER>(
layer: Layer.Layer<R, ER>,
memoMap?: Layer.MemoMap,
): ReactManagedRuntime<R, ER> => ({
runtime: ManagedRuntime.make(layer, memoMap),
context: React.createContext<Runtime.Runtime<R>>(null!),
})
export interface SyncProviderProps<R, ER> {
readonly runtime: ReactManagedRuntime<R, ER>
readonly children?: React.ReactNode
}
export const SyncProvider = <R, ER>(
props: SyncProviderProps<R, ER>
): React.ReactNode => React.createElement(props.runtime.context, {
value: React.useMemo(() => Effect.runSync(props.runtime.runtime.runtimeEffect), [props.runtime]),
children: props.children,
})
SyncProvider.displayName = "ReactManagedRuntimeSyncProvider"