main
js 124 lines 3.6 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @noformat
8 * @nolint
9 * @flow strict-local
10 */
11
12 'use strict';
13
14 import {type ViewConfig} from './ReactNativeTypes';
15 import invariant from 'invariant';
16
17 // Event configs
18 export const customBubblingEventTypes: {
19 [eventName: string]: Readonly<{
20 phasedRegistrationNames: Readonly<{
21 captured: string,
22 bubbled: string,
23 skipBubbling?: ?boolean,
24 }>,
25 }>,
26 } = {};
27 export const customDirectEventTypes: {
28 [eventName: string]: Readonly<{
29 registrationName: string,
30 }>,
31 } = {};
32
33 const viewConfigCallbacks = new Map<string, ?() => ViewConfig>();
34 const viewConfigs = new Map<string, ViewConfig>();
35
36 function processEventTypes(viewConfig: ViewConfig): void {
37 const {bubblingEventTypes, directEventTypes} = viewConfig;
38
39 if (__DEV__) {
40 if (bubblingEventTypes != null && directEventTypes != null) {
41 for (const topLevelType in directEventTypes) {
42 invariant(
43 bubblingEventTypes[topLevelType] == null,
44 'Event cannot be both direct and bubbling: %s',
45 topLevelType,
46 );
47 }
48 }
49 }
50
51 if (bubblingEventTypes != null) {
52 for (const topLevelType in bubblingEventTypes) {
53 if (customBubblingEventTypes[topLevelType] == null) {
54 customBubblingEventTypes[topLevelType] =
55 bubblingEventTypes[topLevelType];
56 }
57 }
58 }
59
60 if (directEventTypes != null) {
61 for (const topLevelType in directEventTypes) {
62 if (customDirectEventTypes[topLevelType] == null) {
63 customDirectEventTypes[topLevelType] = directEventTypes[topLevelType];
64 }
65 }
66 }
67 }
68
69 /**
70 * Registers a native view/component by name.
71 * A callback is provided to load the view config from UIManager.
72 * The callback is deferred until the view is actually rendered.
73 */
74 export function register(name: string, callback: () => ViewConfig): string {
75 invariant(
76 !viewConfigCallbacks.has(name),
77 'Tried to register two views with the same name %s',
78 name,
79 );
80 invariant(
81 typeof callback === 'function',
82 'View config getter callback for component `%s` must be a function (received `%s`)',
83 name,
84 /* $FlowFixMe[invalid-compare] Error discovered during Constant Condition
85 * roll out. See https://fburl.com/workplace/5whu3i34. */
86 callback === null ? 'null' : typeof callback,
87 );
88 viewConfigCallbacks.set(name, callback);
89 return name;
90 }
91
92 /**
93 * Retrieves a config for the specified view.
94 * If this is the first time the view has been used,
95 * This configuration will be lazy-loaded from UIManager.
96 */
97 export function get(name: string): ViewConfig {
98 let viewConfig = viewConfigs.get(name);
99 if (viewConfig == null) {
100 const callback = viewConfigCallbacks.get(name);
101 if (typeof callback !== 'function') {
102 invariant(
103 false,
104 'View config getter callback for component `%s` must be a function (received `%s`).%s',
105 name,
106 callback === null ? 'null' : typeof callback,
107 // $FlowFixMe[recursive-definition]
108 typeof name[0] === 'string' && /[a-z]/.test(name[0])
109 ? ' Make sure to start component names with a capital letter.'
110 : '',
111 );
112 }
113 viewConfig = callback();
114 invariant(viewConfig, 'View config not found for component `%s`', name);
115
116 processEventTypes(viewConfig);
117 viewConfigs.set(name, viewConfig);
118
119 // Clear the callback after the config is set so that
120 // we don't mask any errors during registration.
121 viewConfigCallbacks.set(name, null);
122 }
123 return viewConfig;
124 }