<S extends ReadonlyArray<Key<any, any>>>(...services: S): <Services>(
self: Context<Services>
) => Context<Services & Service.Identifier<S[number]>>Returns a new Context that contains only the specified services.
When to use
Use when you want to keep an allowlist of services in a Context.
Example (Picking services from a context)
import { Context, Option, pipe } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const someContext = pipe(
Context.make(Port, { PORT: 8080 }),
Context.add(Timeout, { TIMEOUT: 5000 })
)
const context = pipe(someContext, Context.pick(Port))
assert.deepStrictEqual(
Context.getOption(context, Port),
Option.some({ PORT: 8080 })
)
assert.deepStrictEqual(Context.getOption(context, Timeout), Option.none())export const const pick: <
S extends ReadonlyArray<Key<any, any>>
>(
...services: S
) => <Services>(
self: Context<Services>
) => Context<
Services & Service.Identifier<S[number]>
>
Returns a new Context that contains only the specified services.
When to use
Use when you want to keep an allowlist of services in a Context.
Example (Picking services from a context)
import { Context, Option, pipe } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const someContext = pipe(
Context.make(Port, { PORT: 8080 }),
Context.add(Timeout, { TIMEOUT: 5000 })
)
const context = pipe(someContext, Context.pick(Port))
assert.deepStrictEqual(
Context.getOption(context, Port),
Option.some({ PORT: 8080 })
)
assert.deepStrictEqual(Context.getOption(context, Timeout), Option.none())
pick = <function (type parameter) S in <S extends ReadonlyArray<Key<any, any>>>(...services: S): <Services>(self: Context<Services>) => Context<Services & Service.Identifier<S[number]>>S extends interface ReadonlyArray<T>ReadonlyArray<interface Key<out Identifier, out Shape>Typed identifier for a service stored in a Context.
When to use
Use as the typed handle for storing, retrieving, and requiring a specific
service in a Context.
Details
Identifier tracks the requirement in Effect types, while Shape is the
service implementation retrieved by the key. A key is also an Effect value,
so yielding it inside Effect.gen retrieves the service from the current
fiber context.
Key<any, any>>>(
...services: S extends ReadonlyArray<Key<any, any>>services: function (type parameter) S in <S extends ReadonlyArray<Key<any, any>>>(...services: S): <Services>(self: Context<Services>) => Context<Services & Service.Identifier<S[number]>>S
) =>
<function (type parameter) Services in <Services>(self: Context<Services>): Context<Services & Service.Identifier<S[number]>>Services>(self: Context<Services>(parameter) self: {
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;
}
self: interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) Services in <Services>(self: Context<Services>): Context<Services & Service.Identifier<S[number]>>Services>): interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) Services in <Services>(self: Context<Services>): Context<Services & Service.Identifier<S[number]>>Services & Service.type Service<in out Identifier, in out Shape>.Identifier<T> = T extends Key<infer I, infer _S> ? I : neverExtracts the identifier, or requirement type, associated with a Context
service key.
Example (Extracting a service identifier)
import { Context } from "effect"
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
// Extract the identifier type from a key
type DatabaseId = Context.Service.Identifier<typeof Database>
// DatabaseId is the identifier type
Identifier<function (type parameter) S in <S extends ReadonlyArray<Key<any, any>>>(...services: S): <Services>(self: Context<Services>) => Context<Services & Service.Identifier<S[number]>>S[number]>> =>
const withMapUnsafe: <Services, B>(
self: Context<Services>,
f: (map: Map<string, any>) => void
) => Context<B>
withMapUnsafe(self: Context<Services>(parameter) self: {
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;
}
self, (map: Map<string, any>map) => {
const const keySet: Set<string>keySet = new var Set: SetConstructor
new <string>(iterable?: Iterable<string> | null | undefined) => Set<string> (+1 overload)
Set(services: S extends ReadonlyArray<Key<any, any>>services.ReadonlyArray<Key<any, any>>.map<string>(callbackfn: (value: Key<any, any>, index: number, array: readonly Key<any, any>[]) => string, thisArg?: any): string[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((key: Key<any, any>(parameter) key: {
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;
}
key) => key: Key<any, any>(parameter) key: {
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;
}
key.Key<out Identifier, out Shape>.key: stringkey))
map: Map<string, any>map.Map<string, any>.forEach(callbackfn: (value: any, key: string, map: Map<string, any>) => void, thisArg?: any): voidExecutes a provided function once per each key/value pair in the Map, in insertion order.
forEach((_: any_, key: stringkey) => {
if (const keySet: Set<string>keySet.Set<string>.has(value: string): booleanhas(key: stringkey)) return
map: Map<string, any>map.Map<string, any>.delete(key: string): booleandelete(key: stringkey)
})
})