0.1.0 #1
@@ -1,38 +1,18 @@
|
|||||||
import { Context, Effect, Fiber, Layer, ManagedRuntime, Ref, Runtime, Scope, Stream, SubscriptionRef } from "effect"
|
import { Context, Effect, Fiber, FiberRefs, Ref, Runtime, RuntimeFlags, Scope, Stream, SubscriptionRef } from "effect"
|
||||||
import React from "react"
|
import React from "react"
|
||||||
import * as ReffuseReactContext from "./ReffuseReactContext.js"
|
import type * as ReffuseReactContext from "./ReffuseReactContext.js"
|
||||||
|
import * as ReffuseReactContextProvider from "./ReffuseReactContextProvider.js"
|
||||||
|
|
||||||
export interface ProviderProps<R, E> {
|
|
||||||
readonly layer: Layer.Layer<R, E>
|
|
||||||
readonly children?: React.ReactNode
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export class Reffuse<R> {
|
export class Reffuse<R> {
|
||||||
|
|
||||||
readonly Context = React.createContext<ReffuseReactContext.ReffuseReactContext<R>>(null!)
|
readonly Context = React.createContext<ReffuseReactContext.ReffuseReactContext<R>>(null!)
|
||||||
readonly Provider: React.FC<ProviderProps<R, unknown>>
|
readonly Provider: ReffuseReactContextProvider.ReffuseReactContextProvider<R>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
runtime: Runtime.Runtime<R>
|
runtime: Runtime.Runtime<never>
|
||||||
) {
|
) {
|
||||||
this.Provider = (props: ProviderProps<R, unknown>) => {
|
this.Provider = ReffuseReactContextProvider.make(runtime, this.Context)
|
||||||
const value = React.useMemo(() => ({
|
|
||||||
runtime,
|
|
||||||
context: Effect.context<R>().pipe(
|
|
||||||
Effect.provide(props.layer),
|
|
||||||
Runtime.runSync(runtime),
|
|
||||||
),
|
|
||||||
}), [props.layer])
|
|
||||||
|
|
||||||
return (
|
|
||||||
<this.Context
|
|
||||||
{...props}
|
|
||||||
value={value}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -131,4 +111,8 @@ export class Reffuse<R> {
|
|||||||
|
|
||||||
|
|
||||||
export const make = <R>(): Reffuse<R> =>
|
export const make = <R>(): Reffuse<R> =>
|
||||||
new Reffuse(ManagedRuntime.make(layer))
|
new Reffuse(Runtime.make({
|
||||||
|
context: Context.empty(),
|
||||||
|
runtimeFlags: RuntimeFlags.make(),
|
||||||
|
fiberRefs: FiberRefs.empty(),
|
||||||
|
}))
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { Effect, Runtime, type Layer } from "effect"
|
||||||
|
import React from "react"
|
||||||
|
import type * as ReffuseReactContext from "./ReffuseReactContext.js"
|
||||||
|
|
||||||
|
|
||||||
|
export interface Props<R> {
|
||||||
|
readonly layer: Layer.Layer<R, unknown>
|
||||||
|
readonly children?: React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ReffuseReactContextProvider<R> = React.FC<Props<R>>
|
||||||
|
|
||||||
|
|
||||||
|
export function make<R>(
|
||||||
|
runtime: Runtime.Runtime<never>,
|
||||||
|
Context: React.Context<ReffuseReactContext.ReffuseReactContext<R>>,
|
||||||
|
) {
|
||||||
|
return function ReffuseReactContextProvider(props: Props<R>) {
|
||||||
|
const value = React.useMemo(() => ({
|
||||||
|
runtime,
|
||||||
|
context: Effect.context<R>().pipe(
|
||||||
|
Effect.provide(props.layer),
|
||||||
|
Runtime.runSync(runtime),
|
||||||
|
),
|
||||||
|
}), [props.layer])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Context
|
||||||
|
{...props}
|
||||||
|
value={value}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user