-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(js): Adds fix for circular references in inputs and outputs (#985)
~~This does add a few stringify calls - we could do this more efficiently if needed but I am not sure it's worth prematurely optimizing~~ Fixes #962
- Loading branch information
1 parent
c6f4cad
commit b5e583f
Showing
4 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export const CIRCULAR_VALUE_REPLACEMENT_STRING = "[Circular]"; | ||
|
||
/** | ||
* JSON.stringify version that handles circular references by replacing them | ||
* with an object marking them as such ({ result: "[Circular]" }). | ||
*/ | ||
export const stringifyForTracing = (value: any): string => { | ||
const seen = new WeakSet(); | ||
|
||
const serializer = (_: string, value: any): any => { | ||
if (typeof value === "object" && value !== null) { | ||
if (seen.has(value)) { | ||
return { | ||
result: CIRCULAR_VALUE_REPLACEMENT_STRING, | ||
}; | ||
} | ||
seen.add(value); | ||
} | ||
return value; | ||
}; | ||
return JSON.stringify(value, serializer); | ||
}; |