<K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>Creates a TxHashMap from an iterable of key-value pairs.
Example (Creating a map from an iterable)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create from various iterable sources
const configEntries = [
["database.host", "localhost"],
["database.port", "5432"],
["cache.enabled", "true"],
["logging.level", "info"]
] as const
const configMap = yield* TxHashMap.fromIterable(configEntries)
// Verify the configuration was loaded
const size = yield* TxHashMap.size(configMap)
console.log(size) // 4
const dbHost = yield* TxHashMap.get(configMap, "database.host")
console.log(dbHost) // Option.some("localhost")
// Can also create from Map, Set of tuples, etc.
const jsMap = new Map([["key1", "value1"], ["key2", "value2"]])
const txMapFromJs = yield* TxHashMap.fromIterable(jsMap)
})export const const fromIterable: <K, V>(
entries: Iterable<readonly [K, V]>
) => Effect.Effect<TxHashMap<K, V>>
Creates a TxHashMap from an iterable of key-value pairs.
Example (Creating a map from an iterable)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create from various iterable sources
const configEntries = [
["database.host", "localhost"],
["database.port", "5432"],
["cache.enabled", "true"],
["logging.level", "info"]
] as const
const configMap = yield* TxHashMap.fromIterable(configEntries)
// Verify the configuration was loaded
const size = yield* TxHashMap.size(configMap)
console.log(size) // 4
const dbHost = yield* TxHashMap.get(configMap, "database.host")
console.log(dbHost) // Option.some("localhost")
// Can also create from Map, Set of tuples, etc.
const jsMap = new Map([["key1", "value1"], ["key2", "value2"]])
const txMapFromJs = yield* TxHashMap.fromIterable(jsMap)
})
fromIterable = <function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>V>(
entries: Iterable<readonly [K, V]>entries: interface Iterable<T, TReturn = any, TNext = any>Iterable<readonly [function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>V]>
): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>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.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): Effect.Effect<TxHashMap<K, V>>V>> =>
import EffectEffect.gen(function*() {
const const hashMap: HashMap.HashMap<K, V>const hashMap: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
hashMap = import HashMapHashMap.fromIterable(entries: Iterable<readonly [K, V]>entries)
const const ref: TxRef.TxRef<
HashMap.HashMap<K, V>
>
const ref: {
version: number;
pending: Map<unknown, () => void>;
value: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
ref = yield* import TxRefTxRef.const make: <any>(initial: any) => anyCreates a new TxRef with the specified initial value.
When to use
Use to create a TxRef inside an Effect workflow.
Example (Creating transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
// Create a transactional reference with initial value
const counter = yield* TxRef.make(0)
const name = yield* TxRef.make("Alice")
// Use in transactions
yield* Effect.tx(Effect.gen(function*() {
yield* TxRef.set(counter, 42)
yield* TxRef.set(name, "Bob")
}))
console.log(yield* TxRef.get(counter)) // 42
console.log(yield* TxRef.get(name)) // "Bob"
})
make(const hashMap: HashMap.HashMap<K, V>const hashMap: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
hashMap)
return var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<any, {
ref: any;
}>(target: any, source: {
ref: any;
}): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign(var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.create(o: object | null): any (+1 overload)Creates an object that has the specified prototype or that has null prototype.
create(const TxHashMapProto: {
"~effect/transactions/TxHashMap": string
[NodeInspectSymbol](
this: TxHashMap<unknown, unknown>
): unknown
toString(
this: TxHashMap<unknown, unknown>
): string
toJSON(this: TxHashMap<unknown, unknown>): {
_id: string
ref: unknown
}
pipe(this: TxHashMap<unknown, unknown>): unknown
}
TxHashMapProto), { ref: TxRef.TxRef<HashMap.HashMap<K, V>>(property) ref: {
version: number;
pending: Map<unknown, () => void>;
value: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
ref })
})