@@ -1,4 +1,5 @@
|
||||
import { Function, Predicate } from "effect"
|
||||
import { Effect, Function, Predicate, Runtime, Scope } from "effect"
|
||||
import * as React from "react"
|
||||
import type * as Component from "./Component.js"
|
||||
import type { ExcludeKeys } from "./utils.js"
|
||||
|
||||
@@ -7,7 +8,7 @@ export const TypeId: unique symbol = Symbol.for("effect-fc/Suspense")
|
||||
export type TypeId = typeof TypeId
|
||||
|
||||
export interface Suspense extends Suspense.Options {
|
||||
readonly [TypeId]: true
|
||||
readonly [TypeId]: TypeId
|
||||
}
|
||||
|
||||
export namespace Suspense {
|
||||
@@ -19,6 +20,30 @@ export namespace Suspense {
|
||||
}
|
||||
|
||||
|
||||
const SuspenseProto = Object.freeze({
|
||||
[TypeId]: TypeId,
|
||||
makeFunctionComponent(
|
||||
this: Component.Component<any, any, any> & Suspense,
|
||||
runtimeRef: React.RefObject<Runtime.Runtime<any>>,
|
||||
scope: Scope.Scope,
|
||||
): React.FC<any> {
|
||||
const SuspenseInner = (props: { readonly promise: Promise<React.ReactNode> }) => React.use(props.promise)
|
||||
|
||||
return ({ fallback, name, ...props }: Suspense.Props) => {
|
||||
const promise = Runtime.runPromise(runtimeRef.current)(
|
||||
Effect.provideService(this.body(props), Scope.Scope, scope)
|
||||
)
|
||||
|
||||
return React.createElement(
|
||||
React.Suspense,
|
||||
{ fallback: fallback ?? this.defaultFallback, name },
|
||||
React.createElement(SuspenseInner, { promise }),
|
||||
)
|
||||
}
|
||||
},
|
||||
} as const)
|
||||
|
||||
|
||||
export const isSuspense = (u: unknown): u is Suspense => Predicate.hasProperty(u, TypeId)
|
||||
|
||||
export const suspense = <T extends Component.Component<any, any, P>, P extends {}>(
|
||||
@@ -28,7 +53,7 @@ export const suspense = <T extends Component.Component<any, any, P>, P extends {
|
||||
& Component.Component<Component.Component.Error<T>, Component.Component.Context<T>, P & Suspense.Props>
|
||||
& Suspense
|
||||
) => Object.setPrototypeOf(
|
||||
{ ...self, [TypeId]: true },
|
||||
{ ...self, ...SuspenseProto },
|
||||
Object.getPrototypeOf(self),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user