Skip to content

Commit 6454d2f

Browse files
rubennortefacebook-github-bot
authored andcommitted
Introduce concept of platform objects and serialization (#51256)
Summary: Pull Request resolved: #51256 Changelog: [internal] This is just in preparation for `structuredClone` handling the cloning (or not) of platform objects correctly. It marks all existing Web platform objects in RN as such, and defines the clone method for `DOMRectReadOnly`, `DOMRect` and `DOMException`. Reviewed By: hoxyq Differential Revision: D74574856 fbshipit-source-id: 9e9647fcaafcc1d32fb36e5ee40167871572c544
1 parent 1bb8546 commit 6454d2f

18 files changed

Lines changed: 121 additions & 1 deletion

File tree

packages/react-native/src/private/webapis/dom/events/Event.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import type EventTarget from './EventTarget';
1919

20+
import {setPlatformObject} from '../../webidl/PlatformObjects';
2021
import {
2122
COMPOSED_PATH_KEY,
2223
CURRENT_TARGET_KEY,
@@ -234,3 +235,5 @@ export type EventPhase =
234235
| (typeof Event)['CAPTURING_PHASE']
235236
| (typeof Event)['AT_TARGET']
236237
| (typeof Event)['BUBBLING_PHASE'];
238+
239+
setPlatformObject(Event);

packages/react-native/src/private/webapis/dom/events/EventTarget.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import type {EventPhase} from './Event';
1717

18+
import {setPlatformObject} from '../../webidl/PlatformObjects';
1819
import Event from './Event';
1920
import {
2021
getStopImmediatePropagationFlag,
@@ -229,6 +230,8 @@ export default class EventTarget {
229230
}
230231
}
231232

233+
setPlatformObject(EventTarget);
234+
232235
function validateCallback(callback: EventListener, methodName: string): void {
233236
if (typeof callback !== 'function' && typeof callback !== 'object') {
234237
throw new TypeError(

packages/react-native/src/private/webapis/dom/nodes/ReadOnlyNode.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {InstanceHandle} from './internals/NodeInternals';
1515
import type ReactNativeDocument from './ReactNativeDocument';
1616
import type ReadOnlyElement from './ReadOnlyElement';
1717

18-
import * as ReactNativeFeatureFlags from '../../../featureflags/ReactNativeFeatureFlags';
18+
import {setPlatformObject} from '../../webidl/PlatformObjects';
1919
import {createNodeList} from '../oldstylecollections/NodeList';
2020
import {
2121
getNativeNodeReference,
@@ -288,6 +288,8 @@ export default class ReadOnlyNode {
288288
static DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: number = 32;
289289
}
290290

291+
setPlatformObject(ReadOnlyNode);
292+
291293
export function getChildNodes(
292294
node: ReadOnlyNode,
293295
): $ReadOnlyArray<ReadOnlyNode> {

packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import type {ArrayLike} from '../../utils/ArrayLikeUtils';
1414

1515
import {createValueIterator} from '../../utils/ArrayLikeUtils';
16+
import {setPlatformObject} from '../../webidl/PlatformObjects';
1617

1718
// IMPORTANT: The type definition for this module is defined in `HTMLCollection.js.flow`
1819
// because Flow only supports indexers in classes in declaration files.
@@ -69,6 +70,8 @@ export default class HTMLCollection<T> implements Iterable<T>, ArrayLike<T> {
6970
}
7071
}
7172

73+
setPlatformObject(HTMLCollection);
74+
7275
/**
7376
* This is an internal method to create instances of `HTMLCollection`,
7477
* which avoids leaking its constructor to end users.

packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
createKeyIterator,
1818
createValueIterator,
1919
} from '../../utils/ArrayLikeUtils';
20+
import {setPlatformObject} from '../../webidl/PlatformObjects';
2021

2122
// IMPORTANT: The Flow type definition for this module is defined in `NodeList.js.flow`
2223
// because Flow only supports indexers in classes in declaration files.
@@ -93,6 +94,8 @@ export default class NodeList<T> implements Iterable<T>, ArrayLike<T> {
9394
}
9495
}
9596

97+
setPlatformObject(NodeList);
98+
9699
/**
97100
* This is an internal method to create instances of `NodeList`,
98101
* which avoids leaking its constructor to end users.

packages/react-native/src/private/webapis/errors/DOMException.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
// flowlint unsafe-getters-setters:off
1818

19+
import {setPlatformObject} from '../webidl/PlatformObjects';
20+
1921
const ERROR_NAME_TO_ERROR_CODE_MAP: {[string]: number} = {
2022
IndexSizeError: 1,
2123
HierarchyRequestError: 3,
@@ -159,3 +161,7 @@ for (const code in ERROR_CODES) {
159161
value: ERROR_CODES[code],
160162
});
161163
}
164+
165+
setPlatformObject(DOMException, {
166+
clone: exception => new DOMException(exception.message, exception.name),
167+
});

packages/react-native/src/private/webapis/geometry/DOMRect.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
1515
*/
1616

17+
import {setPlatformObject} from '../webidl/PlatformObjects';
1718
import DOMRectReadOnly, {type DOMRectInit} from './DOMRectReadOnly';
1819

1920
// flowlint unsafe-getters-setters:off
@@ -80,3 +81,7 @@ export default class DOMRect extends DOMRectReadOnly {
8081
return new DOMRect(rect.x, rect.y, rect.width, rect.height);
8182
}
8283
}
84+
85+
setPlatformObject(DOMRect, {
86+
clone: rect => new DOMRect(rect.x, rect.y, rect.width, rect.height),
87+
});

packages/react-native/src/private/webapis/geometry/DOMRectList.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ArrayLike} from '../utils/ArrayLikeUtils';
1414
import type DOMRectReadOnly from './DOMRectReadOnly';
1515

1616
import {createValueIterator} from '../utils/ArrayLikeUtils';
17+
import {setPlatformObject} from '../webidl/PlatformObjects';
1718

1819
// IMPORTANT: The Flow type definition for this module is defined in `DOMRectList.js.flow`
1920
// because Flow only supports indexers in classes in declaration files.
@@ -63,6 +64,8 @@ export default class DOMRectList implements Iterable<DOMRectReadOnly> {
6364
}
6465
}
6566

67+
setPlatformObject(DOMRectList);
68+
6669
/**
6770
* This is an internal method to create instances of `DOMRectList`,
6871
* which avoids leaking its constructor to end users.

packages/react-native/src/private/webapis/geometry/DOMRectReadOnly.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
1515
*/
1616

17+
import {setPlatformObject} from '../webidl/PlatformObjects';
18+
1719
// flowlint sketchy-null:off, unsafe-getters-setters:off
1820

1921
export interface DOMRectInit {
@@ -186,3 +188,7 @@ export default class DOMRectReadOnly {
186188
this.#height = castToNumber(height);
187189
}
188190
}
191+
192+
setPlatformObject(DOMRectReadOnly, {
193+
clone: rect => new DOMRectReadOnly(rect.x, rect.y, rect.width, rect.height),
194+
});

packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {IntersectionObserverId} from './internals/IntersectionObserverManag
1414
import type IntersectionObserverEntry from './IntersectionObserverEntry';
1515

1616
import ReactNativeElement from '../dom/nodes/ReactNativeElement';
17+
import {setPlatformObject} from '../webidl/PlatformObjects';
1718
import * as IntersectionObserverManager from './internals/IntersectionObserverManager';
1819

1920
export type IntersectionObserverCallback = (
@@ -250,6 +251,8 @@ export default class IntersectionObserver {
250251
}
251252
}
252253

254+
setPlatformObject(IntersectionObserver);
255+
253256
/**
254257
* Converts the user defined `threshold` value into an array of sorted valid
255258
* threshold options for `IntersectionObserver` (double ∈ [0, 1]).

0 commit comments

Comments
 (0)