usePromise
All checks were successful
Lint / lint (push) Successful in 11s

This commit is contained in:
Julien Valverdé
2025-02-18 15:25:46 +01:00
parent 933b061b5d
commit bfcc097882
2 changed files with 64 additions and 58 deletions

View File

@@ -2,44 +2,36 @@ import { R } from "@/reffuse"
import { HttpClient } from "@effect/platform" import { HttpClient } from "@effect/platform"
import { Text } from "@radix-ui/themes" import { Text } from "@radix-ui/themes"
import { createFileRoute } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import { Console, Effect } from "effect" import { Effect, Schema } from "effect"
import { Suspense, use, useEffect, useMemo } from "react" import { Suspense, use } from "react"
export const Route = createFileRoute("/promise")({ export const Route = createFileRoute("/promise")({
component: RouteComponent component: RouteComponent
}) })
const Result = Schema.Tuple(Schema.String)
type Result = typeof Result.Type
function RouteComponent() { function RouteComponent() {
const promise = R.usePromise(HttpClient.HttpClient.pipe(
Effect.flatMap(client => client.get("https://www.uuidtools.com/api/generate/v4")),
HttpClient.withTracerPropagation(false),
Effect.flatMap(res => res.json),
Effect.flatMap(Schema.decodeUnknown(Result)),
Effect.scoped,
))
return ( return (
<Suspense fallback={<Text>Loading...</Text>}> <Suspense fallback={<Text>Loading...</Text>}>
<AsyncComponent /> <AsyncComponent promise={promise} />
</Suspense> </Suspense>
) )
} }
function AsyncComponent() { function AsyncComponent({ promise }: { readonly promise: Promise<Result> }) {
const [uuid] = use(promise)
// const runPromise = R.useRunPromise() return <Text>{uuid}</Text>
// const promise = useMemo(() => HttpClient.HttpClient.pipe(
// Effect.flatMap(client => client.get("https://www.uuidtools.com/api/generate/v4")),
// HttpClient.withTracerPropagation(false),
// Effect.flatMap(res => res.json),
// Effect.tap(Console.log),
// Effect.scoped,
// runPromise,
// ), [runPromise])
const promise = useMemo(() => new Promise<string>((resolve => {
setTimeout(() => { resolve("prout") }, 500)
})), [])
console.log("React.use invoked with:", promise);
const value = use(promise)
return <div>Hello "/tests"!</div>
} }

View File

@@ -284,43 +284,57 @@ export class Reffuse<R> {
]) ])
} }
// useSuspense<A, E>( usePromise<A, E>(
// effect: Effect.Effect<A, E, R>, effect: Effect.Effect<A, E, R>,
// deps?: React.DependencyList, deps?: React.DependencyList,
// options?: { readonly signal?: AbortSignal } & RenderOptions, options?: { readonly signal?: AbortSignal } & RenderOptions,
// ): A { ): Promise<A> {
// const runPromise = this.useRunPromise() const runPromise = this.useRunPromise()
// const promise = React.useMemo(() => runPromise(effect, options), [ return React.useMemo(() => runPromise(effect, options), [
// ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runPromise], ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runPromise],
// ...(deps ?? []), ...(deps ?? []),
// ]) ])
// return React.use(promise) }
// }
// useSuspenseScoped<A, E>( usePromiseScoped<A, E>(
// effect: Effect.Effect<A, E, R | Scope.Scope>, effect: Effect.Effect<A, E, R | Scope.Scope>,
// deps?: React.DependencyList, deps?: React.DependencyList,
// options?: { readonly signal?: AbortSignal } & RenderOptions & ScopeOptions, options?: { readonly signal?: AbortSignal } & RenderOptions & ScopeOptions,
// ): A { ): Promise<A> {
// const runSync = this.useRunSync() const runSync = this.useRunSync()
// const runPromise = this.useRunPromise() const runPromise = this.useRunPromise()
// const initialPromise = React.useMemo(() => runPromise(Effect.scoped(effect)), []) // Calculate an initial version of the value so that it can be accessed during the first render
// const [promise, setPromise] = React.useState(initialPromise) const initialScope = React.useMemo(() => runSync(Scope.make(options?.finalizerExecutionStrategy)), [])
const initialValue = React.useMemo(() => runPromise(Effect.provideService(effect, Scope.Scope, initialScope), options), [])
// React.useEffect(() => { // Keep track of the state of the initial scope
// const scope = runSync(Scope.make()) const initialScopeClosed = React.useRef(false)
// setPromise(runPromise(Effect.provideService(effect, Scope.Scope, scope), options))
// return () => { runPromise(Scope.close(scope, Exit.void)) } const [value, setValue] = React.useState(initialValue)
// }, [
// ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runPromise],
// ...(deps ?? []),
// ])
// return React.use(promise) React.useEffect(() => {
// } const closeInitialScopeIfNeeded = Scope.close(initialScope, Exit.void).pipe(
Effect.andThen(Effect.sync(() => { initialScopeClosed.current = true })),
Effect.when(() => !initialScopeClosed.current),
)
const scope = closeInitialScopeIfNeeded.pipe(
Effect.andThen(Scope.make(options?.finalizerExecutionStrategy)),
runSync,
)
setValue(runPromise(Effect.provideService(effect, Scope.Scope, initialScope), options))
return () => { runSync(Scope.close(scope, Exit.void)) }
}, [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runPromise],
...(deps ?? []),
])
return value
}
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> { useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {