This commit is contained in:
@@ -1,33 +1,30 @@
|
|||||||
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
|
||||||
import { Effect, Function, Predicate, Runtime, Scope } from "effect"
|
import { Effect, Predicate, Runtime, Scope } from "effect"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import * as Component from "./Component.js"
|
import * as Component from "./Component.js"
|
||||||
|
|
||||||
|
|
||||||
export const TypeId: unique symbol = Symbol.for("@effect-fc/Async/Async")
|
export const AsyncTypeId: unique symbol = Symbol.for("@effect-fc/Async/Async")
|
||||||
export type TypeId = typeof TypeId
|
export type AsyncTypeId = typeof AsyncTypeId
|
||||||
|
|
||||||
export interface Async extends AsyncOptions {
|
export interface Async extends AsyncPrototype {}
|
||||||
readonly [TypeId]: TypeId
|
export interface AsyncPrototype {
|
||||||
|
readonly [AsyncTypeId]: AsyncTypeId
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AsyncOptions {
|
const PromiseTypeId: unique symbol = Symbol.for("@effect-fc/Async/Promise")
|
||||||
readonly defaultFallback?: React.ReactNode
|
|
||||||
}
|
|
||||||
|
|
||||||
export type AsyncProps = Omit<React.SuspenseProps, "children">
|
export const AsyncPrototype: AsyncPrototype = Object.freeze({
|
||||||
|
[AsyncTypeId]: AsyncTypeId,
|
||||||
|
|
||||||
export const AsyncPrototype = Object.freeze({
|
|
||||||
[TypeId]: TypeId,
|
|
||||||
|
|
||||||
asFunctionComponent<P extends {}, A extends React.ReactNode, E, R>(
|
asFunctionComponent<P extends {}, A extends React.ReactNode, E, R>(
|
||||||
this: Component.Component<P, A, E, R> & Async,
|
this: Component.Component<P, A, E, R> & Async,
|
||||||
runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
|
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)(
|
const promise = Runtime.runPromise(runtimeRef.current)(
|
||||||
Effect.andThen(
|
Effect.andThen(
|
||||||
Component.useScope([], this),
|
Component.useScope([], this),
|
||||||
@@ -35,49 +32,20 @@ export const AsyncPrototype = Object.freeze({
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return React.createElement(
|
return React.createElement(Inner, { ...props, [PromiseTypeId]: promise })
|
||||||
React.Suspense,
|
|
||||||
{ fallback: fallback ?? this.defaultFallback, name },
|
|
||||||
React.createElement(SuspenseInner, { promise }),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
} as const)
|
} 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>>(
|
export const async = <T extends Component.Component<any, any, any, any>>(
|
||||||
self: T
|
self: T
|
||||||
): (
|
): T & Async => Object.setPrototypeOf(
|
||||||
& Omit<T, keyof Component.Component.AsComponent<T>>
|
Object.assign(() => {}, self),
|
||||||
& 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),
|
|
||||||
Object.freeze(Object.setPrototypeOf(
|
Object.freeze(Object.setPrototypeOf(
|
||||||
Object.assign({}, AsyncPrototype),
|
Object.assign({}, AsyncPrototype),
|
||||||
Object.getPrototypeOf(self),
|
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),
|
|
||||||
))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user