Skip to content

Commit b7df8dc

Browse files
mareklibrajeff-phillips-18
authored andcommitted
fix(VncConsole): disconnect VNC session when unmounting (patternfly#1123)
The VNC session is properly disconnected when unmounting the VncConsole component. This fixes memory-leak and asynchronously fired events to an unmounted component.
1 parent 758b23f commit b7df8dc

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

packages/patternfly-3/react-console/src/VncConsole/VncConsole.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const { noop } = helpers;
1818
class VncConsole extends React.Component {
1919
state = { status: CONNECTING };
2020

21+
addEventListeners() {
22+
this.rfb.addEventListener('connect', this.onConnected);
23+
this.rfb.addEventListener('disconnect', this.onDisconnected);
24+
this.rfb.addEventListener('securityfailure', this.onSecurityFailure);
25+
}
26+
2127
componentDidMount() {
2228
const {
2329
host,
@@ -45,9 +51,7 @@ class VncConsole extends React.Component {
4551
};
4652

4753
this.rfb = new RFB(this.novncElem, url, options);
48-
this.rfb.addEventListener('connect', this.onConnected);
49-
this.rfb.addEventListener('disconnect', this.onDisconnected);
50-
this.rfb.addEventListener('securityfailure', this.onSecurityFailure);
54+
this.addEventListeners();
5155
this.rfb.viewOnly = viewOnly;
5256
this.rfb.scaleViewport = false; // if the remote session is smaller than HTML container, the view will be centered
5357
this.rfb.resizeSession = resizeSession;
@@ -57,6 +61,19 @@ class VncConsole extends React.Component {
5761
}
5862
}
5963

64+
componentWillUnmount() {
65+
this.removeEventListeners();
66+
this.disconnect();
67+
}
68+
69+
disconnect() {
70+
if (!this.rfb) {
71+
return;
72+
}
73+
this.rfb.disconnect();
74+
this.rfb = undefined;
75+
}
76+
6077
onConnected = () => {
6178
this.setState({ status: CONNECTED });
6279
};
@@ -78,6 +95,12 @@ class VncConsole extends React.Component {
7895
this.props.onSecurityFailure(e);
7996
};
8097

98+
removeEventListeners() {
99+
this.rfb.removeEventListener('connect', this.onConnected);
100+
this.rfb.removeEventListener('disconnect', this.onDisconnected);
101+
this.rfb.removeEventListener('securityfailure', this.onSecurityFailure);
102+
}
103+
81104
setNovncElem = e => {
82105
this.novncElem = e;
83106
};

0 commit comments

Comments
 (0)