2016-12-08 10:15:13 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-12-08 10:15:13 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-12-08 10:15:13 +08:00
|
|
|
*
|
2018-05-15 08:47:47 +08:00
|
|
|
* @format
|
2018-09-07 03:27:44 +08:00
|
|
|
* @flow strict-local
|
2016-12-08 10:15:13 +08:00
|
|
|
*/
|
2018-05-15 08:47:47 +08:00
|
|
|
|
2018-04-10 11:05:57 +08:00
|
|
|
'use strict';
|
2016-12-08 10:15:13 +08:00
|
|
|
|
2021-02-26 13:00:32 +08:00
|
|
|
import {type ViewConfig} from './ReactNativeTypes';
|
|
|
|
|
import invariant from 'invariant';
|
2017-10-26 02:07:54 +08:00
|
|
|
|
2018-04-10 10:42:23 +08:00
|
|
|
// Event configs
|
2019-10-19 01:05:09 +08:00
|
|
|
const customBubblingEventTypes: {
|
2022-09-10 04:03:48 +08:00
|
|
|
[eventName: string]: $ReadOnly<{
|
|
|
|
|
phasedRegistrationNames: $ReadOnly<{
|
2019-10-19 01:05:09 +08:00
|
|
|
captured: string,
|
|
|
|
|
bubbled: string,
|
2022-03-15 01:59:21 +08:00
|
|
|
skipBubbling?: ?boolean,
|
2022-09-10 04:03:48 +08:00
|
|
|
}>,
|
|
|
|
|
}>,
|
2023-01-31 21:25:05 +08:00
|
|
|
...
|
2019-10-19 01:05:09 +08:00
|
|
|
} = {};
|
|
|
|
|
const customDirectEventTypes: {
|
2022-09-10 04:03:48 +08:00
|
|
|
[eventName: string]: $ReadOnly<{
|
2019-10-19 01:05:09 +08:00
|
|
|
registrationName: string,
|
2022-09-10 04:03:48 +08:00
|
|
|
}>,
|
2023-01-31 21:25:05 +08:00
|
|
|
...
|
2019-10-19 01:05:09 +08:00
|
|
|
} = {};
|
2018-04-10 11:05:57 +08:00
|
|
|
|
|
|
|
|
exports.customBubblingEventTypes = customBubblingEventTypes;
|
|
|
|
|
exports.customDirectEventTypes = customDirectEventTypes;
|
2018-04-10 10:42:23 +08:00
|
|
|
|
2023-01-28 00:55:38 +08:00
|
|
|
const viewConfigCallbacks = new Map<string, ?() => ViewConfig>();
|
|
|
|
|
const viewConfigs = new Map<string, ViewConfig>();
|
2016-12-08 10:15:13 +08:00
|
|
|
|
2021-02-26 13:00:32 +08:00
|
|
|
function processEventTypes(viewConfig: ViewConfig): void {
|
2018-04-10 10:42:23 +08:00
|
|
|
const {bubblingEventTypes, directEventTypes} = viewConfig;
|
|
|
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
if (bubblingEventTypes != null && directEventTypes != null) {
|
|
|
|
|
for (const topLevelType in directEventTypes) {
|
|
|
|
|
invariant(
|
|
|
|
|
bubblingEventTypes[topLevelType] == null,
|
|
|
|
|
'Event cannot be both direct and bubbling: %s',
|
|
|
|
|
topLevelType,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bubblingEventTypes != null) {
|
|
|
|
|
for (const topLevelType in bubblingEventTypes) {
|
|
|
|
|
if (customBubblingEventTypes[topLevelType] == null) {
|
2019-06-05 20:21:13 +08:00
|
|
|
customBubblingEventTypes[topLevelType] =
|
2018-04-10 10:42:23 +08:00
|
|
|
bubblingEventTypes[topLevelType];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (directEventTypes != null) {
|
|
|
|
|
for (const topLevelType in directEventTypes) {
|
|
|
|
|
if (customDirectEventTypes[topLevelType] == null) {
|
2019-06-05 20:21:13 +08:00
|
|
|
customDirectEventTypes[topLevelType] = directEventTypes[topLevelType];
|
2018-04-10 10:42:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
/**
|
2017-11-08 02:09:33 +08:00
|
|
|
* 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.
|
|
|
|
|
*/
|
2023-01-31 21:25:05 +08:00
|
|
|
exports.register = function (name: string, callback: () => ViewConfig): string {
|
2017-11-03 03:50:03 +08:00
|
|
|
invariant(
|
|
|
|
|
!viewConfigCallbacks.has(name),
|
|
|
|
|
'Tried to register two views with the same name %s',
|
|
|
|
|
name,
|
|
|
|
|
);
|
2019-09-25 08:46:21 +08:00
|
|
|
invariant(
|
|
|
|
|
typeof callback === 'function',
|
|
|
|
|
'View config getter callback for component `%s` must be a function (received `%s`)',
|
|
|
|
|
name,
|
|
|
|
|
callback === null ? 'null' : typeof callback,
|
|
|
|
|
);
|
2017-11-03 03:50:03 +08:00
|
|
|
viewConfigCallbacks.set(name, callback);
|
|
|
|
|
return name;
|
2018-04-10 11:05:57 +08:00
|
|
|
};
|
2017-09-01 04:42:27 +08:00
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
/**
|
2017-11-08 02:09:33 +08:00
|
|
|
* 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.
|
|
|
|
|
*/
|
2023-01-31 21:25:05 +08:00
|
|
|
exports.get = function (name: string): ViewConfig {
|
2017-11-03 03:50:03 +08:00
|
|
|
let viewConfig;
|
|
|
|
|
if (!viewConfigs.has(name)) {
|
|
|
|
|
const callback = viewConfigCallbacks.get(name);
|
2018-08-16 21:54:45 +08:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
|
invariant(
|
|
|
|
|
false,
|
2019-09-25 08:46:21 +08:00
|
|
|
'View config getter callback for component `%s` must be a function (received `%s`).%s',
|
2018-08-16 21:54:45 +08:00
|
|
|
name,
|
2019-09-25 08:46:21 +08:00
|
|
|
callback === null ? 'null' : typeof callback,
|
2018-08-16 21:54:45 +08:00
|
|
|
typeof name[0] === 'string' && /[a-z]/.test(name[0])
|
|
|
|
|
? ' Make sure to start component names with a capital letter.'
|
|
|
|
|
: '',
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-11-03 03:50:03 +08:00
|
|
|
viewConfig = callback();
|
2018-04-10 10:42:23 +08:00
|
|
|
processEventTypes(viewConfig);
|
2017-11-03 03:50:03 +08:00
|
|
|
viewConfigs.set(name, viewConfig);
|
2019-09-18 22:31:00 +08:00
|
|
|
|
|
|
|
|
// Clear the callback after the config is set so that
|
|
|
|
|
// we don't mask any errors during registration.
|
|
|
|
|
viewConfigCallbacks.set(name, null);
|
2017-11-03 03:50:03 +08:00
|
|
|
} else {
|
|
|
|
|
viewConfig = viewConfigs.get(name);
|
|
|
|
|
}
|
|
|
|
|
invariant(viewConfig, 'View config not found for name %s', name);
|
|
|
|
|
return viewConfig;
|
2018-04-10 11:05:57 +08:00
|
|
|
};
|