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

Function checkCapacity

packages/effect/src/ScopedCache.ts:304–321  ·  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

302}
303
304const checkCapacity = <K, A, E>(
305 parent: Fiber.Fiber<unknown, unknown>,
306 map: MutableHashMap.MutableHashMap<K, Entry<A, E>>,
307 capacity: number
308): Effect.Effect<void> => {
309 if (!Number.isFinite(capacity)) return effect.void
310 let diff = MutableHashMap.size(map) - capacity
311 if (diff <= 0) return effect.void
312 // MutableHashMap has insertion order, so we can remove the oldest entries
313 const fibers = Arr.empty<Fiber.Fiber<unknown, unknown>>()
314 for (const [key, entry] of map) {
315 MutableHashMap.remove(map, key)
316 fibers.push(effect.forkUnsafe(parent as any, Scope.close(entry.scope, effect.exitVoid), true))
317 diff--
318 if (diff === 0) break
319 }
320 return effect.fiberAwaitAll(fibers)
321}
322
323/**
324 * Reads an existing unexpired cache entry without running the lookup function.

Callers 1

ScopedCache.tsFile · 0.70

Calls 4

pushMethod · 0.80
sizeMethod · 0.65
closeMethod · 0.65
removeMethod · 0.45

Tested by

no test coverage detected