0.1.0 #1

Merged
Thilawyn merged 87 commits from next into master 2025-01-18 00:54:42 +01:00
Showing only changes of commit 4f091ae221 - Show all commits

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)