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

Function checkCapacity

packages/effect/src/ScopedCache.ts:390–407  ·  view source on GitHub ↗
(
  parent: Fiber.Fiber<unknown, unknown>,
  map: MutableHashMap.MutableHashMap<K, Entry<A, E>>,
  capacity: number
)

Source from the content-addressed store, hash-verified

388 })
389
390const checkCapacity = <K, A, E>(
391 parent: Fiber.Fiber<unknown, unknown>,
392 map: MutableHashMap.MutableHashMap<K, Entry<A, E>>,
393 capacity: number
394): Effect.Effect<void> => {
395 if (!Number.isFinite(capacity)) return effect.void
396 let diff = MutableHashMap.size(map) - capacity
397 if (diff <= 0) return effect.void
398 // MutableHashMap has insertion order, so we can remove the oldest entries
399 const fibers = Arr.empty<Fiber.Fiber<unknown, unknown>>()
400 for (const [key, entry] of map) {
401 MutableHashMap.remove(map, key)
402 fibers.push(effect.forkUnsafe(parent as any, closeEvicted(entry), true))
403 diff--
404 if (diff === 0) break
405 }
406 return effect.fiberAwaitAll(fibers)
407}
408
409/**
410 * Reads an existing unexpired cache entry without running the lookup function.

Callers 1

ScopedCache.tsFile · 0.70

Calls 4

closeEvictedFunction · 0.85
sizeMethod · 0.65
removeMethod · 0.65
pushMethod · 0.65

Tested by

no test coverage detected