Skip to content

Commit bcdeec6

Browse files
fix(linting): resolve warnings being flagged by the linter (#26)
1 parent d15ab2f commit bcdeec6

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@typescript-eslint/prefer-namespace-keyword": "error",
5151
"@typescript-eslint/unified-signatures": "error",
5252
"@typescript-eslint/no-var-requires": "off",
53+
"@typescript-eslint/no-explicit-any": "off",
5354
"arrow-body-style": "error",
5455
"camelcase": [
5556
"error",

packages/module/src/components/SerialConsole/XTerm.tsx

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
2929
onData,
3030
innerRef
3131
}) => {
32-
let terminal: Terminal;
32+
const terminalRef = React.useRef<Terminal>();
3333
const ref = React.useRef<HTMLDivElement>();
3434

3535
useImperativeHandle(innerRef, () => ({
3636
focusTerminal() {
37-
if (terminal) {
38-
terminal.focus();
37+
if (terminalRef.current) {
38+
terminalRef.current.focus();
3939
}
4040
},
4141
/**
@@ -44,8 +44,8 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
4444
* @param {string} data String content to be writen into the terminal
4545
*/
4646
onDataReceived: (data: string) => {
47-
if (terminal) {
48-
terminal.write(data);
47+
if (terminalRef.current) {
48+
terminalRef.current.write(data);
4949
}
5050
},
5151
/**
@@ -54,16 +54,35 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
5454
* @param {string} reason String error to be written into the terminal
5555
*/
5656
onConnectionClosed: (reason: string) => {
57-
if (terminal) {
58-
terminal.write(`\x1b[31m${reason || 'disconnected'}\x1b[m\r\n`);
59-
terminal.refresh(terminal.rows, terminal.rows); // start to end row
57+
if (terminalRef.current) {
58+
terminalRef.current.write(`\x1b[31m${reason || 'disconnected'}\x1b[m\r\n`);
59+
terminalRef.current.refresh(terminalRef.current.rows, terminalRef.current.rows); // start to end row
6060
}
6161
}
6262
}));
6363

64+
const onBeforeUnload = React.useCallback((event: any) => {
65+
// Firefox requires this when the page is in an iframe
66+
event.preventDefault();
67+
68+
// see "an almost cross-browser solution" at
69+
// https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
70+
event.returnValue = '';
71+
return '';
72+
}, []);
73+
74+
75+
const onFocusIn = () => {
76+
window.addEventListener('beforeunload', onBeforeUnload);
77+
};
78+
79+
const onFocusOut = React.useCallback(() => {
80+
window.removeEventListener('beforeunload', onBeforeUnload);
81+
}, [onBeforeUnload]);
82+
6483
React.useEffect(() => {
6584
const fitAddon = new FitAddon();
66-
terminal = new Terminal({
85+
terminalRef.current = new Terminal({
6786
cols,
6887
rows,
6988
cursorBlink: true,
@@ -75,21 +94,21 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
7594
const onWindowResize = () => {
7695
const geometry = fitAddon.proposeDimensions();
7796
if (geometry) {
78-
terminal.resize(geometry.rows, geometry.cols);
97+
terminalRef.current.resize(geometry.rows, geometry.cols);
7998
}
8099
};
81100

82101
if (onData) {
83-
terminal.onData(onData);
102+
terminalRef.current.onData(onData);
84103
}
85104

86105
if (onTitleChanged) {
87-
terminal.onTitleChange(onTitleChanged);
106+
terminalRef.current.onTitleChange(onTitleChanged);
88107
}
89108

90-
terminal.loadAddon(fitAddon);
109+
terminalRef.current.loadAddon(fitAddon);
91110

92-
terminal.open(ref.current);
111+
terminalRef.current.open(ref.current);
93112

94113
const resizeListener = debounce(onWindowResize, 100);
95114

@@ -99,34 +118,16 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
99118
}
100119
onWindowResize();
101120
}
102-
terminal.focus();
121+
terminalRef.current.focus();
103122

104123
return () => {
105-
terminal.dispose();
124+
terminalRef.current.dispose();
106125
if (canUseDOM) {
107126
window.removeEventListener('resize', resizeListener);
108127
}
109128
onFocusOut();
110129
};
111-
}, []);
112-
113-
const onBeforeUnload = (event: any) => {
114-
// Firefox requires this when the page is in an iframe
115-
event.preventDefault();
116-
117-
// see "an almost cross-browser solution" at
118-
// https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
119-
event.returnValue = '';
120-
return '';
121-
};
122-
123-
const onFocusIn = () => {
124-
window.addEventListener('beforeunload', onBeforeUnload);
125-
};
126-
127-
const onFocusOut = () => {
128-
window.removeEventListener('beforeunload', onBeforeUnload);
129-
};
130+
}, [cols, fontFamily, fontSize, onData, onFocusOut, onTitleChanged, rows]);
130131

131132
// ensure react never reuses this div by keying it with the terminal widget
132133
// Workaround for xtermjs/xterm.js#3172

packages/module/src/components/SpiceConsole/SpiceConsole.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,28 @@ export const SpiceConsole: React.FunctionComponent<SpiceConsoleProps> = ({
5050
textCtrlAltDel
5151
}) => {
5252
let spiceStaticComponent: React.ReactNode;
53-
let sc: any;
53+
const scRef = React.useRef<any>();
5454
const [status, setStatus] = React.useState(CONNECTING);
5555

56+
const disconnect = React.useCallback(() => {
57+
if (scRef.current) {
58+
scRef.current.stop();
59+
scRef.current = undefined;
60+
}
61+
}, [scRef]);
62+
63+
const onSpiceError = React.useCallback((e: any) => {
64+
disconnect();
65+
setStatus(DISCONNECTED);
66+
onDisconnected(e);
67+
}, [disconnect, setStatus, onDisconnected]);
68+
5669
React.useEffect(() => {
5770
try {
5871
const protocol = encrypt ? 'wss' : 'ws';
5972
const uri = `${protocol}://${host}:${port}/${path}`;
6073

61-
sc = new SpiceMainConn({
74+
scRef.current = new SpiceMainConn({
6275
uri,
6376
/* eslint-disable camelcase */
6477
screen_id: 'spice-screen',
@@ -71,27 +84,14 @@ export const SpiceConsole: React.FunctionComponent<SpiceConsoleProps> = ({
7184
disconnect();
7285
}
7386
return () => disconnect();
74-
}, []);
75-
76-
const disconnect = () => {
77-
if (sc) {
78-
sc.stop();
79-
sc = undefined;
80-
}
81-
};
87+
}, [disconnect, encrypt, host, onInitFailed, onSpiceError, password, path, port]);
8288

8389
const onCtrlAltDel = () => {
84-
if (sc) {
90+
if (scRef.current) {
8591
sendCtrlAltDel();
8692
}
8793
};
8894

89-
const onSpiceError = (e: any) => {
90-
disconnect();
91-
setStatus(DISCONNECTED);
92-
onDisconnected(e);
93-
};
94-
9595
if (!spiceStaticComponent) {
9696
// create just once
9797
spiceStaticComponent = <div id="spice-screen" />;

0 commit comments

Comments
 (0)