Hyperlinkv0.8.0-beta.28

Context

Context.mergeconsteffect/Context.ts:1092
<R1>(that: Context<R1>): <Services>(
  self: Context<Services>
) => Context<R1 | Services>
<Services, R1>(self: Context<Services>, that: Context<R1>): Context<
  Services | R1
>

Merges two Contexts into one.

When to use

Use when you need to combine two contexts.

Details

When both contexts contain the same service key, the service from that overrides the service from self.

Example (Merging two contexts)

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

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

const firstContext = Context.make(Port, { PORT: 8080 })
const secondContext = Context.make(Timeout, { TIMEOUT: 5000 })

const context = Context.merge(firstContext, secondContext)

assert.deepStrictEqual(Context.get(context, Port), { PORT: 8080 })
assert.deepStrictEqual(Context.get(context, Timeout), { TIMEOUT: 5000 })
combiningmergeAll
Source effect/Context.ts:109210 lines
export const merge: {
  <R1>(that: Context<R1>): <Services>(self: Context<Services>) => Context<R1 | Services>
  <Services, R1>(self: Context<Services>, that: Context<R1>): Context<Services | R1>
} = dual(2, <Services, R1>(self: Context<Services>, that: Context<R1>): Context<Services | R1> => {
  if (self.mapUnsafe.size === 0) return that as any
  if (that.mapUnsafe.size === 0) return self as any
  return withMapUnsafe(self, (map) => {
    that.mapUnsafe.forEach((value, key) => map.set(key, value))
  })
})
Referenced by 6 symbols