<I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(
self: Effect<XA, E, R>
) => Effect<XA, E, R | I>
<XA, E, R, I, A>(
self: Effect<XA, E, R>,
service: Context.Key<I, A>,
f: (value: A) => A
): Effect<XA, E, R | I>Runs an effect with a service implementation transformed by the provided function.
Details
The service must be available in the effect's context; updateService
replaces it for the wrapped effect with the value returned by the updater.
Example (Replacing a service for one effect)
import { Console, Context, Effect } from "effect"
// Define a counter service
const Counter = Context.Service<{ count: number }>("Counter")
const program = Effect.gen(function*() {
const updatedCounter = yield* Effect.service(Counter)
yield* Console.log(`Updated count: ${updatedCounter.count}`)
return updatedCounter.count
}).pipe(
Effect.updateService(Counter, (counter) => ({ count: counter.count + 1 }))
)
// Provide initial service and run
const result = Effect.provideService(program, Counter, { count: 0 })
Effect.runPromise(result).then(console.log)
// Output: Updated count: 1
// 1export const const updateService: {
<I, A>(
service: Context.Key<I, A>,
f: (value: A) => A
): <XA, E, R>(
self: Effect<XA, E, R>
) => Effect<XA, E, R | I>
<XA, E, R, I, A>(
self: Effect<XA, E, R>,
service: Context.Key<I, A>,
f: (value: A) => A
): Effect<XA, E, R | I>
}
Runs an effect with a service implementation transformed by the provided
function.
Details
The service must be available in the effect's context; updateService
replaces it for the wrapped effect with the value returned by the updater.
Example (Replacing a service for one effect)
import { Console, Context, Effect } from "effect"
// Define a counter service
const Counter = Context.Service<{ count: number }>("Counter")
const program = Effect.gen(function*() {
const updatedCounter = yield* Effect.service(Counter)
yield* Console.log(`Updated count: ${updatedCounter.count}`)
return updatedCounter.count
}).pipe(
Effect.updateService(Counter, (counter) => ({ count: counter.count + 1 }))
)
// Provide initial service and run
const result = Effect.provideService(program, Counter, { count: 0 })
Effect.runPromise(result).then(console.log)
// Output: Updated count: 1
// 1
updateService: {
<function (type parameter) I in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>I, function (type parameter) A in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>A>(
service: Context.Key<I, A>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
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;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>I, function (type parameter) A in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>A>,
f: (value: A) => Af: (value: Avalue: function (type parameter) A in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>A) => function (type parameter) A in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>A
): <function (type parameter) XA in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>XA, function (type parameter) E in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>E, function (type parameter) R in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>R>(self: Effect<XA, E, R>(parameter) self: {
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;
}
self: 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) XA in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>XA, function (type parameter) E in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>E, function (type parameter) R in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>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) XA in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>XA, function (type parameter) E in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>E, function (type parameter) R in <XA, E, R>(self: Effect<XA, E, R>): Effect<XA, E, R | I>R | function (type parameter) I in <I, A>(service: Context.Key<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>I>
<function (type parameter) XA in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>XA, function (type parameter) E in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>E, function (type parameter) R in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>R, function (type parameter) I in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>I, function (type parameter) A in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>A>(
self: Effect<XA, E, R>(parameter) self: {
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;
}
self: 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) XA in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>XA, function (type parameter) E in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>E, function (type parameter) R in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>R>,
service: Context.Key<I, A>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
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;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>I, function (type parameter) A in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>A>,
f: (value: A) => Af: (value: Avalue: function (type parameter) A in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>A) => function (type parameter) A in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>A
): 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) XA in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>XA, function (type parameter) E in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>E, function (type parameter) R in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>R | function (type parameter) I in <XA, E, R, I, A>(self: Effect<XA, E, R>, service: Context.Key<I, A>, f: (value: A) => A): Effect<XA, E, R | I>I>
} = import internalinternal.const updateService: {
<I, A>(
service: Context.Key<I, A>,
f: (value: A) => A
): <XA, E, R>(
self: Effect.Effect<XA, E, R>
) => Effect.Effect<XA, E, R | I>
<XA, E, R, I, A>(
self: Effect.Effect<XA, E, R>,
service: Context.Key<I, A>,
f: (value: A) => A
): Effect.Effect<XA, E, R | I>
}
updateService