Skip to content

Commit

Permalink
fix(VncConsole): prevent running initialization code more than once (p…
Browse files Browse the repository at this point in the history
…atternfly#8373)

The functions that were on the dependency list of the useEffect hook
got redefined each time the component got updated, resulting
in the component's initialization code, from within the useEffect hook,
to be running more than once.
That was unwanted behavior and the useCallback wrappers should prevent
it.
  • Loading branch information
KKoukiou committed Nov 28, 2022
1 parent a6cd986 commit 2d1b079
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions packages/react-console/src/components/VncConsole/VncConsole.tsx
Expand Up @@ -97,23 +97,43 @@ export const VncConsole: React.FunctionComponent<VncConsoleProps> = ({

const [status, setStatus] = React.useState(CONNECTING);

const addEventListeners = () => {
const onConnected = () => {
setStatus(CONNECTED);
};

const _onDisconnected = React.useCallback((e: any) => {
setStatus(DISCONNECTED);
onDisconnected(e);
}, []);

const _onSecurityFailure = React.useCallback((e: any) => {
setStatus(DISCONNECTED);
onSecurityFailure(e);
}, []);

const onCtrlAltDel = () => {
if (rfb.current) {
rfb?.current?.sendCtrlAltDel();
}
};

const addEventListeners = React.useCallback(() => {
if (rfb.current) {
rfb.current?.addEventListener('connect', onConnected);
rfb.current?.addEventListener('disconnect', _onDisconnected);
rfb.current?.addEventListener('securityfailure', _onSecurityFailure);
}
};
}, [rfb, _onDisconnected, _onSecurityFailure]);

const removeEventListeners = () => {
const removeEventListeners = React.useCallback(() => {
if (rfb.current) {
rfb.current.removeEventListener('connect', onConnected);
rfb.current.removeEventListener('disconnect', _onDisconnected);
rfb.current.removeEventListener('securityfailure', _onSecurityFailure);
}
};
}, [rfb, _onDisconnected, _onSecurityFailure]);

const connect = () => {
const connect = React.useCallback(() => {
const protocol = encrypt ? 'wss' : 'ws';
const url = `${protocol}://${host}:${port}/${path}`;

Expand All @@ -127,7 +147,21 @@ export const VncConsole: React.FunctionComponent<VncConsoleProps> = ({
rfb.current.viewOnly = viewOnly;
rfb.current.scaleViewport = scaleViewport;
rfb.current.resizeSession = resizeSession;
};
}, [
addEventListeners,
host,
path,
port,
resizeSession,
scaleViewport,
viewOnly,
encrypt,
rfb,
repeaterID,
shared,
credentials,
novncElem
]);

React.useEffect(() => {
initLogging(vncLogging);
Expand All @@ -152,26 +186,6 @@ export const VncConsole: React.FunctionComponent<VncConsoleProps> = ({
rfb.current.disconnect();
};

const onConnected = () => {
setStatus(CONNECTED);
};

const _onDisconnected = (e: any) => {
setStatus(DISCONNECTED);
onDisconnected(e);
};

const _onSecurityFailure = (e: any) => {
setStatus(DISCONNECTED);
onSecurityFailure(e);
};

const onCtrlAltDel = () => {
if (rfb.current) {
rfb?.current?.sendCtrlAltDel();
}
};

let rightContent;
let emptyState;
switch (status) {
Expand Down

0 comments on commit 2d1b079

Please sign in to comment.