( injector: Injector, blockName: string, replayQueuedEventsFn?: Function, )
| 372 | * events. |
| 373 | */ |
| 374 | export async function triggerHydrationFromBlockName( |
| 375 | injector: Injector, |
| 376 | blockName: string, |
| 377 | replayQueuedEventsFn?: Function, |
| 378 | ) { |
| 379 | const dehydratedBlockRegistry = injector.get(DEHYDRATED_BLOCK_REGISTRY); |
| 380 | const blocksBeingHydrated = dehydratedBlockRegistry.hydrating; |
| 381 | |
| 382 | // Make sure we don't hydrate/trigger the same thing multiple times |
| 383 | if (blocksBeingHydrated.has(blockName)) { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | // Trigger resource loading and hydration for the blocks in the queue in the order of highest block |
| 388 | // to lowest block. Once a block has finished resource loading, after next render fires after hydration |
| 389 | // finishes. The new block will have its defer instruction called and will be in the registry. |
| 390 | // Due to timing related to potential nested control flow, this has to be scheduled after the next render. |
| 391 | const {parentBlockPromise, hydrationQueue} = getParentBlockHydrationQueue(blockName, injector); |
| 392 | if (hydrationQueue.length === 0) return; |
| 393 | |
| 394 | // It's possible that the hydrationQueue topmost item is actually in the process of hydrating and has |
| 395 | // a promise already. In that case, we don't want to destroy that promise and queue it again. |
| 396 | if (parentBlockPromise !== null) { |
| 397 | hydrationQueue.shift(); |
| 398 | } |
| 399 | |
| 400 | // The hydrating map in the registry prevents re-triggering hydration for a block that's already in |
| 401 | // the hydration queue. Here we generate promises for each of the blocks about to be hydrated |
| 402 | populateHydratingStateForQueue(dehydratedBlockRegistry, hydrationQueue); |
| 403 | |
| 404 | // We await this after populating the hydration state so we can prevent re-triggering hydration for |
| 405 | // the same blocks while this promise is being awaited. |
| 406 | if (parentBlockPromise !== null) { |
| 407 | await parentBlockPromise; |
| 408 | } |
| 409 | |
| 410 | const topmostParentBlock = hydrationQueue[0]; |
| 411 | if (dehydratedBlockRegistry.has(topmostParentBlock)) { |
| 412 | // the topmost parent block is already in the registry and we can proceed |
| 413 | // with hydration. |
| 414 | await triggerHydrationForBlockQueue(injector, hydrationQueue, replayQueuedEventsFn); |
| 415 | } else { |
| 416 | // the topmost parent block is not yet in the registry, which may mean |
| 417 | // a lazy loaded route, a control flow branch was taken, a route has |
| 418 | // been navigated, etc. So we need to queue up the hydration process |
| 419 | // so that it can be finished after the top block has had its defer |
| 420 | // instruction executed. |
| 421 | dehydratedBlockRegistry.awaitParentBlock( |
| 422 | topmostParentBlock, |
| 423 | async () => |
| 424 | await triggerHydrationForBlockQueue(injector, hydrationQueue, replayQueuedEventsFn), |
| 425 | ); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * The core mechanism for incremental hydration. This triggers |
no test coverage detected
searching dependent graphs…