0.1.0 #1
@@ -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>(
|
useFork<A, E>(
|
||||||
self: Effect.Effect<A, E, RuntimeR | ContextR | Scope.Scope>,
|
effect: Effect.Effect<A, E, RuntimeR | ContextR | Scope.Scope>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: Runtime.RunForkOptions,
|
options?: Runtime.RunForkOptions & RenderOptions,
|
||||||
): void {
|
): void {
|
||||||
const runFork = this.useRunFork()
|
const runFork = this.useRunFork()
|
||||||
|
|
||||||
return React.useEffect(() => {
|
return React.useEffect(() => {
|
||||||
const fiber = runFork(Effect.scoped(self), options)
|
const fiber = runFork(Effect.scoped(effect), options)
|
||||||
return () => { runFork(Fiber.interrupt(fiber)) }
|
return () => { runFork(Fiber.interrupt(fiber)) }
|
||||||
}, [runFork, ...(deps ?? [])])
|
}, [
|
||||||
|
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runFork],
|
||||||
|
...(deps ?? []),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
|
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
|
||||||
const runSync = this.useRunSync()
|
return this.useMemo(
|
||||||
return React.useMemo(() => runSync(SubscriptionRef.make(value)), [])
|
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> {
|
useRefFromEffect<A, E>(effect: Effect.Effect<A, E, RuntimeR | ContextR>): SubscriptionRef.SubscriptionRef<A> {
|
||||||
const runSync = this.useRunSync()
|
return this.useMemo(
|
||||||
|
effect.pipe(Effect.flatMap(SubscriptionRef.make)),
|
||||||
return React.useMemo(() => runSync(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>>] {
|
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> =>
|
export const make = <R = never>(): Reffuse<never, R, R> =>
|
||||||
new Reffuse(Runtime.defaultRuntime)
|
new Reffuse(Runtime.defaultRuntime)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user