Refactor Async
All checks were successful
Lint / lint (push) Successful in 12s

This commit is contained in:
Julien Valverdé
2026-03-03 13:17:47 +01:00
parent 2080d35b2c
commit 45b38d6c1f

View File

@@ -1,33 +1,30 @@
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
import { Effect, Function, Predicate, Runtime, Scope } from "effect"
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
import { Effect, Predicate, Runtime, Scope } from "effect"
import * as React from "react"
import * as Component from "./Component.js"
export const TypeId: unique symbol = Symbol.for("@effect-fc/Async/Async")
export type TypeId = typeof TypeId
export const AsyncTypeId: unique symbol = Symbol.for("@effect-fc/Async/Async")
export type AsyncTypeId = typeof AsyncTypeId
export interface Async extends AsyncOptions {
readonly [TypeId]: TypeId
export interface Async extends AsyncPrototype {}
export interface AsyncPrototype {
readonly [AsyncTypeId]: AsyncTypeId
}
export interface AsyncOptions {
readonly defaultFallback?: React.ReactNode
}
const PromiseTypeId: unique symbol = Symbol.for("@effect-fc/Async/Promise")
export type AsyncProps = Omit<React.SuspenseProps, "children">
export const AsyncPrototype = Object.freeze({
[TypeId]: TypeId,
export const AsyncPrototype: AsyncPrototype = Object.freeze({
[AsyncTypeId]: AsyncTypeId,
asFunctionComponent<P extends {}, A extends React.ReactNode, E, R>(
this: Component.Component<P, A, E, R> & Async,
runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
) {
const SuspenseInner = (props: { readonly promise: Promise<React.ReactNode> }) => React.use(props.promise)
const Inner = (props: { readonly [PromiseTypeId]: Promise<React.ReactNode> }) => React.use(props[PromiseTypeId])
Inner.displayName = `${ this.displayName }Inner`
return ({ fallback, name, ...props }: AsyncProps) => {
return (props: {}) => {
const promise = Runtime.runPromise(runtimeRef.current)(
Effect.andThen(
Component.useScope([], this),
@@ -35,49 +32,20 @@ export const AsyncPrototype = Object.freeze({
)
)
return React.createElement(
React.Suspense,
{ fallback: fallback ?? this.defaultFallback, name },
React.createElement(SuspenseInner, { promise }),
)
return React.createElement(Inner, { ...props, [PromiseTypeId]: promise })
}
},
} as const)
export const isAsync = (u: unknown): u is Async => Predicate.hasProperty(u, TypeId)
export const isAsync = (u: unknown): u is Async => Predicate.hasProperty(u, AsyncTypeId)
export const async = <T extends Component.Component<any, any, any, any>>(
self: T
): (
& Omit<T, keyof Component.Component.AsComponent<T>>
& Component.Component<
Component.Component.Props<T> & AsyncProps,
Component.Component.Success<T>,
Component.Component.Error<T>,
Component.Component.Context<T>
>
& Async
) => Object.setPrototypeOf(
Object.assign(function() {}, self),
): T & Async => Object.setPrototypeOf(
Object.assign(() => {}, self),
Object.freeze(Object.setPrototypeOf(
Object.assign({}, AsyncPrototype),
Object.getPrototypeOf(self),
)),
)
export const withOptions: {
<T extends Component.Component<any, any, any, any> & Async>(
options: Partial<AsyncOptions>
): (self: T) => T
<T extends Component.Component<any, any, any, any> & Async>(
self: T,
options: Partial<AsyncOptions>,
): T
} = Function.dual(2, <T extends Component.Component<any, any, any, any> & Async>(
self: T,
options: Partial<AsyncOptions>,
): T => Object.setPrototypeOf(
Object.assign(function() {}, self, options),
Object.getPrototypeOf(self),
))