useMemoScoped
All checks were successful
Lint / lint (push) Successful in 11s

This commit is contained in:
Julien Valverdé
2025-01-21 03:46:41 +01:00
parent e2bd448654
commit a1703aae37

View File

@@ -133,6 +133,29 @@ export class Reffuse<R> {
]) ])
} }
useMemoScoped<A, E>(
effect: Effect.Effect<A, E, R | Scope.Scope>,
deps?: React.DependencyList,
options?: RenderOptions & ScopeOptions,
): A {
const runSync = this.useRunSync()
const initialValue = React.useMemo(() => runSync(Effect.scoped(effect)), [])
const [value, setValue] = React.useState(initialValue)
React.useEffect(() => {
const scope = runSync(Scope.make(options?.finalizerExecutionStrategy))
setValue(runSync(Effect.provideService(effect, Scope.Scope, scope)))
return () => { runSync(Scope.close(scope, Exit.void)) }
}, [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
...(deps ?? []),
])
return value
}
/** /**
* Reffuse equivalent to `React.useEffect`. * Reffuse equivalent to `React.useEffect`.
* *