fix: include uuid in iframe communicator logger

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-09-28 15:40:16 +02:00
parent 865e127d16
commit d5455f7c33
5 changed files with 27 additions and 12 deletions

View file

@ -43,7 +43,8 @@ export const useForceRenderPageUrlOnIframeLoadCallback = (
redirectionInProgress.current = false redirectionInProgress.current = false
log.debug('Redirect complete') log.debug('Redirect complete')
} else { } else {
log.warn(`Navigated away from unknown URL. Forcing back to ${forcedUrl}`) const oldUrl = frame.src === '' ? '(none)' : frame.src
log.warn(`Navigated away from unknown URL. Was ${oldUrl}. Forcing back to ${forcedUrl}`)
onNavigateAway?.() onNavigateAway?.()
redirectionInProgress.current = true redirectionInProgress.current = true
frame.src = forcedUrl frame.src = forcedUrl

View file

@ -37,8 +37,6 @@ export interface RendererIframeProps extends Omit<CommonMarkdownRendererProps &
showWaitSpinner?: boolean showWaitSpinner?: boolean
} }
const log = new Logger('RendererIframe')
/** /**
* Renders the iframe for the HTML-rendering of the markdown content. * Renders the iframe for the HTML-rendering of the markdown content.
* The iframe is enhanced by the {@link useEditorToRendererCommunicator iframe communicator} which is used for * The iframe is enhanced by the {@link useEditorToRendererCommunicator iframe communicator} which is used for
@ -69,18 +67,34 @@ export const RendererIframe: React.FC<RendererIframeProps> = ({
const [rendererReady, setRendererReady] = useState<boolean>(false) const [rendererReady, setRendererReady] = useState<boolean>(false)
const frameReference = useRef<HTMLIFrameElement>(null) const frameReference = useRef<HTMLIFrameElement>(null)
const iframeCommunicator = useEditorToRendererCommunicator() const iframeCommunicator = useEditorToRendererCommunicator()
const log = useMemo(() => new Logger(`RendererIframe[${iframeCommunicator.getUuid()}]`), [iframeCommunicator])
const resetRendererReady = useCallback(() => { const resetRendererReady = useCallback(() => {
log.debug('Reset render status') log.debug('Reset render status')
setRendererReady(false) setRendererReady(false)
}, []) }, [log])
const onIframeLoad = useForceRenderPageUrlOnIframeLoadCallback(frameReference, resetRendererReady) const onIframeLoad = useForceRenderPageUrlOnIframeLoadCallback(frameReference, resetRendererReady)
const [frameHeight, setFrameHeight] = useState<number>(0) const [frameHeight, setFrameHeight] = useState<number>(0)
useEffect(() => {
if (rendererReady) {
log.info('Renderer Ready!')
} else {
log.info('Renderer not ready')
}
}, [log, rendererReady])
useEffect(() => { useEffect(() => {
onRendererStatusChange?.(rendererReady) onRendererStatusChange?.(rendererReady)
}, [onRendererStatusChange, rendererReady]) }, [onRendererStatusChange, rendererReady])
useEffect(() => () => setRendererReady(false), [iframeCommunicator]) useEffect(
() => () => {
log.debug('Component ended')
setRendererReady(false)
},
[iframeCommunicator, log]
)
useEffect(() => { useEffect(() => {
if (!rendererReady) { if (!rendererReady) {
@ -138,7 +152,7 @@ export const RendererIframe: React.FC<RendererIframeProps> = ({
} }
}) })
setRendererReady(true) setRendererReady(true)
}, [iframeCommunicator, rendererType]) }, [iframeCommunicator, log, rendererType])
) )
useEffectOnRenderTypeChange(rendererType, onIframeLoad) useEffectOnRenderTypeChange(rendererType, onIframeLoad)

View file

@ -19,7 +19,7 @@ export class EditorToRendererCommunicator extends WindowPostMessageCommunicator<
EditorToRendererMessageType, EditorToRendererMessageType,
CommunicationMessages CommunicationMessages
> { > {
protected createLogger(): Logger { protected createLogger(uuid: string): Logger {
return new Logger('EditorToRendererCommunicator') return new Logger(`EditorToRendererCommunicator[${uuid}]`)
} }
} }

View file

@ -19,7 +19,7 @@ export class RendererToEditorCommunicator extends WindowPostMessageCommunicator<
RendererToEditorMessageType, RendererToEditorMessageType,
CommunicationMessages CommunicationMessages
> { > {
protected createLogger(): Logger { protected createLogger(uuid: string): Logger {
return new Logger('RendererToEditorCommunicator') return new Logger(`RendererToEditorCommunicator[${uuid}]`)
} }
} }

View file

@ -45,14 +45,14 @@ export abstract class WindowPostMessageCommunicator<
) { ) {
this.boundListener = this.handleEvent.bind(this) this.boundListener = this.handleEvent.bind(this)
this.communicationEnabled = false this.communicationEnabled = false
this.log = this.createLogger() this.log = this.createLogger(uuid)
} }
public getUuid(): string { public getUuid(): string {
return this.uuid return this.uuid
} }
protected abstract createLogger(): Logger protected abstract createLogger(uuid: string): Logger
/** /**
* Registers the event listener on the current global {@link window}. * Registers the event listener on the current global {@link window}.