Create TurboModule for test helpers (#48283)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48283

Migrate from custom JSI integration to use TurboModule base-class.

This doesn't use codegen right now for ease of migration, and to avoid needing to setup a js_library buck definition for react-native-fantom. We should also figure out what the right abstraction/division of responsibilities is going forward for TesterAppDelegate and FantomModule.

Changelog: [Internal]

Reviewed By: rubennorte

Differential Revision: D67111850

fbshipit-source-id: e1623d80f1f25ec123315b3620930dd17dd8a8a7
This commit is contained in:
Pieter De Baets
2024-12-16 04:06:27 -08:00
committed by Facebook GitHub Bot
parent 2fee13094b
commit ba8136e41d
3 changed files with 40 additions and 19 deletions
+5 -12
View File
@@ -9,6 +9,7 @@
* @oncall react_native
*/
import FantomModule from './specs/NativeFantomModule';
// $FlowExpectedError[untyped-import]
import micromatch from 'micromatch';
import * as React from 'react';
@@ -19,12 +20,6 @@ export type RenderOutputConfig = {
includeLayoutMetrics?: boolean,
};
// match RenderFormatOptions.h
type NativeRenderFormatOptions = {
includeRoot: boolean,
includeLayoutMetrics: boolean,
};
type FantomJsonObject = {
type: string,
props: {[key: string]: string},
@@ -117,14 +112,12 @@ export default function getFantomRenderedOutput(
includeLayoutMetrics = false,
...fantomConfig
} = config;
const nativeConfig: NativeRenderFormatOptions = {
includeRoot,
includeLayoutMetrics,
};
return new FantomRenderedOutput(
JSON.parse(
global.$$JSTesterModuleName$$.getRenderedOutput(surfaceId, nativeConfig),
FantomModule.getRenderedOutput(surfaceId, {
includeRoot,
includeLayoutMetrics,
}),
),
fantomConfig,
);
+6 -7
View File
@@ -15,6 +15,7 @@ import type {
import type {MixedElement} from 'react';
import getFantomRenderedOutput from './getFantomRenderedOutput';
import FantomModule from './specs/NativeFantomModule';
import ReactFabric from 'react-native/Libraries/Renderer/shims/ReactFabric';
let globalSurfaceIdCounter = 1;
@@ -34,7 +35,7 @@ class Root {
render(element: MixedElement) {
if (!this.#hasRendered) {
global.$$JSTesterModuleName$$.startSurface(this.#surfaceId);
FantomModule.startSurface(this.#surfaceId);
this.#hasRendered = true;
}
@@ -42,15 +43,13 @@ class Root {
}
getMountingLogs(): Array<string> {
return global.$$JSTesterModuleName$$.getMountingManagerLogs(
this.#surfaceId,
);
return FantomModule.getMountingManagerLogs(this.#surfaceId);
}
destroy() {
// TODO: check for leaks.
global.$$JSTesterModuleName$$.stopSurface(this.#surfaceId);
global.$$JSTesterModuleName$$.flushMessageQueue();
FantomModule.stopSurface(this.#surfaceId);
FantomModule.flushMessageQueue();
}
getRenderedOutput(config: RenderOutputConfig = {}): FantomRenderedOutput {
@@ -81,7 +80,7 @@ export function runTask(task: () => void | Promise<void>) {
try {
flushingQueue = true;
global.$$JSTesterModuleName$$.flushMessageQueue();
FantomModule.flushMessageQueue();
} finally {
flushingQueue = false;
}
@@ -0,0 +1,29 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native';
// match RenderFormatOptions.h
export type RenderFormatOptions = {
includeRoot: boolean,
includeLayoutMetrics: boolean,
};
interface Spec extends TurboModule {
startSurface: (surfaceId: number) => void;
stopSurface: (surfaceId: number) => void;
getMountingManagerLogs: (surfaceId: number) => Array<string>;
flushMessageQueue: () => void;
getRenderedOutput: (surfaceId: number, config: RenderFormatOptions) => string;
}
export default TurboModuleRegistry.getEnforcing<Spec>('Fantom') as Spec;