<A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<
A,
E | E1,
R1 | Exclude<R, Scope.Scope>
>Unwraps a Layer from an Effect, flattening the nested structure.
When to use
Use when you have an Effect that produces a Layer and you want to
use that layer directly.
Details
The resulting Layer will have the combined error and dependency types from both the outer Effect and the inner Layer.
Example (Unwrapping an effectful layer)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layerEffect = Effect.succeed(
Layer.succeed(Database, { query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result")) })
)
const unwrappedLayer = Layer.unwrap(layerEffect)export const const unwrap: <A, E1, R1, E, R>(
self: Effect<Layer<A, E1, R1>, E, R>
) => Layer<
A,
E | E1,
R1 | Exclude<R, Scope.Scope>
>
Unwraps a Layer from an Effect, flattening the nested structure.
When to use
Use when you have an Effect that produces a Layer and you want to
use that layer directly.
Details
The resulting Layer will have the combined error and dependency types from
both the outer Effect and the inner Layer.
Example (Unwrapping an effectful layer)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layerEffect = Effect.succeed(
Layer.succeed(Database, { query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result")) })
)
const unwrappedLayer = Layer.unwrap(layerEffect)
unwrap = <function (type parameter) A in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>A, function (type parameter) E1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E1, function (type parameter) R1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R1, function (type parameter) E in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E, function (type parameter) R in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R>(
self: Effect<Layer<A, E1, R1>, 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<interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) A in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>A, function (type parameter) E1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E1, function (type parameter) R1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R1>, function (type parameter) E in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E, function (type parameter) R in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R>
): interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) A in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>A, function (type parameter) E in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E | function (type parameter) E1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E1, function (type parameter) R1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R1 | type Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) R in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R, import ScopeScope.Scope>> => {
const const service: Context.Service<
Layer<A, E1, R1>,
Layer<A, E1, R1>
>
const service: {
key: string;
Service: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<A>, E1, R1>;
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; <…;
};
of: (this: void, self: Layer<A, E1, R1>) => Layer<A, E1, R1>;
context: (self: Layer<A, E1, R1>) => Context.Context<Layer<A, E1, R1>>;
use: (f: (service: Layer<A, E1, R1>) => Effect<A, E, R>) => Effect<A, E, Layer<A, E1, R1> | R>;
useSync: (f: (service: Layer<A, E1, R1>) => A) => Effect<A, never, Layer<A, E1, R1>>;
Identifier: Identifier;
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.const Service: {
<Identifier, Shape = Identifier>(
key: string
): Service<Identifier, Shape>
<Self, Shape>(): <
Identifier extends string,
E,
R = Types.unassigned,
Args extends ReadonlyArray<any> = never
>(
id: Identifier,
options?:
| {
readonly make:
| ((
...args: Args
) => Effect<Shape, E, R>)
| Effect<Shape, E, R>
| undefined
}
| undefined
) => ServiceClass<Self, Identifier, Shape> &
([Types.unassigned] extends [R]
? unknown
: {
readonly make: [Args] extends [never]
? Effect<Shape, E, R>
: (
...args: Args
) => Effect<Shape, E, R>
})
<Self>(): <
Identifier extends string,
Make extends
| Effect<any, any, any>
| ((...args: any) => Effect<any, any, any>)
>(
id: Identifier,
options: { readonly make: Make }
) => ServiceClass<
Self,
Identifier,
Make extends
| Effect<infer _A, infer _E, infer _R>
| ((
...args: infer _Args
) => Effect<infer _A, infer _E, infer _R>)
? _A
: never
> & { readonly make: Make }
}
Service<interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) A in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>A, function (type parameter) E1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>E1, function (type parameter) R1 in <A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>R1>>("effect/Layer/unwrap")
return const flatMap: {
<A, A2, E2, R2>(
f: (
context: Context.Context<A>
) => Layer<A2, E2, R2>
): <E, R>(
self: Layer<A, E, R>
) => Layer<A2, E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
self: Layer<A, E, R>,
f: (
context: Context.Context<A>
) => Layer<A2, E2, R2>
): Layer<A2, E | E2, R | R2>
}
flatMap(const effect: {
<I, S>(service: Context.Key<I, S>): <E, R>(
effect: Effect<S, E, R>
) => Layer<I, E, Exclude<R, Scope.Scope>>
<I, S, E, R>(
service: Context.Key<I, S>,
effect: Effect<Types.NoInfer<S>, E, R>
): Layer<I, E, Exclude<R, Scope.Scope>>
}
effect(const service: Context.Service<
Layer<A, E1, R1>,
Layer<A, E1, R1>
>
const service: {
key: string;
Service: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<A>, E1, R1>;
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; <…;
};
of: (this: void, self: Layer<A, E1, R1>) => Layer<A, E1, R1>;
context: (self: Layer<A, E1, R1>) => Context.Context<Layer<A, E1, R1>>;
use: (f: (service: Layer<A, E1, R1>) => Effect<A, E, R>) => Effect<A, E, Layer<A, E1, R1> | R>;
useSync: (f: (service: Layer<A, E1, R1>) => A) => Effect<A, never, Layer<A, E1, R1>>;
Identifier: Identifier;
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)(self: Effect<Layer<A, E1, R1>, 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), import ContextContext.const get: {
<Services, I extends Services, S>(
service: Key<I, S>
): (self: Context<Services>) => S
<Services, I extends Services, S>(
self: Context<Services>,
service: Key<I, S>
): S
}
get(const service: Context.Service<
Layer<A, E1, R1>,
Layer<A, E1, R1>
>
const service: {
key: string;
Service: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<A>, E1, R1>;
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; <…;
};
of: (this: void, self: Layer<A, E1, R1>) => Layer<A, E1, R1>;
context: (self: Layer<A, E1, R1>) => Context.Context<Layer<A, E1, R1>>;
use: (f: (service: Layer<A, E1, R1>) => Effect<A, E, R>) => Effect<A, E, Layer<A, E1, R1> | R>;
useSync: (f: (service: Layer<A, E1, R1>) => A) => Effect<A, never, Layer<A, E1, R1>>;
Identifier: Identifier;
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))
}