-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 [RUMF-1410] Allow serialization of objects with cyclic references #1783
Conversation
2000bb1
to
14cd826
Compare
Codecov Report
@@ Coverage Diff @@
## main #1783 +/- ##
==========================================
+ Coverage 90.92% 91.10% +0.17%
==========================================
Files 129 129
Lines 5038 5037 -1
Branches 1133 1132 -1
==========================================
+ Hits 4581 4589 +8
+ Misses 457 448 -9
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
14cd826
to
c56f53f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
packages/core/src/tools/utils.ts
Outdated
const isValidObject = (v: any): v is object => typeof v === 'object' && v !== null | ||
const getCyclicReplacer = <T>() => { | ||
// Using a weakmap instead of a weakset to support IE11 | ||
const visited = new WeakMap<object, boolean>() | ||
return (_k: string, v: T) => { | ||
if (isValidObject(v)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥜 nitpick: using a meaningful word instead of a letter could be easier to follow, something like candidate
or value
instead of v
.
Looks great! |
packages/core/src/tools/utils.ts
Outdated
// subclasses. | ||
const restoreObjectPrototypeToJson = detachToJsonMethod(Object.prototype) | ||
const restoreArrayPrototypeToJson = detachToJsonMethod(Array.prototype) | ||
const restoreValuePrototypeToJson = detachToJsonMethod(Object.getPrototypeOf(value)) | ||
const restoreValueToJson = detachToJsonMethod(value) | ||
|
||
// Line below can be removed when migrating to TS4.8+ | ||
const isValidObject = (v: any): v is object => typeof v === 'object' && v !== null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 suggestion: use getType(destination) === 'object'
instead
packages/core/src/tools/utils.ts
Outdated
// subclasses. | ||
const restoreObjectPrototypeToJson = detachToJsonMethod(Object.prototype) | ||
const restoreArrayPrototypeToJson = detachToJsonMethod(Array.prototype) | ||
const restoreValuePrototypeToJson = detachToJsonMethod(Object.getPrototypeOf(value)) | ||
const restoreValueToJson = detachToJsonMethod(value) | ||
|
||
// Line below can be removed when migrating to TS4.8+ | ||
const isValidObject = (v: any): v is object => typeof v === 'object' && v !== null | ||
const getCyclicReplacer = <T>() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥜 nitpick: the type parameter T
isn't useful here, and could be replaced by unknown
in the argument list
🥜 nitpick: FMU you create a closure just to keep the circularReferenceChecker
variable private. IMO this is not worth it and the following would be a bit easier to read:
const circularReferenceChecker = createCircularReferenceChecker()
const cyclicReplacer = (_key: string, value: unknown) => {
if (isValidObject(value) && circularReferenceChecker.hasAlreadyBeenSeen(value)) {
return '<warning: cyclic reference not serialized>'
}
return value
}
} | ||
const array: any[] = [] | ||
// Using a weakmap instead of a weakset to support IE11 | ||
const map: WeakMap<any, boolean> = new WeakMap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 praise: great, love it :)
/to-staging |
🚂 MergeFlow Merge branch is merge-1783-to-staging-44 |
5b254b4
to
146da85
Compare
Motivation
Fix for #807
Changes
Added a replacer within the jsonStringify function that removes cyclic references while serializing.
Testing
I have gone over the contributing documentation.