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

refactor: use ESM exports in ReactNativeViewConfigRegistry #27508

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,16 @@

'use strict';

import type {
ReactNativeBaseComponentViewConfig,
ViewConfigGetter,
} from './ReactNativeTypes';
import {type ViewConfig} from './ReactNativeTypes';

// Event configs
const customBubblingEventTypes = {};
const customDirectEventTypes = {};
const eventTypes = {};

exports.customBubblingEventTypes = customBubblingEventTypes;
exports.customDirectEventTypes = customDirectEventTypes;
exports.eventTypes = eventTypes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this also needs removed from here:

eventTypes: Object,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find, removed that in a separate PR

export const customBubblingEventTypes = {};
export const customDirectEventTypes = {};

const viewConfigCallbacks = new Map();
const viewConfigs = new Map();

function processEventTypes(
viewConfig: ReactNativeBaseComponentViewConfig<>,
): void {
function processEventTypes(viewConfig: ViewConfig): void {
const {bubblingEventTypes, directEventTypes} = viewConfig;

if (__DEV__) {
Expand All @@ -46,7 +36,7 @@ function processEventTypes(
if (bubblingEventTypes != null) {
for (const topLevelType in bubblingEventTypes) {
if (customBubblingEventTypes[topLevelType] == null) {
eventTypes[topLevelType] = customBubblingEventTypes[topLevelType] =
customBubblingEventTypes[topLevelType] =
bubblingEventTypes[topLevelType];
}
}
Expand All @@ -55,8 +45,7 @@ function processEventTypes(
if (directEventTypes != null) {
for (const topLevelType in directEventTypes) {
if (customDirectEventTypes[topLevelType] == null) {
eventTypes[topLevelType] = customDirectEventTypes[topLevelType] =
directEventTypes[topLevelType];
customDirectEventTypes[topLevelType] = directEventTypes[topLevelType];
}
}
}
Expand All @@ -66,9 +55,8 @@ function processEventTypes(
* Registers a native view/component by name.
* A callback is provided to load the view config from UIManager.
* The callback is deferred until the view is actually rendered.
* This is done to avoid causing Prepack deopts.
*/
exports.register = function (name: string, callback: ViewConfigGetter): string {
export function register(name: string, callback: () => ViewConfig): string {
if (viewConfigCallbacks.has(name)) {
throw new Error(`Tried to register two views with the same name ${name}`);
}
Expand All @@ -83,14 +71,14 @@ exports.register = function (name: string, callback: ViewConfigGetter): string {

viewConfigCallbacks.set(name, callback);
return name;
};
}

/**
* Retrieves a config for the specified view.
* If this is the first time the view has been used,
* This configuration will be lazy-loaded from UIManager.
*/
exports.get = function (name: string): ReactNativeBaseComponentViewConfig<> {
export function get(name: string): ViewConfig {
let viewConfig;
if (!viewConfigs.has(name)) {
const callback = viewConfigCallbacks.get(name);
Expand Down Expand Up @@ -121,4 +109,4 @@ exports.get = function (name: string): ReactNativeBaseComponentViewConfig<> {
}

return viewConfig;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {type ViewConfig} from './ReactNativeTypes';
import invariant from 'invariant';

// Event configs
const customBubblingEventTypes: {
export const customBubblingEventTypes: {
[eventName: string]: $ReadOnly<{
phasedRegistrationNames: $ReadOnly<{
captured: string,
Expand All @@ -24,16 +24,13 @@ const customBubblingEventTypes: {
}>,
...
} = {};
const customDirectEventTypes: {
export const customDirectEventTypes: {
[eventName: string]: $ReadOnly<{
registrationName: string,
}>,
...
} = {};

exports.customBubblingEventTypes = customBubblingEventTypes;
exports.customDirectEventTypes = customDirectEventTypes;

const viewConfigCallbacks = new Map<string, ?() => ViewConfig>();
const viewConfigs = new Map<string, ViewConfig>();

Expand Down Expand Up @@ -75,7 +72,7 @@ function processEventTypes(viewConfig: ViewConfig): void {
* A callback is provided to load the view config from UIManager.
* The callback is deferred until the view is actually rendered.
*/
exports.register = function (name: string, callback: () => ViewConfig): string {
export function register(name: string, callback: () => ViewConfig): string {
invariant(
!viewConfigCallbacks.has(name),
'Tried to register two views with the same name %s',
Expand All @@ -89,14 +86,14 @@ exports.register = function (name: string, callback: () => ViewConfig): string {
);
viewConfigCallbacks.set(name, callback);
return name;
};
}

/**
* Retrieves a config for the specified view.
* If this is the first time the view has been used,
* This configuration will be lazy-loaded from UIManager.
*/
exports.get = function (name: string): ViewConfig {
export function get(name: string): ViewConfig {
let viewConfig;
if (!viewConfigs.has(name)) {
const callback = viewConfigCallbacks.get(name);
Expand Down Expand Up @@ -124,4 +121,4 @@ exports.get = function (name: string): ViewConfig {
}
invariant(viewConfig, 'View config not found for name %s', name);
return viewConfig;
};
}