| 32 | * functions post hydration. |
| 33 | */ |
| 34 | export class DehydratedBlockRegistry { |
| 35 | private registry = new Map<string, DehydratedDeferBlock>(); |
| 36 | private cleanupFns = new Map<string, Function[]>(); |
| 37 | private jsActionMap: Map<string, Set<Element>> = inject(JSACTION_BLOCK_ELEMENT_MAP); |
| 38 | private contract: EventContractDetails = inject(JSACTION_EVENT_CONTRACT); |
| 39 | |
| 40 | add(blockId: string, info: DehydratedDeferBlock) { |
| 41 | this.registry.set(blockId, info); |
| 42 | // It's possible that hydration is queued that's waiting for the |
| 43 | // resolution of a lazy loaded route. In this case, we ensure |
| 44 | // the callback function is called to continue the hydration process |
| 45 | // for the queued block set. |
| 46 | if (this.awaitingCallbacks.has(blockId)) { |
| 47 | const awaitingCallbacks = this.awaitingCallbacks.get(blockId)!; |
| 48 | for (const cb of awaitingCallbacks) { |
| 49 | cb(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | get(blockId: string): DehydratedDeferBlock | null { |
| 55 | return this.registry.get(blockId) ?? null; |
| 56 | } |
| 57 | |
| 58 | has(blockId: string): boolean { |
| 59 | return this.registry.has(blockId); |
| 60 | } |
| 61 | |
| 62 | cleanup(hydratedBlocks: string[]) { |
| 63 | removeListenersFromBlocks(hydratedBlocks, this.jsActionMap); |
| 64 | for (let blockId of hydratedBlocks) { |
| 65 | this.registry.delete(blockId); |
| 66 | this.jsActionMap.delete(blockId); |
| 67 | this.invokeTriggerCleanupFns(blockId); |
| 68 | this.hydrating.delete(blockId); |
| 69 | this.awaitingCallbacks.delete(blockId); |
| 70 | } |
| 71 | if (this.size === 0) { |
| 72 | this.contract.instance?.cleanUp(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | get size(): number { |
| 77 | return this.registry.size; |
| 78 | } |
| 79 | |
| 80 | // we have to leave the lowest block Id in the registry |
| 81 | // unless that block has no children |
| 82 | addCleanupFn(blockId: string, fn: Function) { |
| 83 | let cleanupFunctions: Function[] = []; |
| 84 | if (this.cleanupFns.has(blockId)) { |
| 85 | cleanupFunctions = this.cleanupFns.get(blockId)!; |
| 86 | } |
| 87 | cleanupFunctions.push(fn); |
| 88 | this.cleanupFns.set(blockId, cleanupFunctions); |
| 89 | } |
| 90 | |
| 91 | invokeTriggerCleanupFns(blockId: string) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…