@koordinates/xstate-tree 5.2.1 → 5.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/utils.js +30 -1
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -121,12 +121,35 @@ function mergeMeta(meta) {
121
121
  }, {});
122
122
  }
123
123
  exports.mergeMeta = mergeMeta;
124
+ function isReactElement(value) {
125
+ if (typeof value !== "object" || value === null) {
126
+ return false;
127
+ }
128
+ const v = value;
129
+ // React elements have $$typeof set to Symbol(react.element) or
130
+ // Symbol(react.transitional.element)
131
+ if (typeof v.$$typeof === "symbol" && String(v.$$typeof).includes("react")) {
132
+ return true;
133
+ }
134
+ return false;
135
+ }
124
136
  function getCircularReplacer(stripKeys) {
125
137
  const seen = new WeakSet();
126
138
  return (key, value) => {
127
139
  if (stripKeys.includes(key)) {
128
140
  return;
129
141
  }
142
+ // Replace React elements with a placeholder to avoid
143
+ // traversing massive internal fiber structures
144
+ if (isReactElement(value)) {
145
+ const type = value.type;
146
+ const name = typeof type === "string"
147
+ ? type
148
+ : typeof type === "function"
149
+ ? type.displayName || type.name || "Anonymous"
150
+ : "Unknown";
151
+ return `[React Element: ${name}]`;
152
+ }
130
153
  if (typeof value === "object" && value !== null) {
131
154
  if (seen.has(value)) {
132
155
  // Circular reference found, discard key
@@ -139,6 +162,12 @@ function getCircularReplacer(stripKeys) {
139
162
  };
140
163
  }
141
164
  function toJSON(value, stripKeys = []) {
142
- return JSON.parse(JSON.stringify(value, getCircularReplacer(stripKeys)));
165
+ const start = performance.now();
166
+ const result = JSON.parse(JSON.stringify(value, getCircularReplacer(stripKeys)));
167
+ const elapsed = performance.now() - start;
168
+ if (typeof result === "object" && result !== null) {
169
+ result.__toJSON_ms = Math.round(elapsed * 100) / 100;
170
+ }
171
+ return result;
143
172
  }
144
173
  exports.toJSON = toJSON;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@koordinates/xstate-tree",
3
3
  "main": "lib/index.js",
4
4
  "types": "lib/xstate-tree.d.ts",
5
- "version": "5.2.1",
5
+ "version": "5.2.2",
6
6
  "license": "MIT",
7
7
  "description": "Build UIs with Actors using xstate and React",
8
8
  "keywords": [