0.1.0 #1

Merged
Thilawyn merged 87 commits from next into master 2025-01-18 00:54:42 +01:00
2 changed files with 44 additions and 39 deletions
Showing only changes of commit 4b6cf9a46e - Show all commits

View File

@@ -17,8 +17,9 @@ export class Reffuse<
runtime: Runtime.Runtime<RuntimeR>,
parent?: Reffuse<RuntimeR, ParentContextR, unknown, unknown>,
) {
// TODO: split into makeProvider and makeScopedProvider
this.Provider = ReffuseReactContext.makeProvider(runtime, this.Context, parent)
this.Provider = parent
? ReffuseReactContext.makeNestedProvider(runtime, this.Context, parent)
: ReffuseReactContext.makeRootProvider<RuntimeR, OwnContextR>(runtime, this.Context)
}

View File

@@ -24,7 +24,29 @@ export interface ProviderProps<
readonly children?: React.ReactNode
}
export function makeProvider<
export function makeRootProvider<RuntimeR, ContextR>(
runtime: Runtime.Runtime<RuntimeR>,
ReactContext: React.Context<Value<RuntimeR, ContextR>>,
): Provider<RuntimeR, ContextR, never> {
return function ReffuseRootReactContextProvider(props) {
const value = React.useMemo(() => ({
runtime,
context: Effect.context<ContextR>().pipe(
Effect.provide(props.layer),
Runtime.runSync(runtime),
),
}), [props.layer])
return (
<ReactContext
{...props}
value={value}
/>
)
}
}
export function makeNestedProvider<
RuntimeR,
ContextR extends ParentContextR | OwnContextR,
OwnContextR,
@@ -32,10 +54,9 @@ export function makeProvider<
>(
runtime: Runtime.Runtime<RuntimeR>,
ReactContext: React.Context<Value<RuntimeR, ContextR>>,
parent?: Reffuse.Reffuse<RuntimeR, ParentContextR, unknown, unknown>,
parent: Reffuse.Reffuse<RuntimeR, ParentContextR, unknown, unknown>,
): Provider<RuntimeR, OwnContextR, ParentContextR> {
return parent
? function ReffuseReactContextProvider(props) {
return function ReffuseNestedReactContextProvider(props) {
const parentContext = parent.useContext()
const value = React.useMemo(() => ({
@@ -47,23 +68,6 @@ export function makeProvider<
),
}), [props.layer, parentContext])
return (
<ReactContext
{...props}
value={value}
/>
)
}
: function ReffuseReactContextProvider(props) {
const value = React.useMemo(() => ({
runtime,
context: Effect.context<ContextR>().pipe(
Effect.provide(props.layer),
Effect.provide(Context.empty() as Context.Context<ParentContextR>), // Required for type safety
Runtime.runSync(runtime),
),
}), [props.layer])
return (
<ReactContext
{...props}