MCPcopy Index your code
hub / github.com/Effect-TS/effect / getOrCreatePool

Function getOrCreatePool

packages/effect/src/internal/keyedPool.ts:110–175  ·  view source on GitHub ↗
(key: K)

Source from the content-addressed store, hash-verified

108 ]),
109 core.map(([context, fiberId, map, scope]) => {
110 const getOrCreatePool = (key: K): Effect.Effect<Pool.Pool<A, E>> =>
111 core.suspend(() => {
112 let value: MapValue<A, E> | undefined = Option.getOrUndefined(HashMap.get(MutableRef.get(map), key))
113 if (value === undefined) {
114 return core.uninterruptibleMask((restore) => {
115 const deferred = core.deferredUnsafeMake<Pool.Pool<A, E>>(fiberId)
116 value = new Pending(deferred)
117 let previous: MapValue<A, E> | undefined = undefined
118 if (HashMap.has(MutableRef.get(map), key)) {
119 previous = Option.getOrUndefined(HashMap.get(MutableRef.get(map), key))
120 } else {
121 MutableRef.update(map, HashMap.set(key, value as MapValue<A, E>))
122 }
123 if (previous === undefined) {
124 return pipe(
125 restore(
126 fiberRuntime.scopeExtend(
127 pool.makeWithTTL({
128 acquire: core.provideContext(get(key), context),
129 min: min(key),
130 max: max(key),
131 timeToLive: Option.getOrElse(timeToLive(key), () => Duration.infinity)
132 }),
133 scope
134 )
135 ),
136 core.matchCauseEffect({
137 onFailure: (cause) => {
138 const current = Option.getOrUndefined(HashMap.get(MutableRef.get(map), key))
139 if (Equal.equals(current, value)) {
140 MutableRef.update(map, HashMap.remove(key))
141 }
142 return core.zipRight(
143 core.deferredFailCause(deferred, cause),
144 core.failCause(cause)
145 )
146 },
147 onSuccess: (pool) => {
148 MutableRef.update(map, HashMap.set(key, new Complete(pool) as MapValue<A, E>))
149 return core.as(
150 core.deferredSucceed(deferred, pool),
151 pool
152 )
153 }
154 })
155 )
156 }
157 switch (previous._tag) {
158 case "Complete": {
159 return core.succeed(previous.pool)
160 }
161 case "Pending": {
162 return restore(core.deferredAwait(previous.deferred))
163 }
164 }
165 })
166 }
167 switch (value._tag) {

Callers

nothing calls this directly

Calls 10

restoreFunction · 0.85
failCauseMethod · 0.80
pipeFunction · 0.70
getFunction · 0.70
getMethod · 0.65
updateMethod · 0.65
setMethod · 0.65
removeMethod · 0.65
minFunction · 0.50
maxFunction · 0.50

Tested by

no test coverage detected