Skip to content
Liminal

Prelude vs Layer

The prelude and layer fields in ActorRuntime.make(...) serve different lifetimes.

Use prelude for long-lived infrastructure:

  • database access
  • config
  • logging
  • asset bindings
  • Cloudflare service bindings

Use layer for short-lived context derived from the current client-specific actor invocation:

  • current user id
  • current authorization

Derive request context

The next example builds a layer that provides an actor-specific CurrentUserId derived from two different actor definitions.

 
export class  extends .<
  ,
  typeof .
>()("CurrentUserId") {}
 
// Use the actor name as the id.
export const  = .(function* () {
  const {  } = yield* 
  return 
}).(.())
 
// Use the current client attachment as the id.
export const  = .(function* () {
  const {  } = yield* 
  const {  } = yield* .
  return 
}).(.())

In Effect v4, context is defined with Context.Service<Self, A>()(id). This replaces Context.Tag from earlier versions.

Read Actor Context for the values available through yield* ActorTag.