soiz1 commited on
Commit
f3442a0
·
verified ·
1 Parent(s): 256c57a

Update src/lib/monitor-adapter.js

Browse files
Files changed (1) hide show
  1. src/lib/monitor-adapter.js +30 -11
src/lib/monitor-adapter.js CHANGED
@@ -2,6 +2,17 @@ import OpcodeLabels from './opcode-labels.js';
2
 
3
  const isUndefined = a => typeof a === 'undefined';
4
 
 
 
 
 
 
 
 
 
 
 
 
5
  /**
6
  * Convert monitors from VM format to what the GUI needs to render.
7
  * - Convert opcode to a label and a category
@@ -36,7 +47,7 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
36
  if (typeof value === 'boolean') {
37
  value = value.toString();
38
  }
39
-
40
  // Lists can contain booleans, which should also be turned to strings
41
  if (Array.isArray(value)) {
42
  value = value.slice();
@@ -45,21 +56,29 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
45
  if (typeof item === 'boolean') {
46
  value[i] = item.toString();
47
  }
48
- if (typeof item === 'object' &&
49
- typeof (item.toListItem || value.toMonitorContent || item.toReporterContent) === 'function') {
50
- value[i].isHTML = true;
 
 
 
 
51
  }
52
  }
53
  }
54
 
55
  let isHTML = false;
56
- if (typeof value === 'object' &&
57
- typeof (value.toMonitorContent || value.toReporterContent) === 'function') {
58
- value = value.toMonitorContent
59
- ? value.toMonitorContent()
60
- : value.toReporterContent();
61
- isHTML = true;
 
 
 
 
62
  }
63
 
64
  return {id, label, category, value, isHTML};
65
- }
 
2
 
3
  const isUndefined = a => typeof a === 'undefined';
4
 
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
+
16
  /**
17
  * Convert monitors from VM format to what the GUI needs to render.
18
  * - Convert opcode to a label and a category
 
47
  if (typeof value === 'boolean') {
48
  value = value.toString();
49
  }
50
+
51
  // Lists can contain booleans, which should also be turned to strings
52
  if (Array.isArray(value)) {
53
  value = value.slice();
 
56
  if (typeof item === 'boolean') {
57
  value[i] = item.toString();
58
  }
59
+ if (typeof item === 'object') {
60
+ // check if this is a pure object or custom display
61
+ if (typeof (item.toListItem || value.toMonitorContent || item.toReporterContent) === 'function') {
62
+ value[i].isHTML = true;
63
+ } else {
64
+ value[i] = JSON.stringify(item, circularReplacer());
65
+ }
66
  }
67
  }
68
  }
69
 
70
  let isHTML = false;
71
+ if (typeof value === 'object') {
72
+ // check if this is a pure object or custom display
73
+ if (typeof (value.toMonitorContent || value.toReporterContent) === 'function') {
74
+ value = value.toMonitorContent
75
+ ? value.toMonitorContent() : value.toReporterContent();
76
+ isHTML = true;
77
+ } else if (!Array.isArray(value)) {
78
+ // only applies to objects
79
+ value = JSON.stringify(value, circularReplacer());
80
+ }
81
  }
82
 
83
  return {id, label, category, value, isHTML};
84
+ }