Skip to content

RcMap

10 exports Added in v3.5.0 Source

Combinators

get

Added in v3.5.0 Source

Signature

declare const get: {
  <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect<A, E, Scope>;
  <K, A, E>(self: RcMap<K, A, E>, key: K): Effect<A, E, Scope>;
};

has

Added in v3.17.7 Source

Signature

declare const has: {
  <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect<boolean>;
  <K, A, E>(self: RcMap<K, A, E>, key: K): Effect<boolean>;
};

invalidate

Added in v3.13.0 Source

Signature

declare const invalidate: {
  <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect<void>;
  <K, A, E>(self: RcMap<K, A, E>, key: K): Effect<void>;
};

keys

Added in v3.8.0 Source

Signature

declare const keys: <K, A, E>(self: RcMap<K, A, E>) => Effect.Effect<Array<K>>;

touch

Added in v3.13.0 Source

Signature

declare const touch: {
  <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect<void>;
  <K, A, E>(self: RcMap<K, A, E>, key: K): Effect<void>;
};

Models

make

Added in v3.5.0 Source

An RcMap can contain multiple reference counted resources that can be indexed by a key. The resources are lazily acquired on the first call to get and released when the last reference is released.

Complex keys can extend Equal and Hash to allow lookups by value.

Options

- capacity: The maximum number of resources that can be held in the map. - idleTimeToLive: When the reference count reaches zero, the resource will be released after this duration. Can be a static duration or a function that returns a duration based on the key.

Signature

declare const make: {
  <K, A, E, R>(options: {
    readonly capacity?: undefined;
    readonly idleTimeToLive?: Duration.DurationInput | (key: K) => Duration.DurationInput;
    readonly lookup: (key: K) => Effect.Effect<A, E, R>;
  }): Effect<RcMap<K, A, E>, never, Scope | R>;
  <K, A, E, R>(options: {
    readonly capacity: number;
    readonly idleTimeToLive?: Duration.DurationInput | (key: K) => Duration.DurationInput;
    readonly lookup: (key: K) => Effect.Effect<A, E, R>;
  }): Effect<RcMap<K, A, ExceededCapacityException | E>, never, Scope | R>;
}

Example

import { Effect, RcMap } from "effect"

Effect.gen(function* () {
  const map = yield* RcMap.make({
    lookup: (key: string) =>
      Effect.acquireRelease(Effect.succeed(`acquired ${key}`), () =>
        Effect.log(`releasing ${key}`),
      ),
  })

  // Get "foo" from the map twice, which will only acquire it once.
  // It will then be released once the scope closes.
  yield* RcMap.get(map, "foo").pipe(Effect.andThen(RcMap.get(map, "foo")), Effect.scoped)
})

RcMap

Added in v3.5.0 Source

RcMap interface

Added in v3.5.0 Source

Signature

interface RcMap<in K, out A, out E = never> extends Pipeable {
  readonly [TypeId]: Variance<K, A, E>;
}

Type Ids

TypeId

Added in v3.5.0 Source

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v3.5.0 Source

Signature

type TypeId = typeof TypeId;