API work
Some checks failed
Lint / lint (push) Failing after 10s

This commit is contained in:
Julien Valverdé
2025-01-14 21:29:38 +01:00
parent 671a80b6ff
commit 4f091ae221

View File

@@ -77,31 +77,71 @@ export class Reffuse<
}
useMemo<A, E>(
effect: Effect.Effect<A, E, RuntimeR | ContextR>,
deps: React.DependencyList,
options?: RenderOptions,
): A {
const runSync = this.useRunSync()
return React.useMemo(() => runSync(effect), [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
...deps,
])
}
// useEffect<A, E>(
// effect: Effect.Effect<A, E, RuntimeR | ContextR | Scope.Scope>,
// deps?: React.DependencyList,
// options?: RenderOptions,
// ): void {
// const runSync = this.useRunSync()
// return React.useEffect(() => { runSync(effect) }, [
// ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
// ...(deps ?? []),
// ])
// }
useSuspense<A, E>(
effect: Effect.Effect<A, E, RuntimeR | ContextR>,
options?: { readonly signal?: AbortSignal },
): A {
const runPromise = this.useRunPromise()
return React.use(runPromise(effect, options))
}
useFork<A, E>(
self: Effect.Effect<A, E, RuntimeR | ContextR | Scope.Scope>,
effect: Effect.Effect<A, E, RuntimeR | ContextR | Scope.Scope>,
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<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
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<A, E>(effect: Effect.Effect<A, E, RuntimeR | ContextR>): SubscriptionRef.SubscriptionRef<A> {
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<A>(ref: SubscriptionRef.SubscriptionRef<A>): [A, React.Dispatch<React.SetStateAction<A>>] {
@@ -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 = <R = never>(): Reffuse<never, R, R> =>
new Reffuse(Runtime.defaultRuntime)