Hyperlinkv0.8.0-beta.28

Context

Context.getOptionconsteffect/Context.ts:1047
<S, I>(service: Key<I, S>): <Services>(
  self: Context<Services>
) => Option.Option<S>
<Services, S, I>(
  self: Context<Services>,
  service: Key<I, S>
): Option.Option<S>

Gets the service for a key safely wrapped in an Option.

When to use

Use when you need to read a Context service as an Option so absence is represented as data.

Details

Returns Option.some when the service is stored in the context. If the key is a Context.Reference and no override is stored, returns Option.some of the cached default value. Missing non-reference keys return Option.none.

Example (Getting optional services)

import { Context, Option } from "effect"
import * as assert from "node:assert"

const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")

const context = Context.make(Port, { PORT: 8080 })

assert.deepStrictEqual(
  Context.getOption(context, Port),
  Option.some({ PORT: 8080 })
)
assert.deepStrictEqual(Context.getOption(context, Timeout), Option.none())
gettersgetOrElse
export const getOption: {
  <S, I>(service: Key<I, S>): <Services>(self: Context<Services>) => Option.Option<S>
  <Services, S, I>(self: Context<Services>, service: Key<I, S>): Option.Option<S>
} = dual(2, <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): Option.Option<S> => {
  if (self.mapUnsafe.has(service.key)) {
    return Option.some(self.mapUnsafe.get(service.key)! as any)
  }
  return isReference(service) ? Option.some(getDefaultValue(service as any)) : Option.none()
})
Referenced by 2 symbols