Skip to content

Commit 965166b

Browse files
authored
Port TW Monitor Changes (#178)
1 parent c317121 commit 965166b

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
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

@@ -37,7 +38,7 @@ const MonitorList = props => (
3738
params={monitorData.params}
3839
spriteName={monitorData.spriteName}
3940
targetId={monitorData.targetId}
40-
value={monitorData.value}
41+
value={sanitizeVariableType(monitorData.value, monitorData.mode)}
4142
width={monitorData.width}
4243
x={monitorData.x}
4344
y={monitorData.y}

src/lib/monitor-adapter.js

Lines changed: 0 additions & 16 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 function replacer(_, 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
@@ -64,8 +53,6 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
6453
// check if this is a pure object or custom display
6554
if (typeof (item.toListItem || value.toMonitorContent || item.toReporterContent) === 'function') {
6655
value[i].isHTML = true;
67-
} else {
68-
value[i] = JSON.stringify(item, circularReplacer());
6956
}
7057
}
7158
}
@@ -78,9 +65,6 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
7865
value = value.toMonitorContent
7966
? value.toMonitorContent() : value.toReporterContent();
8067
isHTML = true;
81-
} else if (!Array.isArray(value)) {
82-
// only applies to objects
83-
value = JSON.stringify(value, circularReplacer());
8468
}
8569
}
8670

src/lib/tw-safe-stringify.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Since PenguinMod has custom monitor displays,
3+
* we have to handle that too when sanitizing.
4+
*/
5+
const isCustomMonitor = (value) => {
6+
if (
7+
typeof (item.toListItem || value.toMonitorContent || item.toReporterContent) === 'function'
8+
) {
9+
return true;
10+
} else {
11+
return false
12+
}
13+
}
14+
15+
const circularReplacer = () => {
16+
const seen = new WeakSet();
17+
return (_, value) => {
18+
if (typeof value === 'object' && value !== null) {
19+
if (seen.has(value)) {
20+
return Array.isArray(value) ? '[...]' : '{...}';
21+
}
22+
seen.add(value);
23+
}
24+
return value;
25+
};
26+
};
27+
28+
const sanitize = (input) => {
29+
if (
30+
typeof input === "object" && input !== null
31+
&& !isCustomMonitor(input)
32+
) {
33+
return JSON.stringify(input, circularReplacer());
34+
} else {
35+
return input;
36+
}
37+
};
38+
39+
const sanitizeVariableType = (input, type) => {
40+
if (type === "list") {
41+
return input.map(item => sanitize(item));
42+
} else {
43+
return sanitize(input);
44+
}
45+
};
46+
47+
export { sanitizeVariableType };

0 commit comments

Comments
 (0)