MCPcopy
hub / github.com/vitejs/vite / setupOnCrawlEnd

Function setupOnCrawlEnd

packages/vite/src/node/server/environment.ts:377–436  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

375}
376
377function setupOnCrawlEnd(): CrawlEndFinder {
378 const registeredIds = new Set<string>()
379 const seenIds = new Set<string>()
380 const onCrawlEndPromiseWithResolvers = promiseWithResolvers<void>()
381
382 let timeoutHandle: NodeJS.Timeout | undefined
383
384 let cancelled = false
385 function cancel() {
386 cancelled = true
387 }
388
389 function registerRequestProcessing(
390 id: string,
391 done: () => Promise<any>,
392 ): void {
393 if (!seenIds.has(id)) {
394 seenIds.add(id)
395 registeredIds.add(id)
396 done()
397 .catch(() => {})
398 .finally(() => markIdAsDone(id))
399 }
400 }
401
402 function waitForRequestsIdle(ignoredId?: string): Promise<void> {
403 if (ignoredId) {
404 seenIds.add(ignoredId)
405 markIdAsDone(ignoredId)
406 } else {
407 checkIfCrawlEndAfterTimeout()
408 }
409 return onCrawlEndPromiseWithResolvers.promise
410 }
411
412 function markIdAsDone(id: string): void {
413 registeredIds.delete(id)
414 checkIfCrawlEndAfterTimeout()
415 }
416
417 function checkIfCrawlEndAfterTimeout() {
418 if (cancelled || registeredIds.size > 0) return
419
420 if (timeoutHandle) clearTimeout(timeoutHandle)
421 timeoutHandle = setTimeout(
422 callOnCrawlEndWhenIdle,
423 callCrawlEndIfIdleAfterMs,
424 )
425 }
426 function callOnCrawlEndWhenIdle() {
427 if (cancelled || registeredIds.size > 0) return
428 onCrawlEndPromiseWithResolvers.resolve()
429 }
430
431 return {
432 registerRequestProcessing,
433 waitForRequestsIdle,
434 cancel,

Callers 1

constructorMethod · 0.85

Calls 1

promiseWithResolversFunction · 0.90

Tested by

no test coverage detected