Skip to content

Commit b97d194

Browse files
committed
Defer list value stringification until render
This removes an unnecessary array copy from monitor adapter
1 parent 5cb812b commit b97d194

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {FormattedMessage} from 'react-intl';
66

77
import styles from './monitor.css';
88
import {List} from 'react-virtualized';
9+
import {safeStringify} from '../../lib/tw-safe-stringify.js';
910

1011
class ListMonitorScroller extends React.Component {
1112
constructor (props) {
@@ -71,7 +72,9 @@ class ListMonitorScroller extends React.Component {
7172
</div>
7273

7374
) : (
74-
<div className={styles.valueInner}>{this.props.values[index]}</div>
75+
<div className={styles.valueInner}>
76+
{safeStringify(this.props.values[index])}
77+
</div>
7578
)}
7679
</div>
7780
</div>

src/containers/list-monitor.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {getEventXY} from '../lib/touch-utils';
77
import {getVariableValue, setVariableValue} from '../lib/variable-utils';
88
import ListMonitorComponent from '../components/monitor/list-monitor.jsx';
99
import {Map} from 'immutable';
10+
import {safeStringify} from '../lib/tw-safe-stringify.js';
1011

1112
class ListMonitor extends React.Component {
1213
constructor (props) {
@@ -38,7 +39,7 @@ class ListMonitor extends React.Component {
3839

3940
this.setState({
4041
activeIndex: index,
41-
activeValue: this.props.value[index]
42+
activeValue: safeStringify(this.props.value[index])
4243
});
4344
}
4445

src/lib/monitor-adapter.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,8 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
3434
}
3535

3636
// Anything that isn't a string or number, such as a boolean or object, should be converted to string.
37-
if (Array.isArray(value)) {
38-
value = value.slice();
39-
for (let i = 0; i < value.length; i++) {
40-
const item = value[i];
41-
if (typeof item !== 'string' || typeof item !== 'number') {
42-
value[i] = safeStringify(item);
43-
}
44-
}
45-
} else if (typeof value !== 'string' || typeof value !== 'number') {
37+
// For lists, we do this when we display the list row instead of doing a full list copy on every change.
38+
if (!Array.isArray(value) && (typeof value !== 'string' || typeof value !== 'number')) {
4639
value = safeStringify(value);
4740
}
4841

0 commit comments

Comments
 (0)