-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
tim-smart
wants to merge
4
commits into
datner:main
Choose a base branch
from
tim-smart:runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
} | ||
} | ||
|
||
/** | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🤔