Skip to content

Commit

Permalink
fix: unregister message event listener from iframe (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Jun 25, 2024
1 parent f17667b commit 1e6a55f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/web-debugger/src/initializeDevCycleDebugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class IframeManager {
if (this.debugLogs) {
this.log('posting message', clientData.current)
}
if (!clientData.current.loadCount) {
// iframe hasn't loaded yet, don't post messages until it has
return
}
this.mainIframe.contentWindow?.postMessage(
{
...clientData.current,
Expand Down Expand Up @@ -144,20 +148,16 @@ class IframeManager {
oldTrack(...args)
}

window.addEventListener(
'message',
this.iframeMessageReceiver.bind(this),
)
const boundMessageReceiver = this.iframeMessageReceiver.bind(this)

window.addEventListener('message', boundMessageReceiver)

return () => {
this.log('unsubscribing from events')
this.client.unsubscribe('configUpdated', onConfigUpdated)
this.client.unsubscribe('variableEvaluated:*', onVariableEvaluated)
this.client.unsubscribe('variableUpdated:*', onVariableUpdated)
window.removeEventListener(
'message',
this.iframeMessageReceiver.bind(this),
)
window.removeEventListener('message', boundMessageReceiver)
}
}

Expand Down Expand Up @@ -318,7 +318,11 @@ export const initializeDevCycleDebugger = async (

return () => {
cleanup()
document.body.removeChild(iframeManager.mainIframe)
document.body.removeChild(iframeManager.buttonIframe)
if (document.body.contains(iframeManager.mainIframe)) {
document.body.removeChild(iframeManager.mainIframe)
}
if (document.body.contains(iframeManager.buttonIframe)) {
document.body.removeChild(iframeManager.buttonIframe)
}
}
}

0 comments on commit 1e6a55f

Please sign in to comment.