Hyperlinkv0.8.0-beta.28

Context

Context.pickconsteffect/Context.ts:1192
<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())
filteringomit
Source effect/Context.ts:119211 lines
export const pick = <S extends ReadonlyArray<Key<any, any>>>(
  ...services: S
) =>
<Services>(self: Context<Services>): Context<Services & Service.Identifier<S[number]>> =>
  withMapUnsafe(self, (map) => {
    const keySet = new Set(services.map((key) => key.key))
    map.forEach((_, key) => {
      if (keySet.has(key)) return
      map.delete(key)
    })
  })
Referenced by 1 symbols