Update actions/setup-node action to v7 - autoclosed #55
@@ -1,6 +1,6 @@
|
|||||||
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
|
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
|
||||||
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
||||||
import { Context, type Duration, Effect, Equivalence, ExecutionStrategy, Exit, Fiber, Function, HashMap, identity, Layer, Option, Pipeable, Predicate, Ref, Runtime, Scope, Tracer } from "effect"
|
import { Context, type Duration, Effect, Equivalence, ExecutionStrategy, Exit, Fiber, Function, HashMap, identity, Layer, Option, Pipeable, Predicate, Ref, Scope, Tracer } from "effect"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
|
|
||||||
|
|
||||||
@@ -609,18 +609,18 @@ export const withOptions: {
|
|||||||
*/
|
*/
|
||||||
export const withRuntime: {
|
export const withRuntime: {
|
||||||
<P extends {}, A extends React.ReactNode, E, R, F extends Component.Signature>(
|
<P extends {}, A extends React.ReactNode, E, R, F extends Component.Signature>(
|
||||||
context: React.Context<Runtime.Runtime<R>>,
|
context: React.Context<Context.Context<R>>,
|
||||||
): (self: Component<P, A, E, Scope.Scope | NoInfer<R>, F>) => F
|
): (self: Component<P, A, E, Scope.Scope | NoInfer<R>, F>) => F
|
||||||
<P extends {}, A extends React.ReactNode, E, R, F extends Component.Signature>(
|
<P extends {}, A extends React.ReactNode, E, R, F extends Component.Signature>(
|
||||||
self: Component<P, A, E, Scope.Scope | NoInfer<R>, F>,
|
self: Component<P, A, E, Scope.Scope | NoInfer<R>, F>,
|
||||||
context: React.Context<Runtime.Runtime<R>>,
|
context: React.Context<Context.Context<R>>,
|
||||||
): F
|
): F
|
||||||
} = Function.dual(2, <P extends {}, A extends React.ReactNode, E, R, F extends Component.Signature>(
|
} = Function.dual(2, <P extends {}, A extends React.ReactNode, E, R, F extends Component.Signature>(
|
||||||
self: Component<P, A, E, R, F>,
|
self: Component<P, A, E, R, F>,
|
||||||
context: React.Context<Runtime.Runtime<R>>,
|
context: React.Context<Context.Context<R>>,
|
||||||
) => function WithRuntime(props: P) {
|
) => function WithRuntime(props: P) {
|
||||||
return React.createElement(
|
return React.createElement(
|
||||||
Runtime.runSync(React.useContext(context))(self.use) as React.FC<P>,
|
Effect.runSyncWith(React.useContext(context))(self.use) as React.FC<P>,
|
||||||
props,
|
props,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,41 +1,52 @@
|
|||||||
/** biome-ignore-all lint/complexity/useArrowFunction: React component names are intentional */
|
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
||||||
import { Context, Layer, ManagedRuntime, Predicate } from "effect"
|
import { type Context, Effect, Layer, ManagedRuntime, Predicate } from "effect"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
|
import * as Component from "./Component.js"
|
||||||
import * as ErrorObserver from "./ErrorObserver.js"
|
import * as ErrorObserver from "./ErrorObserver.js"
|
||||||
import * as QueryClient from "./QueryClient.js"
|
import * as QueryClient from "./QueryClient.js"
|
||||||
|
|
||||||
|
|
||||||
export const TypeId: unique symbol = Symbol.for("@effect-fc/ReactRuntime/ReactRuntime")
|
export const ReactRuntimeTypeId: unique symbol = Symbol.for("@effect-fc/ReactRuntime/ReactRuntime")
|
||||||
export type TypeId = typeof TypeId
|
export type ReactRuntimeTypeId = typeof ReactRuntimeTypeId
|
||||||
|
|
||||||
export interface ReactRuntime<R, ER> {
|
export interface ReactRuntime<R, ER> {
|
||||||
new(_: never): Record<string, never>
|
new(_: never): Record<string, never>
|
||||||
readonly [TypeId]: TypeId
|
readonly [ReactRuntimeTypeId]: ReactRuntimeTypeId
|
||||||
readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
|
readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
|
||||||
readonly context: React.Context<Context.Context<R>>
|
readonly context: React.Context<Context.Context<R>>
|
||||||
}
|
}
|
||||||
|
|
||||||
const ReactRuntimeProto = Object.freeze({ [TypeId]: TypeId } as const)
|
const ReactRuntimeProto = Object.freeze({ [ReactRuntimeTypeId]: ReactRuntimeTypeId } as const)
|
||||||
|
|
||||||
export const Prelude: Layer.Layer<ErrorObserver.ErrorObserver | QueryClient.QueryClient> = Layer.merge(
|
export const preludeLayer: Layer.Layer<
|
||||||
|
| Component.ScopeMap
|
||||||
|
| ErrorObserver.ErrorObserver
|
||||||
|
| QueryClient.QueryClient
|
||||||
|
> = Layer.mergeAll(
|
||||||
|
Component.ScopeMap.layer,
|
||||||
ErrorObserver.layer,
|
ErrorObserver.layer,
|
||||||
QueryClient.QueryClient.Default,
|
QueryClient.QueryClient.Default,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
export const isReactRuntime = (u: unknown): u is ReactRuntime<unknown, unknown> => Predicate.hasProperty(u, TypeId)
|
export const isReactRuntime = (u: unknown): u is ReactRuntime<unknown, unknown> => Predicate.hasProperty(u, TypeId)
|
||||||
|
|
||||||
export const make = <R, ER>(
|
export const make = <R, ER>(
|
||||||
layer: Layer.Layer<R, ER>,
|
layer: Layer.Layer<R, ER>,
|
||||||
memoMap?: Layer.MemoMap,
|
memoMap?: Layer.MemoMap,
|
||||||
): ReactRuntime<R | ErrorObserver.ErrorObserver | QueryClient.QueryClient, ER> => Object.setPrototypeOf(
|
): ReactRuntime<Layer.Success<typeof layer> | R, ER> => Object.setPrototypeOf(
|
||||||
Object.assign(function() {}, {
|
Object.assign(function() {}, {
|
||||||
runtime: ManagedRuntime.make(Layer.merge(layer, Prelude), { memoMap }),
|
runtime: ManagedRuntime.make(
|
||||||
// biome-ignore lint/style/noNonNullAssertion: initialized by Provider before consumers render
|
Layer.merge(layer, preludeLayer),
|
||||||
context: React.createContext<Context.Context<R | ErrorObserver.ErrorObserver | QueryClient.QueryClient>>(null!),
|
memoMap,
|
||||||
|
),
|
||||||
|
// biome-ignore lint/style/noNonNullAssertion: context initialization
|
||||||
|
context: React.createContext<Context.Context<R>>(null!),
|
||||||
}),
|
}),
|
||||||
ReactRuntimeProto,
|
ReactRuntimeProto,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
export namespace Provider {
|
export namespace Provider {
|
||||||
export interface Props<R, ER> extends React.SuspenseProps {
|
export interface Props<R, ER> extends React.SuspenseProps {
|
||||||
readonly runtime: ReactRuntime<R, ER>
|
readonly runtime: ReactRuntime<R, ER>
|
||||||
@@ -44,8 +55,13 @@ export namespace Provider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const Provider = <R, ER>(
|
export const Provider = <R, ER>(
|
||||||
{ runtime, children, ...suspenseProps }: Provider.Props<R, ER>,
|
{ runtime, children, ...suspenseProps }: Provider.Props<R, ER>
|
||||||
): React.ReactNode => {
|
): React.ReactNode => {
|
||||||
|
Effect.runSync(Component.useOnChange(
|
||||||
|
() => Effect.addFinalizer(() => runtime.runtime.disposeEffect),
|
||||||
|
[runtime],
|
||||||
|
))
|
||||||
|
|
||||||
const promise = React.useMemo(() => runtime.runtime.context(), [runtime])
|
const promise = React.useMemo(() => runtime.runtime.context(), [runtime])
|
||||||
|
|
||||||
return React.createElement(
|
return React.createElement(
|
||||||
@@ -60,12 +76,5 @@ const ProviderInner = <R, ER>(
|
|||||||
readonly runtime: ReactRuntime<R, ER>
|
readonly runtime: ReactRuntime<R, ER>
|
||||||
readonly promise: Promise<Context.Context<R>>
|
readonly promise: Promise<Context.Context<R>>
|
||||||
readonly children?: React.ReactNode
|
readonly children?: React.ReactNode
|
||||||
},
|
}
|
||||||
): React.ReactNode => {
|
): React.ReactNode => React.createElement(runtime.context, { value: React.use(promise) }, children)
|
||||||
const context = React.use(promise)
|
|
||||||
React.useEffect(() => () => {
|
|
||||||
void runtime.runtime.dispose()
|
|
||||||
}, [runtime])
|
|
||||||
|
|
||||||
return React.createElement(runtime.context, { value: context }, children)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user