Skip to content

Commit

Permalink
fix(logging): strip _subscription property
Browse files Browse the repository at this point in the history
This doesn't provide any debugging ability and is rather noisy on things like invoked observables
  • Loading branch information
UberMouse committed Aug 12, 2024
1 parent 5b5357b commit 4d567e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ export function mergeMeta(meta: Record<string, any>) {
return acc;
}, {});
}
function getCircularReplacer() {
function getCircularReplacer(stripKeys: string[]) {
const seen = new WeakSet();
return (key: string, value: any) => {
if (stripKeys.includes(key)) {
return;
}

if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
// Circular reference found, discard key
Expand All @@ -177,6 +181,9 @@ function getCircularReplacer() {
};
}

export function toJSON<T = unknown>(value: unknown): T {
return JSON.parse(JSON.stringify(value, getCircularReplacer()));
export function toJSON<T = unknown>(
value: unknown,
stripKeys = [] as string[]
): T {
return JSON.parse(JSON.stringify(value, getCircularReplacer(stripKeys)));
}
7 changes: 4 additions & 3 deletions src/xstateTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,18 @@ export function buildRootComponent<TMachine extends AnyXstateTreeMachine>(
const lastSnapshot =
lastSnapshotsRef.current[event.actorRef.sessionId];

const strippedKeys = ["_subscription"];
if (!lastSnapshot) {
console.log(
`[xstate-tree] initial snapshot: ${event.actorRef.id}`,
toJSON(event.snapshot)
toJSON(event.snapshot, strippedKeys)
);
} else {
console.log(
`[xstate-tree] snapshot: ${event.actorRef.id} transitioning to`,
toJSON(event.snapshot),
toJSON(event.snapshot, strippedKeys),
"from",
toJSON(lastSnapshot)
toJSON(lastSnapshot, strippedKeys)
);
}

Expand Down

0 comments on commit 4d567e6

Please sign in to comment.