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

Method getOrElseMemoize

packages/effect/src/internal/layer.ts:213–316  ·  view source on GitHub ↗

* Checks the memo map to see if a layer exists. If it is, immediately * returns it. Otherwise, obtains the layer, stores it in the memo map, * and adds a finalizer to the `Scope`.

(
    layer: Layer.Layer<ROut, E, RIn>,
    scope: Scope.Scope
  )

Source from the content-addressed store, hash-verified

211 * and adds a finalizer to the `Scope`.
212 */
213 getOrElseMemoize<RIn, E, ROut>(
214 layer: Layer.Layer<ROut, E, RIn>,
215 scope: Scope.Scope
216 ): Effect.Effect<Context.Context<ROut>, E, RIn> {
217 return pipe(
218 synchronized.modifyEffect(this.ref, (map) => {
219 const inMap = map.get(layer)
220 if (inMap !== undefined) {
221 const [acquire, release] = inMap
222 const cached: Effect.Effect<Context.Context<ROut>, E> = pipe(
223 acquire as Effect.Effect<readonly [FiberRefsPatch.FiberRefsPatch, Context.Context<ROut>], E>,
224 core.flatMap(([patch, b]) => pipe(effect.patchFiberRefs(patch), core.as(b))),
225 core.onExit(core.exitMatch({
226 onFailure: () => core.void,
227 onSuccess: () => core.scopeAddFinalizerExit(scope, release)
228 }))
229 )
230 return core.succeed([cached, map] as const)
231 }
232 return pipe(
233 ref.make(0),
234 core.flatMap((observers) =>
235 pipe(
236 core.deferredMake<readonly [FiberRefsPatch.FiberRefsPatch, Context.Context<ROut>], E>(),
237 core.flatMap((deferred) =>
238 pipe(
239 ref.make<Scope.Scope.Finalizer>(() => core.void),
240 core.map((finalizerRef) => {
241 const resource = core.uninterruptibleMask((restore) =>
242 pipe(
243 fiberRuntime.scopeMake(),
244 core.flatMap((innerScope) =>
245 pipe(
246 restore(core.flatMap(
247 makeBuilder(layer, innerScope, true),
248 (f) => effect.diffFiberRefs(f(this))
249 )),
250 core.exit,
251 core.flatMap((exit) => {
252 switch (exit._tag) {
253 case EffectOpCodes.OP_FAILURE: {
254 return pipe(
255 core.deferredFailCause(deferred, exit.effect_instruction_i0),
256 core.zipRight(core.scopeClose(innerScope, exit)),
257 core.zipRight(core.failCause(exit.effect_instruction_i0))
258 )
259 }
260 case EffectOpCodes.OP_SUCCESS: {
261 return pipe(
262 ref.set(finalizerRef, (exit) =>
263 pipe(
264 core.scopeClose(innerScope, exit),
265 core.whenEffect(
266 ref.modify(observers, (n) => [n === 1, n - 1] as const)
267 ),
268 core.asVoid
269 )),
270 core.zipRight(ref.update(observers, (n) => n + 1)),

Callers 1

makeBuilderFunction · 0.80

Calls 15

restoreFunction · 0.85
makeBuilderFunction · 0.85
isFreshFunction · 0.85
failCauseMethod · 0.80
syncMethod · 0.80
pipeFunction · 0.70
finalizerFunction · 0.70
modifyEffectMethod · 0.65
getMethod · 0.65
onExitMethod · 0.65
makeMethod · 0.65
mapMethod · 0.65

Tested by

no test coverage detected