UNPKG

@donverduyn/react-runtime

Version:
183 lines (171 loc) 10 kB
import * as React$1 from 'react'; import { IsUnknown, Simplify, Merge } from 'type-fest'; import { Effect, Scope, ManagedRuntime, Layer } from 'effect'; import { Call, Tuples, Booleans, Objects } from 'hotscript'; import * as react_jsx_runtime from 'react/jsx-runtime'; import * as effect_Cause from 'effect/Cause'; type Fallback<T, U> = [T, U] extends [infer A, infer B] ? unknown extends A ? B : A : never; type Fallback2<T extends unknown[], U extends unknown[]> = [ T, U ] extends [infer A extends unknown[], infer B extends unknown[]] ? unknown[] extends A ? B : A : never; declare const createUse: <R>(localContext: RuntimeContext<R>, instances: Map<RuntimeContext<any>, RuntimeInstance<any>>) => <A, E, R1>(targetOrEffect: RuntimeInstance<R1> | RuntimeContext<R1> | Effect.Effect<A, E, R>, effectOrDeps?: Effect.Effect<A, E, Fallback<R1, R>> | React$1.DependencyList, deps?: React$1.DependencyList) => A; type InvalidShapes = React$1.ProviderProps<any> | React$1.Context<any> | { Provider: unknown; } | undefined; type Sanitize<T> = T extends InvalidShapes ? unknown : T extends [infer Head, ...infer Tail] ? [Sanitize<Head>, ...Sanitize<Tail>] : T; declare const createFn: <R>(localContext: RuntimeContext<R>, instances: Map<RuntimeContext<any>, RuntimeInstance<any>>) => <T extends unknown[], T1 extends unknown[], A, A1, E, E1, R1>(targetOrEffect: RuntimeInstance<R1> | RuntimeContext<R1> | ((...args: T) => Effect.Effect<A, E, R | Scope.Scope>), fnOrDeps?: ((...args: T1) => Effect.Effect<A1, E1, Fallback<R1, R> | Scope.Scope>) | React$1.DependencyList, deps?: React$1.DependencyList) => IsUnknown<Fallback<T1, Sanitize<T>>> extends true ? () => Promise<Fallback<A1, A>> : (...args: Fallback2<T1, Sanitize<T>>) => Promise<Fallback<A1, A>>; declare const createRun: <R>(localContext: RuntimeContext<R>, instances: Map<RuntimeContext<any>, RuntimeInstance<any>>) => <A, E, R1>(targetOrEffect: RuntimeInstance<R1> | RuntimeContext<R1> | Effect.Effect<A, E, R | Scope.Scope>, effectOrDeps?: Effect.Effect<A, E, Fallback<R1, R> | Scope.Scope> | React$1.DependencyList, deps?: React$1.DependencyList) => void; declare const RUNTIME_PROP = "__runtimes"; declare const COMPONENT_PROP = "__component"; declare const PROPS_PROP = "__props"; declare const UPSTREAM_PROP = "__upstream"; type RuntimeApi<R> = { runtime: RuntimeInstance<R>; use: ReturnType<typeof createUse<R>>; useFn: ReturnType<typeof createFn<R>>; useRun: ReturnType<typeof createRun<R>>; }; type Config = { componentName: string; debug: boolean; postUnmountTTL: number; env: 'prod' | 'dev'; id: string; fresh: boolean; disposeStrategy: 'unmount' | 'dispose'; }; type RuntimeContext<T> = React.Context<RuntimeInstance<T> | undefined> & { layer: Layer.Layer<T>; isDisposed?: boolean; config: Partial<Config>; }; type RuntimeInstance<R> = ManagedRuntime.ManagedRuntime<R, never> & { config: Config; isDisposed?: boolean; id: string; }; type RuntimeType<T> = T extends React.Context<infer U> ? NonNullable<U> : never; type GetContextType<T> = T extends RuntimeContext<infer U> ? U : never; type RuntimeConfigFn<R, C extends React.FC<any>, TProps = Record<PropertyKey, any>> = (api: { configure: (config?: Partial<Config>) => RuntimeApi<R>; runtime: RuntimeApi<R>; }, props: Merge<Partial<React.ComponentProps<C>>, ExtractStaticProps<C>>) => TProps | undefined; type RuntimeEntry<R, C extends React.FC<any>> = { id: string; type: 'runtime' | 'upstream'; context: RuntimeContextReference<R, C>; configFn?: RuntimeConfigFn<R, C> | undefined; }; type RuntimeContextReference<R, C extends React.FC<any> = React.FC<any>> = { context: RuntimeContext<R>; reference: () => C; }; type ExtractStaticComponent<T> = T extends { [COMPONENT_PROP]: infer C; } ? C extends (props: infer P) => any ? React.FC<Simplify<P>> : never : T; type ExtractStaticRuntimes<T> = T extends { [RUNTIME_PROP]: infer R; } ? R extends unknown[] ? R : never : []; type ExtractStaticProps<T> = T extends { [PROPS_PROP]: infer P; } ? P : Record<never, never>; type ExtractStaticUpstream<T> = T extends { [UPSTREAM_PROP]: infer U; } ? U extends unknown[] ? U : never : []; type UnwrapRuntime<T> = T extends { __runtime: infer R; } ? R : T; type ExtractReference<T> = UnwrapRuntime<T> extends { reference: () => infer R; } ? R : never; declare const UpstreamSymbol: unique symbol; type UpstreamBrand = { readonly [UpstreamSymbol]: never; }; type Up<T> = T & UpstreamBrand; type KeepUpstream<T> = Call<Tuples.Filter<Booleans.Extends<{ [UpstreamSymbol]: true; }>>, T>; type DownstreamBrand = { readonly [UpstreamSymbol]: false; }; type Down<T> = T & DownstreamBrand; type FilterReference<T> = Call<Tuples.Map<Objects.Get<'type'>>, T>; type ExtractRuntimes<T> = T extends { __runtimes?: infer R; } ? R : never; type Includes<T extends readonly unknown[], V> = T extends [ infer Head, ...infer Rest ] ? [V] extends [Head] ? true : Includes<Rest, V> : false; type PushUnique<T extends readonly unknown[], V> = Includes<T, V> extends true ? T : [...T, V]; type Reverse<T> = Call<Tuples.Reverse, T>; type CollectRuntimes<Component, Seen extends unknown[] = [], Acc extends unknown[] = []> = ExtractRuntimes<Component> extends readonly [infer Head, ...infer Tail] ? ExtractReference<Head> extends infer Ref ? Ref extends object ? Ref extends Seen[number] ? CollectRuntimes<{ __runtimes?: Tail; }, Seen, PushUnique<Acc, Head>> : CollectRuntimes<{ __runtimes?: Tail; }, [ ...Seen, Ref ], PushUnique<[ ...Acc, ...CollectRuntimes<Ref, [...Seen, Ref], Acc> ], Head>> : CollectRuntimes<{ __runtimes?: Tail; }, Seen, PushUnique<Acc, Head>> : Acc : Acc; type TraverseDeps<T> = Reverse<Unique<CollectRuntimes<T>>>; type Unique<T extends any[], Acc extends any[] = []> = T extends [ infer Head, ...infer Tail ] ? Head extends Acc[number] ? Unique<Tail, Acc> : Unique<Tail, [...Acc, Head]> : Acc; declare function isReactContext2<T>(variable: unknown): variable is React$1.Context<T>; declare const isReactContext: <T>(variable: any) => variable is typeof variable extends T ? T : never; type ExtractMeta<T> = T extends React$1.FC<any> ? Omit<T, keyof React$1.FC> : never; declare const getDisplayName: <C extends React$1.FC<any> | undefined>(Component: C, prefix?: string) => string; declare function copyStaticProperties(base: Record<string, unknown>, target: unknown): void; declare const createElement: <C extends React$1.FC<any> | undefined>(Component: C, mergedProps: C extends React$1.FC<any> ? React$1.ComponentProps<C> : Record<never, never>) => react_jsx_runtime.JSX.Element | null; declare const extractMeta: <C extends React$1.FC<any>>(Component: C) => ExtractMeta<C>; declare function withRuntime<TProps, C extends React$1.FC<any>, TContext, R>(Context: TContext & RuntimeContextReference<R>, getSource: (api: { configure: (config?: Partial<Config>) => RuntimeApi<R>; runtime: RuntimeApi<R>; }, props: Merge<Partial<React$1.ComponentProps<C>>, ExtractStaticProps<C>>) => TProps): (Component: C) => React$1.FC<Simplify<Omit<React$1.ComponentProps<C>, keyof TProps>>> & Merge<ExtractMeta<C>, { [UPSTREAM_PROP]: ExtractStaticUpstream<C>; [RUNTIME_PROP]: [...ExtractStaticRuntimes<C>, Down<TContext>]; [COMPONENT_PROP]: ExtractStaticComponent<C>; [PROPS_PROP]: Merge<ExtractStaticProps<C>, TProps>; }>; declare function withUpstream<TProps, C extends React$1.FC<any>, TContext, R>(Context: TContext & RuntimeContextReference<R>, getSource: (api: { runtime: RuntimeApi<R>; }, props: Merge<Partial<React$1.ComponentProps<C>>, ExtractStaticProps<C>>) => TProps): (Component: C) => React$1.FC<Simplify<Omit<React$1.ComponentProps<C>, keyof TProps>>> & Merge<ExtractMeta<C>, { [UPSTREAM_PROP]: TraverseDeps<{ [RUNTIME_PROP]: KeepUpstream<[ ...ExtractStaticRuntimes<C>, Up<TContext> ]>; }>; [RUNTIME_PROP]: [...ExtractStaticRuntimes<C>, Up<TContext>]; [COMPONENT_PROP]: ExtractStaticComponent<C>; [PROPS_PROP]: Merge<ExtractStaticProps<C>, TProps>; }>; type WithStaticFn<T, C extends React.FC<any>> = (Component: C) => React.FC<Simplify<React.ComponentProps<C>>> & T; declare function withStatic<C extends React.FC<any>, T extends Record<any, any>>(staticProperties: T): WithStaticFn<T, C>; type RuntimeContext2<T, R> = { context: React$1.Context<(ManagedRuntime.ManagedRuntime<T, never> & { id: string; }) | undefined>; layer: Layer.Layer<T>; reference: R; }; declare const createRuntimeContext2: <T, R extends { reference: any; }>(fn: (layer: Layer.Layer<T>) => R) => (layer: Layer.Layer<T>) => { context: React$1.Context<(ManagedRuntime.ManagedRuntime<T, never> & { id: string; }) | undefined>; layer: Layer.Layer<T, never, never>; } & R; declare const createRuntimeContext: <TT extends RuntimeContext<R>, R = never>(config: Partial<Config>) => (layer: Layer.Layer<R>) => TT; declare const fromLayer: <A, E, R, TResult = Effect.Effect<A, E, R>>(layer: Effect.Effect<A, E, R>, cb?: (arg: A) => TResult) => [TResult] extends [Effect.Effect<infer A1, infer E1, infer R1>] ? Effect.Effect<A1, E | E1, R | R1> : [TResult] extends [PromiseLike<infer A1_1>] ? Effect.Effect<A1_1, E | effect_Cause.UnknownException, R> : Effect.Effect<TResult, E, R>; export { COMPONENT_PROP, PROPS_PROP, RUNTIME_PROP, UPSTREAM_PROP, copyStaticProperties, createElement, createRuntimeContext, createRuntimeContext2, extractMeta, fromLayer, getDisplayName, isReactContext, isReactContext2, withRuntime, withStatic, withUpstream }; export type { CollectRuntimes, Config, Down, ExtractMeta, ExtractStaticComponent, ExtractStaticProps, ExtractStaticRuntimes, ExtractStaticUpstream, FilterReference, GetContextType, KeepUpstream, RuntimeApi, RuntimeConfigFn, RuntimeContext, RuntimeContext2, RuntimeContextReference, RuntimeEntry, RuntimeInstance, RuntimeType, TraverseDeps, UnwrapRuntime, Up };