mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Introduce better mechanism for non-serializable built-ins in structuredClone (#51255)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51255 Changelog: [internal] Just a small refactor of how we handle non-serializable built-ins in structuredClone Reviewed By: hoxyq Differential Revision: D74398113 fbshipit-source-id: 26211920fcd4cf11c5da278e213eaa2b6b9aa1b5
This commit is contained in:
1 parent
caaa5c93fb
commit
1bb8546698
1 file changed
+18
-6
+18
-6
@@ -155,12 +155,7 @@ function structuredCloneInternal<T>(value: T): T {
|
||||
}
|
||||
|
||||
// Known non-serializable objects.
|
||||
// TODO: Handle this more holistically
|
||||
if (
|
||||
value instanceof WeakMap ||
|
||||
value instanceof WeakSet ||
|
||||
value instanceof Promise
|
||||
) {
|
||||
if (isNonSerializableObject(value)) {
|
||||
throw new DOMException(
|
||||
`Failed to execute 'structuredClone' on 'Window': ${String(value)} could not be cloned.`,
|
||||
'DataCloneError',
|
||||
@@ -208,3 +203,20 @@ export default function structuredClone<T>(value: T): T {
|
||||
memory.clear();
|
||||
}
|
||||
}
|
||||
|
||||
const NON_SERIALIZABLE_OBJECT_KEY = Symbol('nonSerializableObject');
|
||||
|
||||
function isNonSerializableObject<T: interface {}>(obj: T): boolean {
|
||||
// $FlowExpectedError[invalid-in-lhs]
|
||||
return NON_SERIALIZABLE_OBJECT_KEY in obj;
|
||||
}
|
||||
|
||||
function markClassAsNonSerializable<T>(cls: Class<T>): void {
|
||||
// $FlowExpectedError[incompatible-use]
|
||||
cls.prototype[NON_SERIALIZABLE_OBJECT_KEY] = true;
|
||||
}
|
||||
|
||||
// Non-serializable built-ins.
|
||||
markClassAsNonSerializable(WeakMap);
|
||||
markClassAsNonSerializable(WeakSet);
|
||||
markClassAsNonSerializable(Promise);
|
||||
Reference in new issue
Block a user