From b7df8dc3030bc8e34b6541294d0225837ee78297 Mon Sep 17 00:00:00 2001 From: Marek Libra Date: Mon, 7 Jan 2019 17:46:56 +0100 Subject: [PATCH] fix(VncConsole): disconnect VNC session when unmounting (#1123) The VNC session is properly disconnected when unmounting the VncConsole component. This fixes memory-leak and asynchronously fired events to an unmounted component. --- .../src/VncConsole/VncConsole.js | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/patternfly-3/react-console/src/VncConsole/VncConsole.js b/packages/patternfly-3/react-console/src/VncConsole/VncConsole.js index c49059487ed..c36edd438f4 100644 --- a/packages/patternfly-3/react-console/src/VncConsole/VncConsole.js +++ b/packages/patternfly-3/react-console/src/VncConsole/VncConsole.js @@ -18,6 +18,12 @@ const { noop } = helpers; class VncConsole extends React.Component { state = { status: CONNECTING }; + addEventListeners() { + this.rfb.addEventListener('connect', this.onConnected); + this.rfb.addEventListener('disconnect', this.onDisconnected); + this.rfb.addEventListener('securityfailure', this.onSecurityFailure); + } + componentDidMount() { const { host, @@ -45,9 +51,7 @@ class VncConsole extends React.Component { }; this.rfb = new RFB(this.novncElem, url, options); - this.rfb.addEventListener('connect', this.onConnected); - this.rfb.addEventListener('disconnect', this.onDisconnected); - this.rfb.addEventListener('securityfailure', this.onSecurityFailure); + this.addEventListeners(); this.rfb.viewOnly = viewOnly; this.rfb.scaleViewport = false; // if the remote session is smaller than HTML container, the view will be centered this.rfb.resizeSession = resizeSession; @@ -57,6 +61,19 @@ class VncConsole extends React.Component { } } + componentWillUnmount() { + this.removeEventListeners(); + this.disconnect(); + } + + disconnect() { + if (!this.rfb) { + return; + } + this.rfb.disconnect(); + this.rfb = undefined; + } + onConnected = () => { this.setState({ status: CONNECTED }); }; @@ -78,6 +95,12 @@ class VncConsole extends React.Component { this.props.onSecurityFailure(e); }; + removeEventListeners() { + this.rfb.removeEventListener('connect', this.onConnected); + this.rfb.removeEventListener('disconnect', this.onDisconnected); + this.rfb.removeEventListener('securityfailure', this.onSecurityFailure); + } + setNovncElem = e => { this.novncElem = e; };