Hyperlinkv0.8.0-beta.28

Context

Context.getReferenceUnsafeconsteffect/Context.ts:973
<Services, S>(self: Context<Services>, service: Reference<S>): S

Gets the value for a Context.Reference, returning its cached default when the context does not contain an override.

When to use

Use when you need a Context.Reference value resolved from either a stored override or the reference's default value.

Details

Stored overrides take precedence. If no override is present, the reference's default value is computed lazily and cached on the reference itself.

Gotchas

Mutable default values can be shared across contexts unless an override is provided, because the default is cached on the Context.Reference.

Example (Getting reference defaults unsafely)

import { Context } from "effect"

const LoggerRef = Context.Reference("Logger", {
  defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})

const context = Context.empty()
const logger = Context.getReferenceUnsafe(context, LoggerRef)

console.log(typeof logger.log) // "function"
Source effect/Context.ts:9736 lines
export const getReferenceUnsafe = <Services, S>(self: Context<Services>, service: Reference<S>): S => {
  if (!self.mapUnsafe.has(service.key)) {
    return getDefaultValue(service as any)
  }
  return self.mapUnsafe.get(service.key)! as any
}
Referenced by 1 symbols