Skip to content

Commit 5cb812b

Browse files
committed
Rewrite #1113
- It did an unnecessary extra list copy by using .map. Might've caused extra re-renders though not sure. - Did not stringify null - Had a lot of lint errors though we don't enforce that here
1 parent df5838f commit 5cb812b

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

src/components/monitor-list/monitor-list.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Monitor from '../../containers/monitor.jsx';
55
import PropTypes from 'prop-types';
66
import {OrderedMap} from 'immutable';
77
import {stageSizeToTransform} from '../../lib/screen-utils';
8-
import {sanitizeVariableType} from '../../lib/tw-safe-stringify.js';
98

109
import styles from './monitor-list.css';
1110

@@ -37,7 +36,7 @@ const MonitorList = props => (
3736
params={monitorData.params}
3837
spriteName={monitorData.spriteName}
3938
targetId={monitorData.targetId}
40-
value={sanitizeVariableType(monitorData.value, monitorData.mode)}
39+
value={monitorData.value}
4140
width={monitorData.width}
4241
x={monitorData.x}
4342
y={monitorData.y}

src/lib/monitor-adapter.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import OpcodeLabels from './opcode-labels.js';
2+
import {safeStringify} from './tw-safe-stringify.js';
23

34
const isUndefined = a => typeof a === 'undefined';
45

@@ -32,20 +33,17 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
3233
value = Number(value.toFixed(6));
3334
}
3435

35-
// Turn the value to a string, to handle boolean values
36-
if (typeof value === 'boolean') {
37-
value = value.toString();
38-
}
39-
40-
// Lists can contain booleans, which should also be turned to strings
36+
// Anything that isn't a string or number, such as a boolean or object, should be converted to string.
4137
if (Array.isArray(value)) {
4238
value = value.slice();
4339
for (let i = 0; i < value.length; i++) {
4440
const item = value[i];
45-
if (typeof item === 'boolean') {
46-
value[i] = item.toString();
41+
if (typeof item !== 'string' || typeof item !== 'number') {
42+
value[i] = safeStringify(item);
4743
}
4844
}
45+
} else if (typeof value !== 'string' || typeof value !== 'number') {
46+
value = safeStringify(value);
4947
}
5048

5149
return {id, label, category, value};

src/lib/tw-safe-stringify.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
const circularReplacer = () => {
2-
const seen = new WeakSet();
3-
return (_, value) => {
4-
if (typeof value === 'object' && value !== null) {
5-
if (seen.has(value)) {
6-
return Array.isArray(value) ? '[...]' : '{...}';
7-
}
8-
seen.add(value);
9-
}
10-
return value;
11-
};
12-
};
13-
14-
const sanitize = (input) => {
15-
if (typeof input === "object" && input !== null) {
16-
return JSON.stringify(input, circularReplacer());
17-
} else {
18-
return input;
19-
}
2+
const seen = new WeakSet();
3+
return (_, value) => {
4+
if (typeof value === 'object' && value !== null) {
5+
if (seen.has(value)) {
6+
return Array.isArray(value) ? '[...]' : '{...}';
7+
}
8+
seen.add(value);
9+
}
10+
return value;
11+
};
2012
};
2113

22-
const sanitizeVariableType = (input, type) => {
23-
if (type === "list") {
24-
return input.map(item => sanitize(item));
25-
} else {
26-
return sanitize(input);
27-
}
14+
/**
15+
* Safely stringify, properly handling circular relations.
16+
* @param {unknown} input Any value
17+
* @returns {string} A stringified version of the input.
18+
*/
19+
export const safeStringify = input => {
20+
if (typeof input === 'object' && input !== null) {
21+
return JSON.stringify(input, circularReplacer());
22+
}
23+
return `${input}`;
2824
};
29-
30-
export { sanitizeVariableType };

0 commit comments

Comments
 (0)