import { i18n } from '@kbn/i18n' ; import apm from 'elastic-apm-node' ; import type { Logger } from 'src/core/server' ; import type { HeadlessChromiumDriver } from '../browsers' ; import { Layout } from '../layouts' ; import { CONTEXT_WAITFORRENDER } from './constants' ; export const waitForRenderComplete = async ( browser , logger , loadDelay , layout ) => { const span = apm . startSpan ( 'wait_for_render' , 'wait' ) ; logger . debug ( i18n . translate ( 'xpack.screenshotting.screencapture.waitingForRenderComplete' , { defaultMessage : 'waiting for rendering to complete' , } ) ) ; return await browser . evaluate ( { fn : ( selector , visLoadDelay ) => { const visualizations = document . querySelectorAll ( selector ) ; const visCount = visualizations . length ; const renderedTasks = [ ] ; function waitForRender ( visualization ) { return new Promise < void > ( ( resolve ) => { visualization . addEventListener ( 'renderComplete' , ( ) => resolve ( ) ) ; } ) ; } function waitForRenderDelay ( ) { return new Promise ( ( resolve ) => { setTimeout ( resolve , visLoadDelay ) ; } ) ; } for ( let i = 0 ; i < visCount ; i ++ ) { const visualization = visualizations [ i ] ; const isRendered = visualization . getAttribute ( 'data-render-complete' ) ; if ( isRendered === 'disabled' ) { renderedTasks . push ( waitForRenderDelay ( ) ) ; } else if ( isRendered === 'false' ) { renderedTasks . push ( waitForRender ( visualization ) ) ; } } const hackyWaitForVisualizations = ( ) => new Promise ( ( r ) => setTimeout ( r , 250 ) ) ; return Promise . all ( renderedTasks ) . then ( hackyWaitForVisualizations ) ; } , args : [ layout . selectors . renderComplete , loadDelay ] , } , { context : CONTEXT_WAITFORRENDER } , logger ) . then ( ( ) => { logger . debug ( i18n . translate ( 'xpack.screenshotting.screencapture.renderIsComplete' , { defaultMessage : 'rendering is complete' , } ) ) ; span ?. end ( ) ; } ) ; } ;