Hyperlinkv0.8.0-beta.28

Context

Context.addOrOmitconsteffect/Context.ts:737
<I, S>(key: Key<I, S>, service: Option.Option<Types.NoInfer<S>>): <
  Services
>(
  self: Context<Services>
) => Context<Services | I>
<Services, I, S>(
  self: Context<Services>,
  key: Key<I, S>,
  service: Option.Option<Types.NoInfer<S>>
): Context<Services | I>

Adds or removes a service depending on an Option.

When to use

Use when you need to add or omit a Context service based on an Option.

Details

When service is Option.some, the value is stored for the key. When it is Option.none, the key is removed from the returned Context.

Example (Adding optional services)

import { Context, Option } from "effect"

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

const withPort = Context.empty().pipe(
  Context.addOrOmit(Port, Option.some({ PORT: 8080 }))
)

const withoutPort = withPort.pipe(
  Context.addOrOmit(Port, Option.none())
)
addersadd
Source effect/Context.ts:73722 lines
export const addOrOmit: {
  <I, S>(
    key: Key<I, S>,
    service: Option.Option<Types.NoInfer<S>>
  ): <Services>(self: Context<Services>) => Context<Services | I>
  <Services, I, S>(
    self: Context<Services>,
    key: Key<I, S>,
    service: Option.Option<Types.NoInfer<S>>
  ): Context<Services | I>
} = dual(3, <Services, I, S>(
  self: Context<Services>,
  key: Key<I, S>,
  service: Option.Option<Types.NoInfer<S>>
): Context<Services | I> =>
  withMapUnsafe(self, (map) => {
    if (service._tag === "None") {
      map.delete(key.key)
    } else {
      map.set(key.key, service.value)
    }
  }))
Referenced by 1 symbols