Skip to content

LayerMap

Caches scoped services selected by key and built from layers.

A LayerMap<K, I, E> turns a key into a cached service Context<I> and exposes that context as either a Layer or a scoped effect. Entries can be invalidated explicitly or released after they sit unused. This is useful for keyed resource families such as tenant clients, regional connections, or environment-specific services.

6 exports Added in v3.14.0 Source

Constructors

fromRecord

Added in v3.14.0 Source

Creates a LayerMap from a record of predefined layers.

Details

The record keys become the keys accepted by the returned LayerMap, and the record values are the layers built for those keys.

Signature

declare function fromRecord<
  Layers extends Record<string, Layer<any, any, any>>,
  Preload extends boolean = false,
>(
  layers: Layers,
  options?: {
    readonly idleTimeToLive?: IdleTimeToLiveInput<keyof Layers>;
    readonly preload?: Preload;
  },
): Effect<
  LayerMap<keyof Layers, Success<Layers[keyof Layers]>, Error<Layers[keyof Layers]>>,
  Preload extends true ? Error<Layers[keyof Layers]> : never,
  Scope | Layers[keyof Layers] extends Layer<_A, _E, _R> ? _R : never
>;

make

Added in v3.14.0 Source

Creates a LayerMap that dynamically provides resources based on a key.

Signature

declare const make: <
  K,
  L extends Layer.Layer<any, any, any>,
  PreloadKeys extends Iterable<K> | undefined = undefined,
>(
  lookup: (key: K) => L,
  options?: {
    readonly idleTimeToLive?: IdleTimeToLiveInput<K>;
    readonly preloadKeys?: PreloadKeys;
  },
) => Effect.Effect<
  LayerMap<K, Layer.Success<L>, Layer.Error<L>>,
  PreloadKeys extends undefined ? never : Layer.Error<L>,
  Scope.Scope | Layer.Services<L>
>;

Models

LayerMap interface

Added in v3.14.0 Source

A scoped, keyed map of layer-built service contexts.

Details

A LayerMap builds resources for a key on demand, exposes them as a Layer or scoped Context, and can invalidate cached resources for a key.

Signature

interface LayerMap<in out K, in out I, in out E = never> {
  readonly "~effect/LayerMap": "~effect/LayerMap";
  readonly rcMap: RcMap<K, Context<I>, E>;
  contextEffect(key: K): Effect<Context<I>, E, Scope>;
  get(key: K): Layer<I, E>;
  invalidate(key: K): Effect<void>;
}

Other

Service

Added in v3.14.0 Source

Type helpers for values created with LayerMap.Service.

Services

Service

Added in v3.14.0 Source

Create a LayerMap service that provides a dynamic set of resources based on a key.

Signature

declare function Service<Self>(): <Id extends string, Options extends NoExcessProperties<{
  readonly dependencies?: readonly Array<Layer<any, any, any>>;
  readonly idleTimeToLive?: IdleTimeToLiveInput<any>;
  readonly lookup: (key: any) => Layer<any, any, any>;
  readonly preloadKeys?: Iterable<Options extends {
    readonly lookup: (key: K) => any;
  } ? K : never, any, any>;
}, Options> | NoExcessProperties<{
  readonly dependencies?: readonly Array<Layer<any, any, any>>;
  readonly idleTimeToLive?: IdleTimeToLiveInput<any>;
  readonly layers: Record<string, Layer.Layer<any, any, any>>;
  readonly preload?: boolean;
}, Options>>(id: Id, options: Options) => TagClass<Self, Id, Options extends {
  readonly lookup: (key: K) => any;
} ? K : Options extends {
  readonly layers: Layers;
} ? keyof Layers : never, Success<Options>, Options extends {
  readonly preload: true;
} ? never : Error<Options>, Services<Options>, Options extends {
  readonly preload: true;
} ? Error<Options> : Options extends {
  readonly preloadKeys: Iterable<any>;
} ? Error<Options> : never, Options extends {
  readonly dependencies: readonly Array<Layer<any, any, any>>;
} ? Options["dependencies"][number] : never>

TagClass interface

Added in v3.14.0 Source

Service class shape produced by LayerMap.Service.

When to use

Use as the public type for classes returned by LayerMap.Service when an API needs to accept, return, or alias the generated service class and its static helpers.

Details

It combines a Context.Service tag for the LayerMap with default layers and helper accessors for retrieving, using, and invalidating keyed resources.

See

  • Service for creating concrete LayerMap service classes

Signature

interface TagClass<
  in out Self,
  in out Id extends string,
  in out K,
  in out I,
  in out E,
  in out R,
  in out LE,
  in out Deps extends Layer.Layer<any, any, any>,
> extends ServiceClass<Self, Id, LayerMap<K, I, E>> {
  constructor(_: never);
  readonly contextEffect: (key: K) => Effect<Context<I>, E, Scope | Self>;
  readonly get: (key: K) => Layer<I, E, Self>;
  readonly invalidate: (key: K) => Effect<void, never, Self>;
  readonly layer: Layer<
    Self,
    LE | Deps extends Layer<_A, _E, _R> ? _E : never,
    Exclude<R, Deps extends Layer<_A, _E, _R> ? _A : never> | Deps extends Layer<_A, _E, _R>
      ? _R
      : never
  >;
  readonly layerNoDeps: Layer<Self, LE, R>;
}