MCPcopy Create free account
hub / github.com/Effect-TS/effect / ResourceMap

Class ResourceMap

packages/effect/src/unstable/cluster/internal/resourceMap.ts:10–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9/** @internal */
10export class ResourceMap<K, A, E> {
11 readonly lookup: (key: K, scope: Scope.Scope) => Effect.Effect<A, E>
12 readonly entries: BackingMap<K, A, E>
13 readonly isClosed: MutableRef.MutableRef<boolean>
14 constructor(
15 lookup: (key: K, scope: Scope.Scope) => Effect.Effect<A, E>,
16 entries: BackingMap<K, A, E>,
17 isClosed: MutableRef.MutableRef<boolean>
18 ) {
19 this.lookup = lookup
20 this.entries = entries
21 this.isClosed = isClosed
22 }
23
24 static make = Effect.fnUntraced(function*<K, A, E, R>(lookup: (key: K) => Effect.Effect<A, E, R>, options?: {
25 readonly referential?: boolean | undefined
26 }) {
27 const scope = yield* Effect.scope
28 const services = yield* Effect.context<R>()
29 const isClosed = MutableRef.make(false)
30
31 const entries: BackingMap<K, A, E> = options?.referential ?
32 {
33 _tag: "Referential",
34 map: new Map()
35 } :
36 {
37 _tag: "Equal",
38 map: MutableHashMap.empty()
39 }
40
41 yield* Scope.addFinalizerExit(
42 scope,
43 (exit) => {
44 MutableRef.set(isClosed, true)
45 return Effect.forEach(entries.map, ([key, { scope }]) => {
46 backingDelete(entries, key)
47 return Effect.exit(Scope.close(scope, exit))
48 }, { concurrency: "unbounded", discard: true })
49 }
50 )
51
52 return new ResourceMap(
53 (key, scope) => Effect.provide(lookup(key), Context.add(services, Scope.Scope, scope)),
54 entries,
55 isClosed
56 )
57 })
58
59 hasUnsafe(key: K): boolean {
60 return backingGet(this.entries, key) !== undefined
61 }
62
63 keysUnsafe(): Array<K> {
64 return Array.from(this.entries.map, ([key]) => key)
65 }
66
67 get(key: K): Effect.Effect<A, E> {

Callers

nothing calls this directly

Calls 9

backingDeleteFunction · 0.85
contextMethod · 0.80
makeMethod · 0.65
setMethod · 0.65
forEachMethod · 0.65
closeMethod · 0.65
addMethod · 0.65
lookupFunction · 0.50
provideMethod · 0.45

Tested by

no test coverage detected