Skip to content

add RuntimeContext module #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/FiberStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type * as Runtime from "@effect/io/Runtime"
/**
* @since 1.0.0
*/
import type * as Stream from "@effect/stream/Stream"
import * as internal from "effect-react/internal/fiberStore"
import type * as ResultBag from "effect-react/ResultBag"
import type * as RuntimeContext from "effect-react/RuntimeContext"

/**
* @since 1.0.0
Expand All @@ -18,4 +21,4 @@ export interface FiberStore<R, E, A> {
* @since 1.0.0
* @category constructors
*/
export const make: <R, E, A>(runtime: Runtime.Runtime<R>) => FiberStore<R, E, A> = internal.make
export const make: <R, E, A>(runtime: RuntimeContext.RuntimeEffect<R>) => FiberStore<R, E, A> = internal.make
43 changes: 43 additions & 0 deletions src/Hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @since 1.0.0
*/
import type * as Stream from "@effect/stream/Stream"
import * as internal from "effect-react/internal/hooks"
import type * as ResultBag from "effect-react/ResultBag"
import type * as RuntimeContext from "effect-react/RuntimeContext"
import type { DependencyList } from "react"

/**
* @since 1.0.0
* @category constructors
*/
export const make: <R>(
runtimeContext: RuntimeContext.ReactContext<R>
) => {
useResult: <R0 extends R, E, A>(
evaluate: () => Stream.Stream<R0, E, A>,
deps: DependencyList
) => ResultBag.ResultBag<unknown, A>
useResultCallback: <Args extends Array<any>, R0 extends R, E, A>(
f: (...args: Args) => Stream.Stream<R0, E, A>
) => readonly [ResultBag.ResultBag<unknown, A>, (...args: Args) => void]
} = internal.make

/**
* @since 1.0.0
* @category constructors
*/
export const makeUseResult: <R>(
runtimeContext: RuntimeContext.ReactContext<R>
) => <R0 extends R, E, A>(evaluate: () => Stream.Stream<R0, E, A>, deps: DependencyList) => ResultBag.ResultBag<E, A> =
internal.makeUseResult

/**
* @since 1.0.0
* @category constructors
*/
export const makeUseResultCallback: <R>(
runtimeContext: RuntimeContext.ReactContext<R>
) => <Args extends Array<any>, R0 extends R, E, A>(
f: (...args: Args) => Stream.Stream<R0, E, A>
) => readonly [ResultBag.ResultBag<E, A>, (...args: Args) => void] = internal.makeUseResultCallback
3 changes: 3 additions & 0 deletions src/Result.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @since 1.0.0
*/
import type * as Data from "@effect/data/Data"
import type * as Option from "@effect/data/Option"
import type { Pipeable } from "@effect/data/Pipeable"
Expand Down
5 changes: 4 additions & 1 deletion src/ResultBag.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/**
* @since 1.0.0
*/
import type * as Option from "@effect/data/Option"
import type * as Cause from "@effect/io/Cause"
import * as internal from "effect-react/internal/resultBag"
import type * as Result from "effect-react/Result"
import type * as TrackedProperties from "effect-react/TrackedProperties"
import type * as TrackedProperties from "effect-react/ResultBag/TrackedProperties"

/**
* @since 1.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* @since 1.0.0
*/
import type * as Option from "@effect/data/Option"
import type * as Cause from "@effect/io/Cause"
import * as internal from "effect-react/internal/trackedProperties"
import * as internal from "effect-react/internal/resultBag/trackedProperties"
import type * as Result from "effect-react/Result"

/**
Expand Down
187 changes: 187 additions & 0 deletions src/RuntimeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* @since 1.0.0
*/
import type * as Context from "@effect/data/Context"
import { dual, pipe } from "@effect/data/Function"
import * as Effect from "@effect/io/Effect"
import * as Exit from "@effect/io/Exit"
import * as Fiber from "@effect/io/Fiber"
import * as Layer from "@effect/io/Layer"
import * as Runtime from "@effect/io/Runtime"
import * as Scope from "@effect/io/Scope"
import * as React from "react"

/**
* @since 1.0.0
* @category type ids
*/
export const RuntimeContextTypeId = Symbol.for("@effect/react/RuntimeContext")

/**
* @since 1.0.0
* @category type ids
*/
export type RuntimeContextTypeId = typeof RuntimeContextTypeId

/**
* @since 1.0.0
* @category models
*/
export interface RuntimeContext<R> extends ReactContext<R> {
readonly [RuntimeContextTypeId]: {
readonly scope: Scope.CloseableScope
readonly context: Effect.Effect<never, never, Context.Context<R>>
}
}
Comment on lines +30 to +35
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we want to ride the ReactContext like that. It's mostly likely safe, but it's an eye raising pattern 🤔


