@@ -67,38 +67,47 @@ export class Reffuse<R> {
|
||||
* 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>(
|
||||
// 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>(
|
||||
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>,
|
||||
deps?: React.DependencyList,
|
||||
options?: RenderOptions,
|
||||
@@ -271,7 +280,7 @@ export class Reffuse<R> {
|
||||
|
||||
|
||||
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
|
||||
return this.useMemoSync(
|
||||
return this.useMemo(
|
||||
SubscriptionRef.make(value),
|
||||
[],
|
||||
{ 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> {
|
||||
return this.useMemoSync(
|
||||
return this.useMemo(
|
||||
effect.pipe(Effect.flatMap(SubscriptionRef.make)),
|
||||
[],
|
||||
{ doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes
|
||||
|
||||
Reference in New Issue
Block a user