Skip to content
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

Upgrade structuredClone options object with skipNotSerializable to avoid cloning exceptions #10255

Open
Juraj-Masiar opened this issue Apr 5, 2024 · 0 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: serialize and transfer

Comments

@Juraj-Masiar
Copy link

Juraj-Masiar commented Apr 5, 2024

What problem are you trying to solve?

Practically, I want to replace JSON.parse(JSON.stringify(obj)) with structuredClone(obj), but there is a big difference in behavior - the exception the structuredClone throws when it finds anything that can't be serialized, for example a function.
Example:

structuredClone({a() {}})

What solutions exist today?

Catching the exception doesn't help at all, because you still need a clone, so you need to clean the object first, which is likely much more work than the original JSON solution.

How would you solve it?

By providing a new switch, for example skipNotSerializable.
This would cause cloning algorithm to ignore properties that can't be cloned while still returning a clone of the rest of the object.

Anything else?

MDN:
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone#options
Review of structured clone algorithms:
tc39/ecma262#2555

@Juraj-Masiar Juraj-Masiar added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: serialize and transfer
Development

No branches or pull requests

2 participants