0.1.0 #1

Merged
Thilawyn merged 81 commits from next into master 2025-07-17 21:17:57 +02:00
4 changed files with 20 additions and 20 deletions
Showing only changes of commit a296d4874c - Show all commits

View File

@@ -2,7 +2,7 @@ import { Context, Effect, ExecutionStrategy, Exit, Function, Pipeable, Ref, Runt
import * as React from "react" import * as React from "react"
export interface ReactComponent<E, R, P extends {}> extends Pipeable.Pipeable { export interface Component<E, R, P extends {}> extends Pipeable.Pipeable {
(props: P): Effect.Effect<React.ReactNode, E, R> (props: P): Effect.Effect<React.ReactNode, E, R>
readonly displayName?: string readonly displayName?: string
readonly options: Options readonly options: Options
@@ -13,9 +13,9 @@ export interface Options {
readonly finalizerExecutionStrategy: ExecutionStrategy.ExecutionStrategy readonly finalizerExecutionStrategy: ExecutionStrategy.ExecutionStrategy
} }
export type Error<T> = T extends ReactComponent<infer E, infer _R, infer _P> ? E : never export type Error<T> = T extends Component<infer E, infer _R, infer _P> ? E : never
export type Context<T> = T extends ReactComponent<infer _E, infer R, infer _P> ? R : never export type Context<T> = T extends Component<infer _E, infer R, infer _P> ? R : never
export type Props<T> = T extends ReactComponent<infer _E, infer _R, infer P> ? P : never export type Props<T> = T extends Component<infer _E, infer _R, infer P> ? P : never
export const nonReactiveTags = [Tracer.ParentSpan] as const export const nonReactiveTags = [Tracer.ParentSpan] as const
@@ -32,7 +32,7 @@ export const make = <
>( >(
body: (props: P) => Generator<Eff, React.ReactNode, never>, body: (props: P) => Generator<Eff, React.ReactNode, never>,
options?: MakeOptions, options?: MakeOptions,
): ReactComponent< ): Component<
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never, [Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never, [Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never,
P P
@@ -44,7 +44,7 @@ export const make = <
? Effect.fn(displayName)(body) ? Effect.fn(displayName)(body)
: Effect.fn(body) : Effect.fn(body)
: Effect.fnUntraced(body) : Effect.fnUntraced(body)
) as ReactComponent<any, any, any> ) as Component<any, any, any>
f.pipe = function pipe() { return Pipeable.pipeArguments(this, arguments) }; f.pipe = function pipe() { return Pipeable.pipeArguments(this, arguments) };
(f as Types.Mutable<typeof f>).displayName = displayName; (f as Types.Mutable<typeof f>).displayName = displayName;
@@ -59,10 +59,10 @@ export const make = <
export const useFC: { export const useFC: {
<E, R, P extends {}>( <E, R, P extends {}>(
self: ReactComponent<E, R, P> self: Component<E, R, P>
): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>> ): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>>
} = Effect.fn("useFC")(function* <E, R, P extends {}>( } = Effect.fn("useFC")(function* <E, R, P extends {}>(
self: ReactComponent<E, R, P> self: Component<E, R, P>
) { ) {
const runtimeRef = React.useRef<Runtime.Runtime<Exclude<R, Scope.Scope>>>(null!) const runtimeRef = React.useRef<Runtime.Runtime<Exclude<R, Scope.Scope>>>(null!)
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>() runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
@@ -128,7 +128,7 @@ const closeScope = (
export const use: { export const use: {
<E, R, P extends {}>( <E, R, P extends {}>(
self: ReactComponent<E, R, P>, self: Component<E, R, P>,
fn: (Component: React.FC<P>) => React.ReactNode, fn: (Component: React.FC<P>) => React.ReactNode,
): Effect.Effect<React.ReactNode, never, Exclude<R, Scope.Scope>> ): Effect.Effect<React.ReactNode, never, Exclude<R, Scope.Scope>>
} = Effect.fn("use")(function*(self, fn) { } = Effect.fn("use")(function*(self, fn) {
@@ -138,13 +138,13 @@ export const use: {
export const withRuntime: { export const withRuntime: {
<E, R, P extends {}>( <E, R, P extends {}>(
context: React.Context<Runtime.Runtime<R>>, context: React.Context<Runtime.Runtime<R>>,
): (self: ReactComponent<E, R | Scope.Scope, P>) => React.FC<P> ): (self: Component<E, R | Scope.Scope, P>) => React.FC<P>
<E, R, P extends {}>( <E, R, P extends {}>(
self: ReactComponent<E, R | Scope.Scope, P>, self: Component<E, R | Scope.Scope, P>,
context: React.Context<Runtime.Runtime<R>>, context: React.Context<Runtime.Runtime<R>>,
): React.FC<P> ): React.FC<P>
} = Function.dual(2, <E, R, P extends {}>( } = Function.dual(2, <E, R, P extends {}>(
self: ReactComponent<E, R | Scope.Scope, P>, self: Component<E, R | Scope.Scope, P>,
context: React.Context<Runtime.Runtime<R>>, context: React.Context<Runtime.Runtime<R>>,
): React.FC<P> => function WithRuntime(props) { ): React.FC<P> => function WithRuntime(props) {
const runtime = React.useContext(context) const runtime = React.useContext(context)

View File

@@ -16,31 +16,31 @@ export const make = <R, ER>(
}) })
export interface AsyncProviderProps<R, ER> extends React.SuspenseProps { export interface ProviderProps<R, ER> extends React.SuspenseProps {
readonly runtime: ReactManagedRuntime<R, ER> readonly runtime: ReactManagedRuntime<R, ER>
readonly children?: React.ReactNode readonly children?: React.ReactNode
} }
export function AsyncProvider<R, ER>( export function AsyncProvider<R, ER>(
{ runtime, children, ...suspenseProps }: AsyncProviderProps<R, ER> { runtime, children, ...suspenseProps }: ProviderProps<R, ER>
): React.ReactNode { ): React.ReactNode {
const promise = React.useMemo(() => Effect.runPromise(runtime.runtime.runtimeEffect), [runtime]) const promise = React.useMemo(() => Effect.runPromise(runtime.runtime.runtimeEffect), [runtime])
return React.createElement( return React.createElement(
React.Suspense, React.Suspense,
suspenseProps, suspenseProps,
React.createElement(AsyncProviderInner<R, ER>, { runtime, promise, children }), React.createElement(ProviderInner<R, ER>, { runtime, promise, children }),
) )
} }
interface AsyncProviderInnerProps<R, ER> { interface ProviderInnerProps<R, ER> {
readonly runtime: ReactManagedRuntime<R, ER> readonly runtime: ReactManagedRuntime<R, ER>
readonly promise: Promise<Runtime.Runtime<R>> readonly promise: Promise<Runtime.Runtime<R>>
readonly children?: React.ReactNode readonly children?: React.ReactNode
} }
function AsyncProviderInner<R, ER>( function ProviderInner<R, ER>(
{ runtime, promise, children }: AsyncProviderInnerProps<R, ER> { runtime, promise, children }: ProviderInnerProps<R, ER>
): React.ReactNode { ): React.ReactNode {
const value = React.use(promise) const value = React.use(promise)
return React.createElement(runtime.context, { value }, children) return React.createElement(runtime.context, { value }, children)

View File

@@ -1,3 +1,3 @@
export * as ReactComponent from "./ReactComponent.js" export * as Component from "./Component.js"
export * as ReactHook from "./ReactHook.js" export * as Hook from "./Hook.js"
export * as ReactManagedRuntime from "./ReactManagedRuntime.js" export * as ReactManagedRuntime from "./ReactManagedRuntime.js"