|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 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 | + * @providesModule ReactNativeViewConfigRegistry |
| 8 | + * @flow |
| 9 | + */ |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +import type { |
| 13 | + ReactNativeBaseComponentViewConfig, |
| 14 | + ViewConfigGetter, |
| 15 | +} from './ReactNativeTypes'; |
| 16 | + |
| 17 | +const invariant = require('fbjs/lib/invariant'); |
| 18 | + |
| 19 | +// Event configs |
| 20 | +const customBubblingEventTypes = {}; |
| 21 | +const customDirectEventTypes = {}; |
| 22 | +const eventTypes = {}; |
| 23 | + |
| 24 | +exports.customBubblingEventTypes = customBubblingEventTypes; |
| 25 | +exports.customDirectEventTypes = customDirectEventTypes; |
| 26 | +exports.eventTypes = eventTypes; |
| 27 | + |
| 28 | +const viewConfigCallbacks = new Map(); |
| 29 | +const viewConfigs = new Map(); |
| 30 | + |
| 31 | +function processEventTypes( |
| 32 | + viewConfig: ReactNativeBaseComponentViewConfig, |
| 33 | +): void { |
| 34 | + const {bubblingEventTypes, directEventTypes} = viewConfig; |
| 35 | + |
| 36 | + if (__DEV__) { |
| 37 | + if (bubblingEventTypes != null && directEventTypes != null) { |
| 38 | + for (const topLevelType in directEventTypes) { |
| 39 | + invariant( |
| 40 | + bubblingEventTypes[topLevelType] == null, |
| 41 | + 'Event cannot be both direct and bubbling: %s', |
| 42 | + topLevelType, |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (bubblingEventTypes != null) { |
| 49 | + for (const topLevelType in bubblingEventTypes) { |
| 50 | + if (customBubblingEventTypes[topLevelType] == null) { |
| 51 | + eventTypes[topLevelType] = customBubblingEventTypes[topLevelType] = |
| 52 | + bubblingEventTypes[topLevelType]; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (directEventTypes != null) { |
| 58 | + for (const topLevelType in directEventTypes) { |
| 59 | + if (customDirectEventTypes[topLevelType] == null) { |
| 60 | + eventTypes[topLevelType] = customDirectEventTypes[topLevelType] = |
| 61 | + directEventTypes[topLevelType]; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Registers a native view/component by name. |
| 69 | + * A callback is provided to load the view config from UIManager. |
| 70 | + * The callback is deferred until the view is actually rendered. |
| 71 | + * This is done to avoid causing Prepack deopts. |
| 72 | + */ |
| 73 | +exports.register = function(name: string, callback: ViewConfigGetter): string { |
| 74 | + invariant( |
| 75 | + !viewConfigCallbacks.has(name), |
| 76 | + 'Tried to register two views with the same name %s', |
| 77 | + name, |
| 78 | + ); |
| 79 | + viewConfigCallbacks.set(name, callback); |
| 80 | + return name; |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * Retrieves a config for the specified view. |
| 85 | + * If this is the first time the view has been used, |
| 86 | + * This configuration will be lazy-loaded from UIManager. |
| 87 | + */ |
| 88 | +exports.get = function(name: string): ReactNativeBaseComponentViewConfig { |
| 89 | + let viewConfig; |
| 90 | + if (!viewConfigs.has(name)) { |
| 91 | + const callback = viewConfigCallbacks.get(name); |
| 92 | + invariant( |
| 93 | + typeof callback === 'function', |
| 94 | + 'View config not found for name %s', |
| 95 | + name, |
| 96 | + ); |
| 97 | + viewConfigCallbacks.set(name, null); |
| 98 | + viewConfig = callback(); |
| 99 | + processEventTypes(viewConfig); |
| 100 | + viewConfigs.set(name, viewConfig); |
| 101 | + } else { |
| 102 | + viewConfig = viewConfigs.get(name); |
| 103 | + } |
| 104 | + invariant(viewConfig, 'View config not found for name %s', name); |
| 105 | + return viewConfig; |
| 106 | +}; |
0 commit comments