Skip to content

TxHashMap

Transactional hash maps for storing and updating key-value pairs inside Effect transactions.

A TxHashMap stores an immutable HashMap in a TxRef, so map reads and writes can commit atomically with other transactional operations. Use it for shared registries, counters, indexes, and other maps that need safe read-modify-write sequences alongside related transactional state.

38 exports Added in v2.0.0 Source

Combinators

clear

Added in v4.0.0 Source

Removes all entries from the TxHashMap.

Details

This function mutates the original TxHashMap by clearing all key-value pairs. It does not return a new TxHashMap reference.

Signature

declare function clear<K, V>(self: TxHashMap<K, V>): Effect<void>;

compact

Added in v4.0.0 Source

Removes all None values from a TxHashMap containing Option values.

Details

This function returns a new TxHashMap reference with only the Some values unwrapped. The original TxHashMap is not modified.

Signature

declare function compact<K, A>(self: TxHashMap<K, Option<A>>): Effect<TxHashMap<K, A>>;

entries

Added in v4.0.0 Source

Returns an array of all key-value pairs in the TxHashMap.

Signature

declare function entries<K, V>(self: TxHashMap<K, V>): Effect<Array<readonly [K, V]>>;

filter

Added in v4.0.0 Source

Filters the TxHashMap to keep only entries that satisfy the provided predicate.

Details

This function returns a new TxHashMap reference containing only the entries that match the condition. The original TxHashMap is not modified.

Signature

declare const filter: {
  <K, V, B>(
    predicate: (value: V, key: K) => value is B,
  ): (self: TxHashMap<K, V>) => Effect<TxHashMap<K, B>>;
  <K, V>(
    predicate: (value: V, key: K) => boolean,
  ): (self: TxHashMap<K, V>) => Effect<TxHashMap<K, V>>;
  <K, V, B>(
    self: TxHashMap<K, V>,
    predicate: (value: V, key: K) => value is B,
  ): Effect<TxHashMap<K, B>>;
  <K, V>(self: TxHashMap<K, V>, predicate: (value: V, key: K) => boolean): Effect<TxHashMap<K, V>>;
};

filterMap

Added in v4.0.0 Source

Combines filtering and mapping in a single operation. Applies a filter to each entry, keeping only successful results and transforming them.

Details

This function returns a new TxHashMap reference containing only the transformed entries that succeeded. The original TxHashMap is not modified.

Signature

declare const filterMap: {
  <V, K, A, X>(
    f: (input: V, key: K) => Result<A, X>,
  ): (self: TxHashMap<K, V>) => Effect<TxHashMap<K, A>>;
  <K, V, A, X>(
    self: TxHashMap<K, V>,
    f: (input: V, key: K) => Result<A, X>,
  ): Effect<TxHashMap<K, A>>;
};

findFirst

Added in v4.0.0 Source

Finds the first entry in the TxHashMap that matches the given predicate. Returns the key-value pair as a tuple wrapped in an Option.

Signature

declare const findFirst: {
  <K, V>(
    predicate: (value: V, key: K) => boolean,
  ): (self: TxHashMap<K, V>) => Effect<Option<[K, V]>>;
  <K, V>(self: TxHashMap<K, V>, predicate: (value: V, key: K) => boolean): Effect<Option<[K, V]>>;
};

flatMap

Added in v4.0.0 Source

Maps each entry effectfully to a TxHashMap and flattens the produced maps.

Details

This function returns a new TxHashMap reference with the flattened results. The original TxHashMap is not modified.

Signature

declare const flatMap: {
  <A, V, K>(
    f: (value: V, key: K) => Effect<TxHashMap<K, A>>,
  ): (self: TxHashMap<K, V>) => Effect<TxHashMap<K, A>>;
  <K, V, A>(
    self: TxHashMap<K, V>,
    f: (value: V, key: K) => Effect<TxHashMap<K, A>>,
  ): Effect<TxHashMap<K, A>>;
};

forEach

Added in v2.0.0 Source

Executes a side-effect function for each entry in the TxHashMap. The function receives the value and key as parameters and can perform effects.

Signature

declare const forEach: {
  <V, K, R, E>(
    f: (value: V, key: K) => Effect<void, E, R>,
  ): (self: TxHashMap<K, V>) => Effect<void, E, R>;
  <K, V, R, E>(
    self: TxHashMap<K, V>,
    f: (value: V, key: K) => Effect<void, E, R>,
  ): Effect<void, E, R>;
};

get

Added in v2.0.0 Source

Looks up the value for the specified key in the TxHashMap.

Signature

declare const get: {
  <K1, K>(key: K1): <V>(self: TxHashMap<K, V>) => Effect<Option<V>>;
  <K1, K, V>(self: TxHashMap<K, V>, key: K1): Effect<Option<V>>;
};

getHash

Added in v4.0.0 Source

Looks up the value for the specified key using a caller-supplied hash.

Gotchas

The supplied hash must be the hash for the same key, such as a precomputed Hash.hash(key) value. If the hash does not match the key, an existing entry may not be found.

Signature

declare const getHash: {
  <K1, K>(key: K1, hash: number): <V>(self: TxHashMap<K, V>) => Effect<Option<V>>;
  <K1, K, V>(self: TxHashMap<K, V>, key: K1, hash: number): Effect<Option<V>>;
};

keys

Added in v2.0.0 Source

Returns an array of all keys in the TxHashMap.

Signature

declare function keys<K, V>(self: TxHashMap<K, V>): Effect<Array<K>>;

map

Added in v4.0.0 Source

Transforms all values in the TxHashMap using the provided function, preserving keys.

Details

This function returns a new TxHashMap reference with the transformed values. The original TxHashMap is not modified.

Signature

declare const map: {
  <A, V, K>(f: (value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect<TxHashMap<K, A>>;
  <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => A): Effect<TxHashMap<K, A>>;
};

modify

Added in v4.0.0 Source

Updates the value for the specified key if it exists, returning the previous value in Some; returns None and leaves the map unchanged when the key is absent.

Details

This function mutates the original TxHashMap by updating the value at the specified key. It does not return a new TxHashMap reference.

Signature

declare const modify: {
  <K, V>(key: K, f: (value: V) => V): (self: TxHashMap<K, V>) => Effect<Option<V>>;
  <K, V>(self: TxHashMap<K, V>, key: K, f: (value: V) => V): Effect<Option<V>>;
};

modifyAt

Added in v4.0.0 Source

Updates the value for the specified key using an Option-based update function.

Details

This function mutates the original TxHashMap by updating, adding, or removing the key-value pair based on the function result. It does not return a new TxHashMap reference.

Signature

declare const modifyAt: {
  <K, V>(key: K, f: (value: Option<V>) => Option<V>): (self: TxHashMap<K, V>) => Effect<void>;
  <K, V>(self: TxHashMap<K, V>, key: K, f: (value: Option<V>) => Option<V>): Effect<void>;
};

reduce

Added in v2.0.0 Source

Reduces the TxHashMap entries to a single value by applying a reducer function. Iterates over all key-value pairs and accumulates them into a final result.

Signature

declare const reduce: {
  <A, V, K>(
    zero: A,
    f: (accumulator: A, value: V, key: K) => A,
  ): (self: TxHashMap<K, V>) => Effect<A>;
  <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect<A>;
};

remove

Added in v2.0.0 Source

Removes the specified key from the TxHashMap.

Details

This function mutates the original TxHashMap by removing the specified key-value pair. It does not return a new TxHashMap reference.

Signature

declare const remove: {
  <K1, K>(key: K1): <V>(self: TxHashMap<K, V>) => Effect<boolean>;
  <K1, K, V>(self: TxHashMap<K, V>, key: K1): Effect<boolean>;
};

removeMany

Added in v4.0.0 Source

Removes multiple keys from the TxHashMap.

Details

This function mutates the original TxHashMap by removing all specified keys. It does not return a new TxHashMap reference.

Signature

declare const removeMany: {
  <K1, K>(keys: Iterable<K1>): <V>(self: TxHashMap<K, V>) => Effect<void>;
  <K1, K, V>(self: TxHashMap<K, V>, keys: Iterable<K1>): Effect<void>;
};

set

Added in v2.0.0 Source

Sets the value for the specified key in the TxHashMap.

Details

This function mutates the original TxHashMap by updating its internal state. It does not return a new TxHashMap reference.

Signature

declare const set: {
  <K, V>(key: K, value: V): (self: TxHashMap<K, V>) => Effect<void>;
  <K, V>(self: TxHashMap<K, V>, key: K, value: V): Effect<void>;
};

setMany

Added in v4.0.0 Source

Sets multiple key-value pairs in the TxHashMap.

Details

This function mutates the original TxHashMap by setting all provided key-value pairs. It does not return a new TxHashMap reference.

Signature

declare const setMany: {
  <K1, K, V1, V>(entries: Iterable<readonly [K1, V1]>): (self: TxHashMap<K, V>) => Effect<void>;
  <K1, K, V1, V>(self: TxHashMap<K, V>, entries: Iterable<readonly [K1, V1]>): Effect<void>;
};

size

Added in v2.0.0 Source

Returns the number of entries in the TxHashMap.

Signature

declare function size<K, V>(self: TxHashMap<K, V>): Effect<number>;

snapshot

Added in v4.0.0 Source

Returns an immutable snapshot of the current TxHashMap state.

Signature

declare function snapshot<K, V>(self: TxHashMap<K, V>): Effect<HashMap<K, V>>;

union

Added in v4.0.0 Source

Merges another HashMap into this TxHashMap. If both maps contain the same key, the value from the other map will be used.

Details

This function mutates the original TxHashMap by merging the provided HashMap into it. It does not return a new TxHashMap reference.

Signature

declare const union: {
  <K1, K, V1, V>(other: HashMap<K1, V1>): (self: TxHashMap<K, V>) => Effect<void>;
  <K1, K, V1, V>(self: TxHashMap<K, V>, other: HashMap<K1, V1>): Effect<void>;
};

values

Added in v2.0.0 Source

Returns an array of all values in the TxHashMap.

Signature

declare function values<K, V>(self: TxHashMap<K, V>): Effect<Array<V>>;

Constructors

empty

Added in v2.0.0 Source

Creates an empty TxHashMap.

Signature

declare function empty<K, V>(): Effect<TxHashMap<K, V>>;

fromIterable

Added in v2.0.0 Source

Creates a TxHashMap from an iterable of key-value pairs.

Signature

declare function fromIterable<K, V>(entries: Iterable<readonly [K, V]>): Effect<TxHashMap<K, V>>;

make

Added in v2.0.0 Source

Creates a TxHashMap from the provided key-value pairs.

Signature

declare function make<K, V>(...entries: Array<readonly [K, V]>): Effect<TxHashMap<K, V>>;

Getters

toEntries

Added in v4.0.0 Source

Returns an array of all key-value pairs in the TxHashMap. This is an alias for the entries function, providing API consistency with HashMap.

Signature

declare function toEntries<K, V>(self: TxHashMap<K, V>): Effect<Array<readonly [K, V]>>;

toValues

Added in v4.0.0 Source

Returns an array of all values in the TxHashMap. This is an alias for the values function, providing API consistency with HashMap.

Signature

declare function toValues<K, V>(self: TxHashMap<K, V>): Effect<Array<V>>;

Guards

isTxHashMap

Added in v4.0.0 Source

Returns true if the specified value is a TxHashMap, false otherwise.

Signature

declare function isTxHashMap<K, V>(value: unknown): value is TxHashMap<K, V>;

Models

TxHashMap interface

Added in v4.0.0 Source

A TxHashMap is a transactional hash map data structure that provides atomic operations on key-value pairs within Effect transactions. It uses an immutable HashMap internally with TxRef for transactional semantics, ensuring all operations are performed atomically.

Signature

interface TxHashMap<in out K, in out V> extends Inspectable, Pipeable {
  readonly "~effect/transactions/TxHashMap": "~effect/transactions/TxHashMap";
  readonly ref: TxRef<HashMap<K, V>>;
}

Other

TxHashMap

Added in v4.0.0 Source

The TxHashMap namespace contains type-level utilities and helper types for working with TxHashMap instances.

Predicates

every

Added in v4.0.0 Source

Checks whether all entries in the TxHashMap satisfy the given predicate.

Signature

declare const every: {
  <K, V>(predicate: (value: V, key: K) => boolean): (self: TxHashMap<K, V>) => Effect<boolean>;
  <K, V>(self: TxHashMap<K, V>, predicate: (value: V, key: K) => boolean): Effect<boolean>;
};

has

Added in v2.0.0 Source

Checks whether the specified key exists in the TxHashMap.

Signature

declare const has: {
  <K1, K>(key: K1): <V>(self: TxHashMap<K, V>) => Effect<boolean>;
  <K1, K, V>(self: TxHashMap<K, V>, key: K1): Effect<boolean>;
};

hasBy

Added in v4.0.0 Source

Checks whether any entry in the TxHashMap matches the given predicate.

Signature

declare const hasBy: {
  <K, V>(predicate: (value: V, key: K) => boolean): (self: TxHashMap<K, V>) => Effect<boolean>;
  <K, V>(self: TxHashMap<K, V>, predicate: (value: V, key: K) => boolean): Effect<boolean>;
};

hasHash

Added in v4.0.0 Source

Checks whether the specified key has an entry using a caller-supplied hash.

Gotchas

The supplied hash must be the hash for the same key, such as a precomputed Hash.hash(key) value. If the hash does not match the key, an existing entry may not be found.

Signature

declare const hasHash: {
  <K1, K>(key: K1, hash: number): <V>(self: TxHashMap<K, V>) => Effect<boolean>;
  <K1, K, V>(self: TxHashMap<K, V>, key: K1, hash: number): Effect<boolean>;
};

isEmpty

Added in v2.0.0 Source

Checks whether the TxHashMap is empty.

Signature

declare function isEmpty<K, V>(self: TxHashMap<K, V>): Effect<boolean>;

isNonEmpty

Added in v4.0.0 Source

Checks whether the TxHashMap is non-empty.

Signature

declare function isNonEmpty<K, V>(self: TxHashMap<K, V>): Effect<boolean>;

some

Added in v4.0.0 Source

Checks whether at least one entry in the TxHashMap satisfies the given predicate.

Signature

declare const some: {
  <K, V>(predicate: (value: V, key: K) => boolean): (self: TxHashMap<K, V>) => Effect<boolean>;
  <K, V>(self: TxHashMap<K, V>, predicate: (value: V, key: K) => boolean): Effect<boolean>;
};