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.
Combinators
Signature
declare function clear<K, V>(self: TxHashMap<K, V>): Effect<void>;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>>;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]>>;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>>;
};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>>;
};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]>>;
};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>>;
};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>;
};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>>;
};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>>;
};Returns an array of all keys in the TxHashMap.
Signature
declare function keys<K, V>(self: TxHashMap<K, V>): Effect<Array<K>>;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>>;
};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>>;
};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>;
};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>;
};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
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>;
};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>;
};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>;
};Returns the number of entries in the TxHashMap.
Signature
declare function size<K, V>(self: TxHashMap<K, V>): Effect<number>;Returns an immutable snapshot of the current TxHashMap state.
Signature
declare function snapshot<K, V>(self: TxHashMap<K, V>): Effect<HashMap<K, V>>;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>;
};Returns an array of all values in the TxHashMap.
Signature
declare function values<K, V>(self: TxHashMap<K, V>): Effect<Array<V>>;Constructors
Creates an empty TxHashMap.
Signature
declare function empty<K, V>(): Effect<TxHashMap<K, V>>;fromIterable
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>>;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
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]>>;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
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
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
Predicates
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>;
};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>;
};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>;
};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>;
};Checks whether the TxHashMap is empty.
Signature
declare function isEmpty<K, V>(self: TxHashMap<K, V>): Effect<boolean>;isNonEmpty
Checks whether the TxHashMap is non-empty.
Signature
declare function isNonEmpty<K, V>(self: TxHashMap<K, V>): Effect<boolean>;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>;
};
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.