|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its 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 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {DispatchConfig} from './ReactSyntheticEventType'; |
| 11 | +import type { |
| 12 | + AnyNativeEvent, |
| 13 | + LegacyPluginModule, |
| 14 | + ModernPluginModule, |
| 15 | +} from './PluginModuleType'; |
| 16 | +import ModernBeforeInputEventPlugin from '../events/plugins/ModernBeforeInputEventPlugin'; |
| 17 | +import ModernChangeEventPlugin from '../events/plugins/ModernChangeEventPlugin'; |
| 18 | +import ModernEnterLeaveEventPlugin from '../events/plugins/ModernEnterLeaveEventPlugin'; |
| 19 | +import ModernSelectEventPlugin from '../events/plugins/ModernSelectEventPlugin'; |
| 20 | +import ModernSimpleEventPlugin from '../events/plugins/ModernSimpleEventPlugin'; |
| 21 | + |
| 22 | +import invariant from 'shared/invariant'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Publishes an event so that it can be dispatched by the supplied plugin. |
| 26 | + * |
| 27 | + * @param {object} dispatchConfig Dispatch configuration for the event. |
| 28 | + * @param {object} PluginModule Plugin publishing the event. |
| 29 | + * @return {boolean} True if the event was successfully published. |
| 30 | + * @private |
| 31 | + */ |
| 32 | +function publishEventForPlugin( |
| 33 | + dispatchConfig: DispatchConfig, |
| 34 | + pluginModule: |
| 35 | + | LegacyPluginModule<AnyNativeEvent> |
| 36 | + | ModernPluginModule<AnyNativeEvent>, |
| 37 | + eventName: string, |
| 38 | +): boolean { |
| 39 | + invariant( |
| 40 | + !eventNameDispatchConfigs.hasOwnProperty(eventName), |
| 41 | + 'EventPluginRegistry: More than one plugin attempted to publish the same ' + |
| 42 | + 'event name, `%s`.', |
| 43 | + eventName, |
| 44 | + ); |
| 45 | + eventNameDispatchConfigs[eventName] = dispatchConfig; |
| 46 | + |
| 47 | + const phasedRegistrationNames = dispatchConfig.phasedRegistrationNames; |
| 48 | + if (phasedRegistrationNames) { |
| 49 | + for (const phaseName in phasedRegistrationNames) { |
| 50 | + if (phasedRegistrationNames.hasOwnProperty(phaseName)) { |
| 51 | + const phasedRegistrationName = phasedRegistrationNames[phaseName]; |
| 52 | + publishRegistrationName( |
| 53 | + phasedRegistrationName, |
| 54 | + pluginModule, |
| 55 | + eventName, |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } else if (dispatchConfig.registrationName) { |
| 61 | + publishRegistrationName( |
| 62 | + dispatchConfig.registrationName, |
| 63 | + pluginModule, |
| 64 | + eventName, |
| 65 | + ); |
| 66 | + return true; |
| 67 | + } |
| 68 | + return false; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Publishes a registration name that is used to identify dispatched events. |
| 73 | + * |
| 74 | + * @param {string} registrationName Registration name to add. |
| 75 | + * @param {object} PluginModule Plugin publishing the event. |
| 76 | + * @private |
| 77 | + */ |
| 78 | +function publishRegistrationName( |
| 79 | + registrationName: string, |
| 80 | + pluginModule: |
| 81 | + | LegacyPluginModule<AnyNativeEvent> |
| 82 | + | ModernPluginModule<AnyNativeEvent>, |
| 83 | + eventName: string, |
| 84 | +): void { |
| 85 | + invariant( |
| 86 | + !registrationNameModules[registrationName], |
| 87 | + 'EventPluginRegistry: More than one plugin attempted to publish the same ' + |
| 88 | + 'registration name, `%s`.', |
| 89 | + registrationName, |
| 90 | + ); |
| 91 | + registrationNameModules[registrationName] = pluginModule; |
| 92 | + registrationNameDependencies[registrationName] = |
| 93 | + pluginModule.eventTypes[eventName].dependencies; |
| 94 | + |
| 95 | + if (__DEV__) { |
| 96 | + const lowerCasedName = registrationName.toLowerCase(); |
| 97 | + possibleRegistrationNames[lowerCasedName] = registrationName; |
| 98 | + |
| 99 | + if (registrationName === 'onDoubleClick') { |
| 100 | + possibleRegistrationNames.ondblclick = registrationName; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Registers plugins so that they can extract and dispatch events. |
| 107 | + */ |
| 108 | + |
| 109 | +/** |
| 110 | + * Ordered list of injected plugins. |
| 111 | + */ |
| 112 | +export const plugins = []; |
| 113 | + |
| 114 | +/** |
| 115 | + * Mapping from event name to dispatch config |
| 116 | + */ |
| 117 | +export const eventNameDispatchConfigs = {}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Mapping from registration name to plugin module |
| 121 | + */ |
| 122 | +export const registrationNameModules = {}; |
| 123 | + |
| 124 | +/** |
| 125 | + * Mapping from registration name to event name |
| 126 | + */ |
| 127 | +export const registrationNameDependencies = {}; |
| 128 | + |
| 129 | +/** |
| 130 | + * Mapping from lowercase registration names to the properly cased version, |
| 131 | + * used to warn in the case of missing event handlers. Available |
| 132 | + * only in __DEV__. |
| 133 | + * @type {Object} |
| 134 | + */ |
| 135 | +export const possibleRegistrationNames = __DEV__ ? {} : (null: any); |
| 136 | +// Trust the developer to only use possibleRegistrationNames in __DEV__ |
| 137 | + |
| 138 | +function injectEventPlugin(pluginModule: ModernPluginModule<any>): void { |
| 139 | + plugins.push(pluginModule); |
| 140 | + const publishedEvents = pluginModule.eventTypes; |
| 141 | + for (const eventName in publishedEvents) { |
| 142 | + publishEventForPlugin(publishedEvents[eventName], pluginModule, eventName); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// TODO: remove top-level side effect. |
| 147 | +injectEventPlugin(ModernSimpleEventPlugin); |
| 148 | +injectEventPlugin(ModernEnterLeaveEventPlugin); |
| 149 | +injectEventPlugin(ModernChangeEventPlugin); |
| 150 | +injectEventPlugin(ModernSelectEventPlugin); |
| 151 | +injectEventPlugin(ModernBeforeInputEventPlugin); |
0 commit comments