diff --git a/packages/reffuse/src/Reffuse.ts b/packages/reffuse/src/Reffuse.ts
index 131c563..a638b6b 100644
--- a/packages/reffuse/src/Reffuse.ts
+++ b/packages/reffuse/src/Reffuse.ts
@@ -77,31 +77,71 @@ export class Reffuse<
}
+ useMemo(
+ effect: Effect.Effect,
+ deps: React.DependencyList,
+ options?: RenderOptions,
+ ): A {
+ const runSync = this.useRunSync()
+
+ return React.useMemo(() => runSync(effect), [
+ ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
+ ...deps,
+ ])
+ }
+
+ // useEffect(
+ // effect: Effect.Effect,
+ // deps?: React.DependencyList,
+ // options?: RenderOptions,
+ // ): void {
+ // const runSync = this.useRunSync()
+
+ // return React.useEffect(() => { runSync(effect) }, [
+ // ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
+ // ...(deps ?? []),
+ // ])
+ // }
+
+ useSuspense(
+ effect: Effect.Effect,
+ options?: { readonly signal?: AbortSignal },
+ ): A {
+ const runPromise = this.useRunPromise()
+ return React.use(runPromise(effect, options))
+ }
+
useFork(
- self: Effect.Effect,
+ effect: Effect.Effect,
deps?: React.DependencyList,
- options?: Runtime.RunForkOptions,
+ options?: Runtime.RunForkOptions & RenderOptions,
): void {
const runFork = this.useRunFork()
return React.useEffect(() => {
- const fiber = runFork(Effect.scoped(self), options)
+ const fiber = runFork(Effect.scoped(effect), options)
return () => { runFork(Fiber.interrupt(fiber)) }
- }, [runFork, ...(deps ?? [])])
+ }, [
+ ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runFork],
+ ...(deps ?? []),
+ ])
}
useRef(value: A): SubscriptionRef.SubscriptionRef {
- const runSync = this.useRunSync()
- return React.useMemo(() => runSync(SubscriptionRef.make(value)), [])
+ return this.useMemo(
+ SubscriptionRef.make(value),
+ [],
+ { doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes
+ )
}
useRefFromEffect(effect: Effect.Effect): SubscriptionRef.SubscriptionRef {
- const runSync = this.useRunSync()
-
- return React.useMemo(() => runSync(effect.pipe(
- Effect.flatMap(SubscriptionRef.make)
- )), [])
+ return this.useMemo(
+ effect.pipe(Effect.flatMap(SubscriptionRef.make)),
+ [],
+ { doNotReExecuteOnRuntimeOrContextChange: false }, // Do not recreate the ref when the context changes
+ )
}
useRefState(ref: SubscriptionRef.SubscriptionRef): [A, React.Dispatch>] {
@@ -128,6 +168,12 @@ export class Reffuse<
}
+export interface RenderOptions {
+ /** Prevents re-executing the effect when the Effect runtime or context changes. Defaults to `false`. */
+ readonly doNotReExecuteOnRuntimeOrContextChange?: boolean
+}
+
+
export const make = (): Reffuse =>
new Reffuse(Runtime.defaultRuntime)