MCPcopy Create free account
hub / github.com/angular/angular / triggerHydrationForBlockQueue

Function triggerHydrationForBlockQueue

packages/core/src/defer/triggering.ts:433–527  ·  view source on GitHub ↗
(
  injector: Injector,
  hydrationQueue: string[],
  replayQueuedEventsFn?: Function,
)

Source from the content-addressed store, hash-verified

431 * events.
432 */
433export async function triggerHydrationForBlockQueue(
434 injector: Injector,
435 hydrationQueue: string[],
436 replayQueuedEventsFn?: Function,
437): Promise<void> {
438 const dehydratedBlockRegistry = injector.get(DEHYDRATED_BLOCK_REGISTRY);
439 const blocksBeingHydrated = dehydratedBlockRegistry.hydrating;
440
441 // Indicate that we have some pending async work.
442 const pendingTasks = injector.get(PendingTasksInternal);
443 const taskId = pendingTasks.add();
444 const lastBlockName = hydrationQueue[hydrationQueue.length - 1];
445
446 try {
447 // Actually do the triggering and hydration of the queue of blocks
448 for (let blockQueueIdx = 0; blockQueueIdx < hydrationQueue.length; blockQueueIdx++) {
449 const dehydratedBlockId = hydrationQueue[blockQueueIdx];
450 const dehydratedDeferBlock = dehydratedBlockRegistry.get(dehydratedBlockId);
451
452 if (dehydratedDeferBlock != null) {
453 // trigger the block resources and await next render for hydration. This should result
454 // in the next block ɵɵdefer instruction being called and that block being added to the dehydrated registry.
455 await triggerResourceLoadingForHydration(dehydratedDeferBlock);
456 await nextRender(injector);
457
458 // if the content has changed since server rendering, we need to check for the expected block
459 // being in the registry or if errors occurred. In that case, we need to clean up the remaining expected
460 // content that won't be rendered or fetched.
461 if (deferBlockHasErrored(dehydratedDeferBlock)) {
462 // Either the expected block has not yet had its ɵɵdefer instruction called or the block errored out when fetching
463 // resources. In the former case, either we're hydrating too soon or the client and server differ. In both cases,
464 // we need to clean up child content and promises.
465 removeDehydratedViewList(dehydratedDeferBlock);
466 cleanupRemainingHydrationQueue(
467 hydrationQueue.slice(blockQueueIdx),
468 dehydratedBlockRegistry,
469 );
470 break;
471 }
472 // The defer block has not errored and we've finished fetching resources and rendering.
473 // At this point it is safe to resolve the hydration promise.
474 blocksBeingHydrated.get(dehydratedBlockId)?.resolve();
475 } else {
476 // The expected block has not yet had its ɵɵdefer instruction called. This is likely due to content changing between
477 // client and server. We need to clean up the dehydrated DOM in the container since it no longer is valid.
478 cleanupParentContainer(blockQueueIdx, hydrationQueue, dehydratedBlockRegistry);
479 cleanupRemainingHydrationQueue(
480 hydrationQueue.slice(blockQueueIdx),
481 dehydratedBlockRegistry,
482 );
483 break;
484 }
485 }
486
487 // Await hydration completion for the last block.
488 await blocksBeingHydrated.get(lastBlockName)?.promise;
489 } finally {
490 // All async work is done, remove the taskId from the registry.

Callers 1

Calls 13

removeDehydratedViewListFunction · 0.90
nextRenderFunction · 0.85
deferBlockHasErroredFunction · 0.85
cleanupParentContainerFunction · 0.85
getMethod · 0.65
addMethod · 0.65
sliceMethod · 0.65
resolveMethod · 0.65
removeMethod · 0.65

Tested by

no test coverage detected