@@ -15,7 +15,6 @@ import type {
1515import type { MixedElement } from 'react' ;
1616import type { RootTag } from 'react-native' ;
1717import type ReactNativeDocument from 'react-native/src/private/webapis/dom/nodes/ReactNativeDocument' ;
18- import type ReadOnlyNode from 'react-native/src/private/webapis/dom/nodes/ReadOnlyNode' ;
1918
2019import * as Benchmark from './Benchmark' ;
2120import { getConstants } from './Constants' ;
@@ -25,11 +24,14 @@ import NativeFantom, {
2524 NativeEventCategory ,
2625} from 'react-native/src/private/testing/fantom/specs/NativeFantom' ;
2726import { getNativeNodeReference } from 'react-native/src/private/webapis/dom/nodes/internals/NodeInternals' ;
27+ import ReadOnlyNode from 'react-native/src/private/webapis/dom/nodes/ReadOnlyNode' ;
2828
2929const nativeRuntimeScheduler = global . nativeRuntimeScheduler ;
3030const { unstable_scheduleCallback, unstable_ImmediatePriority} =
3131 nativeRuntimeScheduler ;
3232
33+ type NodeOrRef = ReadOnlyNode | $ReadOnly < { current : ?ReadOnlyNode } > ;
34+
3335export type RootConfig = {
3436 viewportWidth ?: number ,
3537 viewportHeight ?: number ,
@@ -218,17 +220,19 @@ export function unstable_produceFramesForDuration(milliseconds: number) {
218220 * Note: This API is marked as unstable and may change in future versions.
219221 */
220222export function unstable_getDirectManipulationProps (
221- node : ReadOnlyNode ,
223+ nodeOrRef : NodeOrRef ,
222224) : $ReadOnly < {
223225 [ string ] : mixed ,
224226} > {
227+ const node = getNode ( nodeOrRef ) ;
225228 const shadowNode = getNativeNodeReference ( node ) ;
226229 return NativeFantom . getDirectManipulationProps ( shadowNode ) ;
227230}
228231
229- export function unstable_getFabricUpdateProps ( node : ReadOnlyNode ) : $ReadOnly < {
232+ export function unstable_getFabricUpdateProps ( nodeOrRef : NodeOrRef ) : $ReadOnly < {
230233 [ string ] : mixed ,
231234} > {
235+ const node = getNode ( nodeOrRef ) ;
232236 const shadowNode = getNativeNodeReference ( node ) ;
233237 return NativeFantom . getFabricUpdateProps ( shadowNode ) ;
234238}
@@ -394,11 +398,12 @@ export function createRoot(rootConfig?: RootConfig): Root {
394398 * ```
395399 */
396400export function enqueueNativeEvent (
397- node : ReadOnlyNode ,
401+ nodeOrRef : NodeOrRef ,
398402 type : string ,
399403 payload ?: $ReadOnly < { [ key : string ] : mixed } > ,
400404 options ?: $ReadOnly < { category ?: NativeEventCategory , isUnique ?: boolean } > ,
401405) {
406+ const node = getNode ( nodeOrRef ) ;
402407 const shadowNode = getNativeNodeReference ( node ) ;
403408 NativeFantom . enqueueNativeEvent (
404409 shadowNode ,
@@ -425,11 +430,13 @@ export function enqueueNativeEvent(
425430 * ```
426431 */
427432export function dispatchNativeEvent (
428- node : ReadOnlyNode ,
433+ nodeOrRef : NodeOrRef ,
429434 type : string ,
430435 payload ?: $ReadOnly < { [ key : string ] : mixed } > ,
431436 options ?: $ReadOnly < { category ?: NativeEventCategory , isUnique ?: boolean } > ,
432437) {
438+ const node = getNode ( nodeOrRef ) ;
439+
433440 runOnUIThread ( ( ) => {
434441 enqueueNativeEvent ( node , type , payload , options ) ;
435442 } ) ;
@@ -487,9 +494,10 @@ export type ScrollEventOptions = {
487494 * ```
488495 */
489496export function enqueueScrollEvent (
490- node : ReadOnlyNode ,
497+ nodeOrRef : NodeOrRef ,
491498 options : ScrollEventOptions ,
492499) {
500+ const node = getNode ( nodeOrRef ) ;
493501 const shadowNode = getNativeNodeReference ( node ) ;
494502 NativeFantom . enqueueScrollEvent ( shadowNode , options ) ;
495503}
@@ -525,7 +533,9 @@ export function enqueueScrollEvent(
525533 * // Assert that changes from Fantom.scrollTo are in effect.
526534 * ```
527535 */
528- export function scrollTo ( node : ReadOnlyNode , options : ScrollEventOptions ) {
536+ export function scrollTo ( nodeOrRef : NodeOrRef , options : ScrollEventOptions ) {
537+ const node = getNode ( nodeOrRef ) ;
538+
529539 runOnUIThread ( ( ) => {
530540 enqueueScrollEvent ( node , options ) ;
531541 } ) ;
@@ -558,9 +568,10 @@ export function scrollTo(node: ReadOnlyNode, options: ScrollEventOptions) {
558568 * ```
559569 */
560570export function enqueueModalSizeUpdate (
561- node : ReadOnlyNode ,
571+ nodeOrRef : NodeOrRef ,
562572 size : $ReadOnly < { width : number , height : number } > ,
563573) {
574+ const node = getNode ( nodeOrRef ) ;
564575 const shadowNode = getNativeNodeReference ( node ) ;
565576 NativeFantom . enqueueModalSizeUpdate ( shadowNode , size . width , size . height ) ;
566577}
@@ -572,28 +583,6 @@ export type {
572583 TestOptions as BenchmarkTestOptions ,
573584} from './Benchmark' ;
574585
575- /**
576- * Quick and dirty polyfills required by tinybench.
577- */
578-
579- if ( typeof global . Event === 'undefined' ) {
580- global . Event =
581- require ( 'react-native/src/private/webapis/dom/events/Event' ) . default ;
582- } else {
583- console . warn (
584- 'The global Event class is already defined. If this API is already defined by React Native, you might want to remove this logic.' ,
585- ) ;
586- }
587-
588- if ( typeof global . EventTarget === 'undefined' ) {
589- global . EventTarget =
590- require ( 'react-native/src/private/webapis/dom/events/EventTarget' ) . default ;
591- } else {
592- console . warn (
593- 'The global Event class is already defined. If this API is already defined by React Native, you might want to remove this logic.' ,
594- ) ;
595- }
596-
597586/**
598587 * Returns a function that returns the current reference count for the supplied
599588 * element's shadow node. If the reference count is zero, that means the shadow
@@ -602,9 +591,10 @@ if (typeof global.EventTarget === 'undefined') {
602591 * @param node The node for which to create a reference counting function.
603592 */
604593export function createShadowNodeReferenceCounter (
605- node : ReadOnlyNode ,
594+ nodeOrRef : NodeOrRef ,
606595) : ( ) => number {
607- let shadowNode = getNativeNodeReference ( node ) ;
596+ const node = getNode ( nodeOrRef ) ;
597+ const shadowNode = getNativeNodeReference ( node ) ;
608598 return NativeFantom . createShadowNodeReferenceCounter ( shadowNode ) ;
609599}
610600
@@ -615,9 +605,10 @@ export function createShadowNodeReferenceCounter(
615605 * @param node The node for which to create a revision getter.
616606 */
617607export function createShadowNodeRevisionGetter (
618- node : ReadOnlyNode ,
608+ nodeOrRef : NodeOrRef ,
619609) : ( ) = > ?number {
620- let shadowNode = getNativeNodeReference ( node ) ;
610+ const node = getNode ( nodeOrRef ) ;
611+ const shadowNode = getNativeNodeReference ( node ) ;
621612 return NativeFantom . createShadowNodeRevisionGetter ( shadowNode ) ;
622613}
623614
@@ -681,4 +672,36 @@ function runLogBoxCheck() {
681672 }
682673}
683674
675+ function getNode ( nodeOrRef : NodeOrRef ) : ReadOnlyNode {
676+ if ( nodeOrRef instanceof ReadOnlyNode ) {
677+ return nodeOrRef ;
678+ } else if ( nodeOrRef . current != null ) {
679+ return nodeOrRef . current ;
680+ } else {
681+ throw new TypeError ( 'Could not get node from ref' ) ;
682+ }
683+ }
684+
685+ /**
686+ * Quick and dirty polyfills required by tinybench.
687+ */
688+
689+ if ( typeof global . Event === 'undefined' ) {
690+ global . Event =
691+ require ( 'react-native/src/private/webapis/dom/events/Event' ) . default ;
692+ } else {
693+ console . warn (
694+ 'The global Event class is already defined. If this API is already defined by React Native, you might want to remove this logic.' ,
695+ ) ;
696+ }
697+
698+ if ( typeof global . EventTarget === 'undefined' ) {
699+ global . EventTarget =
700+ require ( 'react-native/src/private/webapis/dom/events/EventTarget' ) . default ;
701+ } else {
702+ console . warn (
703+ 'The global Event class is already defined. If this API is already defined by React Native, you might want to remove this logic.' ,
704+ ) ;
705+ }
706+
684707global . __FANTOM_PACKAGE_LOADED__ = true ;
0 commit comments