Skip to content

Commit 4d567e6

Browse files
committed
fix(logging): strip _subscription property
This doesn't provide any debugging ability and is rather noisy on things like invoked observables
1 parent 5b5357b commit 4d567e6

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/utils.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,13 @@ export function mergeMeta(meta: Record<string, any>) {
162162
return acc;
163163
}, {});
164164
}
165-
function getCircularReplacer() {
165+
function getCircularReplacer(stripKeys: string[]) {
166166
const seen = new WeakSet();
167167
return (key: string, value: any) => {
168+
if (stripKeys.includes(key)) {
169+
return;
170+
}
171+
168172
if (typeof value === "object" && value !== null) {
169173
if (seen.has(value)) {
170174
// Circular reference found, discard key
@@ -177,6 +181,9 @@ function getCircularReplacer() {
177181
};
178182
}
179183

180-
export function toJSON<T = unknown>(value: unknown): T {
181-
return JSON.parse(JSON.stringify(value, getCircularReplacer()));
184+
export function toJSON<T = unknown>(
185+
value: unknown,
186+
stripKeys = [] as string[]
187+
): T {
188+
return JSON.parse(JSON.stringify(value, getCircularReplacer(stripKeys)));
182189
}

src/xstateTree.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,18 @@ export function buildRootComponent<TMachine extends AnyXstateTreeMachine>(
352352
const lastSnapshot =
353353
lastSnapshotsRef.current[event.actorRef.sessionId];
354354

355+
const strippedKeys = ["_subscription"];
355356
if (!lastSnapshot) {
356357
console.log(
357358
`[xstate-tree] initial snapshot: ${event.actorRef.id}`,
358-
toJSON(event.snapshot)
359+
toJSON(event.snapshot, strippedKeys)
359360
);
360361
} else {
361362
console.log(
362363
`[xstate-tree] snapshot: ${event.actorRef.id} transitioning to`,
363-
toJSON(event.snapshot),
364+
toJSON(event.snapshot, strippedKeys),
364365
"from",
365-
toJSON(lastSnapshot)
366+
toJSON(lastSnapshot, strippedKeys)
366367
);
367368
}
368369

0 commit comments

Comments
 (0)