Skip to content

Commit b6367bd

Browse files
authored
Completely rework monitor handling of Object and Arrays (#1113)
1 parent 22fceb9 commit b6367bd

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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';
89

910
import styles from './monitor-list.css';
1011

@@ -36,7 +37,7 @@ const MonitorList = props => (
3637
params={monitorData.params}
3738
spriteName={monitorData.spriteName}
3839
targetId={monitorData.targetId}
39-
value={monitorData.value}
40+
value={sanitizeVariableType(monitorData.value, monitorData.mode)}
4041
width={monitorData.width}
4142
x={monitorData.x}
4243
y={monitorData.y}

src/lib/monitor-adapter.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ import OpcodeLabels from './opcode-labels.js';
22

33
const isUndefined = a => typeof a === 'undefined';
44

5-
const circularReplacer = () => {
6-
const stack = new Set();
7-
return (_, value) => {
8-
if (typeof value === 'object' && value !== null) {
9-
if (stack.has(value)) return Array.isArray(value) ? '[...]' : '{...}';
10-
stack.add(value);
11-
}
12-
return value;
13-
};
14-
};
15-
165
/**
176
* Convert monitors from VM format to what the GUI needs to render.
187
* - Convert opcode to a label and a category
@@ -48,21 +37,13 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
4837
value = value.toString();
4938
}
5039

51-
// Turn the value to a string, to handle JSON values
52-
// do not convert arrays as it will be confused for lists
53-
if (typeof value === 'object' && !Array.isArray(value)) {
54-
value = JSON.stringify(value, circularReplacer());
55-
}
56-
57-
// Lists can contain booleans or Objects, which should also be turned to strings
40+
// Lists can contain booleans, which should also be turned to strings
5841
if (Array.isArray(value)) {
5942
value = value.slice();
6043
for (let i = 0; i < value.length; i++) {
6144
const item = value[i];
6245
if (typeof item === 'boolean') {
6346
value[i] = item.toString();
64-
} else if (typeof value[i] === 'object' && !Array.isArray(value[i])) {
65-
value[i] = JSON.stringify(item, circularReplacer());
6647
}
6748
}
6849
}

src/lib/tw-safe-stringify.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
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+
}
20+
};
21+
22+
const sanitizeVariableType = (input, type) => {
23+
if (type === "list") {
24+
return input.map(item => sanitize(item));
25+
} else {
26+
return sanitize(input);
27+
}
28+
};
29+
30+
export { sanitizeVariableType };

0 commit comments

Comments
 (0)