<R, A, E, R2>(
f: (context: Context.Context<R>) => Effect<A, E, R2>
): Effect<A, E, R | R2>Transforms the current context using the provided function.
When to use
Use to derive an effect from the complete Context.
Details
This function allows you to access the complete context and perform computations based on all available services. This is useful when you need to conditionally execute logic based on what services are available.
Example (Deriving values from the context)
import { Console, Context, Effect, Option } from "effect"
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
const Cache = Context.Service<{
get: (key: string) => string | null
}>("Cache")
const program = Effect.contextWith((services) => {
const cacheOption = Context.getOption(services, Cache)
const hasCache = Option.isSome(cacheOption)
if (hasCache) {
return Effect.gen(function*() {
const cache = yield* Effect.service(Cache)
yield* Console.log("Using cached data")
return cache.get("user:123") || "default"
})
} else {
return Effect.gen(function*() {
yield* Console.log("No cache available, using fallback")
return "fallback data"
})
}
})
const withCache = Effect.provideService(program, Cache, {
get: () => "cached_value"
})export const const contextWith: <R, A, E, R2>(
f: (
context: Context.Context<R>
) => Effect<A, E, R2>
) => Effect<A, E, R | R2>
Transforms the current context using the provided function.
When to use
Use to derive an effect from the complete Context.
Details
This function allows you to access the complete context and perform
computations based on all available services. This is useful when you need
to conditionally execute logic based on what services are available.
Example (Deriving values from the context)
import { Console, Context, Effect, Option } from "effect"
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
const Cache = Context.Service<{
get: (key: string) => string | null
}>("Cache")
const program = Effect.contextWith((services) => {
const cacheOption = Context.getOption(services, Cache)
const hasCache = Option.isSome(cacheOption)
if (hasCache) {
return Effect.gen(function*() {
const cache = yield* Effect.service(Cache)
yield* Console.log("Using cached data")
return cache.get("user:123") || "default"
})
} else {
return Effect.gen(function*() {
yield* Console.log("No cache available, using fallback")
return "fallback data"
})
}
})
const withCache = Effect.provideService(program, Cache, {
get: () => "cached_value"
})
contextWith: <function (type parameter) R in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>R, function (type parameter) A in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>A, function (type parameter) E in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>E, function (type parameter) R2 in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>R2>(
f: (
context: Context.Context<R>
) => Effect<A, E, R2>
f: (context: Context.Context<R>(parameter) context: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
context: import ContextContext.type Context.Context = /*unresolved*/ anyContext<function (type parameter) R in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>R>) => interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>A, function (type parameter) E in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>E, function (type parameter) R2 in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>R2>
) => interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>A, function (type parameter) E in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>E, function (type parameter) R in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>R | function (type parameter) R2 in <R, A, E, R2>(f: (context: Context.Context<R>) => Effect<A, E, R2>): Effect<A, E, R | R2>R2> = import internalinternal.const contextWith: <R, A, E, R2>(
f: (
context: Context.Context<R>
) => Effect.Effect<A, E, R2>
) => Effect.Effect<A, E, R | R2>
contextWith