/**
* @since 1.0.0
* @category models
*/
export type ReactContext<R> = React.Context<Effect.Effect<never, never, Runtime.Runtime<R>>>

/**
* @since 1.0.0
* @category models
*/
export type RuntimeEffect<R> = Effect.Effect<never, never, Runtime.Runtime<R>>

/**
* @since 1.0.0
* @category constructors
*/
export const fromContext = <R>(
context: Context.Context<R>
): RuntimeContext<R> => fromContextEffect(Effect.succeed(context))

const makeRuntimeEffect = <R>(context: Effect.Effect<never, never, Context.Context<R>>): RuntimeEffect<R> => {
const runtime = pipe(
context,
Effect.flatMap((context) => Effect.provideContext(Effect.runtime<R>(), context)),
Effect.cached,
Effect.runSync
)
Effect.runFork(runtime) // prime cache
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will very likely break in ssr environments 🤔

return runtime
}

/**
* @since 1.0.0
* @category constructors
*/
export const fromContextEffect = <R, E>(
effect: Effect.Effect<Scope.Scope, E, Context.Context<R>>
): RuntimeContext<R> => {
const scope = Effect.runSync(Scope.make())
const error = new Error()
const context = Scope.use(
Effect.orDieWith(effect, (e) => {
error.message = `Could not build RuntimeContext: ${e}`
return error
}),
scope
)
const runtime = makeRuntimeEffect(context)
const RuntimeContext = React.createContext(runtime)
return new Proxy(RuntimeContext, {
has(target, p) {
return p === RuntimeContextTypeId || p in target
},
get(target, p, _receiver) {
if (p === RuntimeContextTypeId) {
return {
scope,
context
}
}
return (target as any)[p]
}
}) as any
}

/**
* @since 1.0.0
* @category constructors
*/
export const fromLayer = <R, E>(layer: Layer.Layer<never, E, R>): RuntimeContext<R> =>
fromContextEffect(Effect.runSync(Effect.cached(Layer.build(layer))))

/**
* @since 1.0.0
* @category combinators
*/
export const provideMerge = dual<
<R, RX extends R, R2, E>(
layer: Layer.Layer<RX, E, R2>
) => (self: RuntimeContext<R>) => RuntimeContext<R | R2>,
<R, RX extends R, R2, E>(
self: RuntimeContext<R>,
layer: Layer.Layer<RX, E, R2>
) => RuntimeContext<R | R2>
>(2, (self, layer) => {
const context = self[RuntimeContextTypeId].context
return fromLayer(Layer.provideMerge(Layer.effectContext(context), layer))
})

/**
* @since 1.0.0
* @category combinators
*/
export const toRuntimeEffect = <R>(
self: RuntimeContext<R>
): RuntimeEffect<R> => makeRuntimeEffect(self[RuntimeContextTypeId].context)

/**
* @since 1.0.0
* @category combinators
*/
export const overrideLayer = dual<
<R, RX extends R, E>(
layer: Layer.Layer<never, E, RX>
) => (self: RuntimeContext<R>) => readonly [React.ExoticComponent<React.PropsWithChildren>, Scope.CloseableScope],
<R, E>(
self: RuntimeContext<R>,
layer: Layer.Layer<never, E, R>
) => readonly [React.ExoticComponent<React.PropsWithChildren>, Scope.CloseableScope]
>(2, (self, layer) => {
const context = fromLayer(layer)
const runtime = toRuntimeEffect(context)
return [
(props: React.PropsWithChildren) => React.createElement(self.Provider, { value: runtime }, props.children),
context[RuntimeContextTypeId].scope
] as any
})

/**
* @since 1.0.0
* @category combinators
*/
export const closeEffect = <R>(self: RuntimeContext<R>): Effect.Effect<never, never, void> =>
Scope.close(self[RuntimeContextTypeId].scope, Exit.unit)

/**
* @since 1.0.0
* @category combinators
*/
export const close = <R>(self: RuntimeContext<R>): () => void => {
const effect = closeEffect(self)
return () => {
Effect.runFork(effect)
}
}

/**
* @since 1.0.0
* @category execution
*/
export const runForkJoin = <R>(
runtime: RuntimeEffect<R>
) =>
<RX extends R, E, A>(effect: Effect.Effect<RX, E, A>): Effect.Effect<never, E, A> =>
Effect.flatMap(
runtime,
(runtime) => {
const fiber = Runtime.runFork(runtime)(effect)
return Fiber.join(fiber)
}
)
100 changes: 0 additions & 100 deletions src/RuntimeProvider.ts

This file was deleted.

Loading