Revert
All checks were successful
Lint / lint (push) Successful in 10s

This commit is contained in:
Julien Valverdé
2025-01-19 01:35:15 +01:00
parent 7ada793943
commit b0fac2b027

View File

@@ -67,38 +67,47 @@ export class Reffuse<R> {
* Changes to the Reffuse runtime or context will recompute the value in addition to the deps. * Changes to the Reffuse runtime or context will recompute the value in addition to the deps.
* You can disable this behavior by setting `doNotReExecuteOnRuntimeOrContextChange` to `true` in `options`. * You can disable this behavior by setting `doNotReExecuteOnRuntimeOrContextChange` to `true` in `options`.
*/ */
// useMemo<A, E>(
// effect: Effect.Effect<A, E, R | Scope.Scope>,
// deps?: React.DependencyList,
// options?: RenderOptions & ScopeOptions,
// ): A {
// const runSync = this.useRunSync()
// const [value, scope] = React.useMemo(() => Scope.make(options?.finalizerExecutionStrategy).pipe(
// Effect.flatMap(scope =>
// Effect.provideService(effect, Scope.Scope, scope).pipe(
// Effect.map(value => [value, scope] as const)
// )
// ),
// runSync,
// ), [
// ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
// ...(deps ?? []),
// ])
// React.useEffect(() => {
// // console.log("effect", value, scope)
// return () => {
// // console.log("cleanup", value, scope)
// runSync(Scope.close(scope, Exit.void))
// }
// }, [scope])
// return value
// }
/**
* Reffuse equivalent to `React.useMemo`.
*
* `useMemo` will only recompute the memoized value by running the given synchronous effect when one of the deps has changed. \
* Trying to run an asynchronous effect will throw.
*
* Changes to the Reffuse runtime or context will recompute the value in addition to the deps.
* You can disable this behavior by setting `doNotReExecuteOnRuntimeOrContextChange` to `true` in `options`.
*/
useMemo<A, E>( useMemo<A, E>(
effect: Effect.Effect<A, E, R | Scope.Scope>,
deps?: React.DependencyList,
options?: RenderOptions & ScopeOptions,
): A {
const runSync = this.useRunSync()
const [value, scope] = React.useMemo(() => Scope.make(options?.finalizerExecutionStrategy).pipe(
Effect.flatMap(scope =>
Effect.provideService(effect, Scope.Scope, scope).pipe(
Effect.map(value => [value, scope] as const)
)
),
runSync,
), [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
...(deps ?? []),
])
React.useEffect(() => {
// console.log("effect", value, scope)
return () => {
// console.log("cleanup", value, scope)
runSync(Scope.close(scope, Exit.void))
}
}, [scope])
return value
}
useMemoSync<A, E>(
effect: Effect.Effect<A, E, R>, effect: Effect.Effect<A, E, R>,
deps?: React.DependencyList, deps?: React.DependencyList,
options?: RenderOptions, options?: RenderOptions,
@@ -271,7 +280,7 @@ export class Reffuse<R> {
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> { useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
return this.useMemoSync( return this.useMemo(
SubscriptionRef.make(value), SubscriptionRef.make(value),
[], [],
{ doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes { doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes
@@ -279,7 +288,7 @@ export class Reffuse<R> {
} }
useRefFromEffect<A, E>(effect: Effect.Effect<A, E, R>): SubscriptionRef.SubscriptionRef<A> { useRefFromEffect<A, E>(effect: Effect.Effect<A, E, R>): SubscriptionRef.SubscriptionRef<A> {
return this.useMemoSync( return this.useMemo(
effect.pipe(Effect.flatMap(SubscriptionRef.make)), effect.pipe(Effect.flatMap(SubscriptionRef.make)),
[], [],
{ doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes { doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes