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

Function buildCache

packages/effect/src/ui/Query.ts:5234–7487  ·  view source on GitHub ↗
(
  context: Context.Context<never>,
  options: Omit<Parameters<typeof makeCache>[0], "idleMs"> & {
    readonly idleMs?: number | undefined
    readonly idleTimeToLive?: Duration.Input | undefined
  }
)

Source from the content-addressed store, hash-verified

5232}
5233
5234const buildCache = (
5235 context: Context.Context<never>,
5236 options: Omit<Parameters<typeof makeCache>[0], "idleMs"> & {
5237 readonly idleMs?: number | undefined
5238 readonly idleTimeToLive?: Duration.Input | undefined
5239 }
5240): {
5241 readonly cache: CacheInternals
5242 readonly dispose: Effect.Effect<void>
5243} => {
5244 const { connectivity, visibility } = options
5245 const clock = Context.get(context, Clock.Clock)
5246 const defaultIdleMs = options.idleMs ?? cacheIdleMs(options.idleTimeToLive)
5247 const settleOnce = options.settleOnce === true
5248 const fetches = options.fetches !== false
5249 const offServer = (def: QueryDef): boolean => settleOnce && !def.server
5250 const persistence = settleOnce
5251 ? undefined
5252 : Context.get(context, queryCore.Persistence)
5253 const reactivity = settleOnce
5254 ? undefined
5255 : Context.getOrUndefined(context, Reactivity.Reactivity)
5256
5257 const queries = new Map<string, QueryDef>()
5258 const duplicates = new WeakSet<QueryDef>()
5259 const index = new Map<string, Map<string, Entry>>()
5260 // Builds made for fakes, and every build this cache listens to.
5261 const fakeBuilds = new WeakMap<Fetcher, Build>()
5262 const attached = new Set<Build>()
5263 const fetchListeners = new Set<(event: FetchEvent) => void>()
5264 const fetchEndListeners = new Set<() => void>()
5265 const disposeListeners = new Set<() => void>()
5266 const timers = new Set<Fiber.Fiber<any, any>>()
5267 // Every fetch fiber until its cleanup has finished, and per entry the interrupted one its next fetch waits for.
5268 const fetchFibers = new Set<Fiber.Fiber<any, any>>()
5269 const draining = new Map<string, Fiber.Fiber<any, any>>()
5270 const sharedFetches = new Set<Shared>()
5271 // The awaited sets still waiting on an entry.
5272 const awaits = new Set<Await>()
5273 // Done when an awaited prefetch's entry first goes quiet.
5274 const prefetchAwaits = new Map<Entry, Deferred.Deferred<void>>()
5275 // Hydrated entries whose query hasn't registered yet.
5276 let raw = new Map<string, Map<string, RawTuple>>()
5277 let hydratedAt = 0
5278 let hydratedStatic = false
5279 let windowOpen = false
5280 // From a hydration until adoption: saved values would change the adopted frame.
5281 let adopting = false
5282 // Entries made while adopting and not shipped: they look up their saved value at adoption.
5283 const unrestored = new Set<Entry>()
5284 // Writes and invalidations, entry creation and run starts share one sequence.
5285 let seq = 0
5286 let issued = 0
5287 let disposed = false
5288
5289 const now = (): number => clock.currentTimeMillisUnsafe()
5290 // A server cache lives for one request: a fetch nothing else parents stays under the request's span.
5291 const requestSpan = settleOnce

Callers 1

makeCacheFunction · 0.85

Calls 15

addListenerFunction · 0.90
cacheIdleMsFunction · 0.85
offServerFunction · 0.85
snapshotIssueFunction · 0.85
allEntriesFunction · 0.85
logKeyOfFunction · 0.85
patchedViewOfFunction · 0.85
dropFetchFunction · 0.85
resumeAllFunction · 0.85
endSharedFunction · 0.85
fromMethod · 0.80
listenFunction · 0.70

Tested by

no test coverage detected