Skip to content

Commit ad19592

Browse files
committed
Let monitors handle objects properly
1 parent a77ac0e commit ad19592

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/common/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 default sanitizeVariableType;

src/scaffolding/monitor.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import styles from './style.css';
44
import {readAsText} from '../common/readers';
55
import downloadBlob from './download';
66

7+
import sanitizeVariableType from '../common/safe-stringify';
8+
79
class Monitor {
810
constructor (parent, monitor) {
911
this.parent = parent;
@@ -194,7 +196,7 @@ class VariableMonitor extends Monitor {
194196
return;
195197
}
196198

197-
let value = monitor.get('value');
199+
let value = sanitizeVariableType(monitor.get('value'), '');
198200
if (typeof value === 'number') {
199201
value = Number(value.toFixed(6));
200202
}
@@ -547,7 +549,7 @@ class ListMonitor extends Monitor {
547549
this.root.style.width = `${this.width}px`;
548550
this.root.style.height = `${this.height}px`;
549551

550-
this.updateValue(monitor.get('value'));
552+
this.updateValue(sanitizeVariableType(monitor.get('value'), 'list'));
551553
}
552554

553555
createRow (index) {

0 commit comments

Comments
 (0)