forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventPluginRegistry.js
265 lines (239 loc) · 7.42 KB
/
EventPluginRegistry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {DispatchConfig} from './ReactSyntheticEventType';
import type {
AnyNativeEvent,
PluginName,
LegacyPluginModule,
} from './PluginModuleType';
import type {TopLevelType} from './TopLevelEventTypes';
type NamesToPlugins = {
[key: PluginName]: LegacyPluginModule<AnyNativeEvent>,
};
type EventPluginOrder = null | Array<PluginName>;
/**
* Injectable ordering of event plugins.
*/
let eventPluginOrder: EventPluginOrder = null;
/**
* Injectable mapping from names to event plugin modules.
*/
const namesToPlugins: NamesToPlugins = {};
/**
* Recomputes the plugin list using the injected plugins and plugin ordering.
*
* @private
*/
function recomputePluginOrdering(): void {
if (!eventPluginOrder) {
// Wait until an `eventPluginOrder` is injected.
return;
}
for (const pluginName in namesToPlugins) {
const pluginModule = namesToPlugins[pluginName];
// $FlowFixMe[incompatible-use] found when upgrading Flow
const pluginIndex = eventPluginOrder.indexOf(pluginName);
if (pluginIndex <= -1) {
throw new Error(
'EventPluginRegistry: Cannot inject event plugins that do not exist in ' +
`the plugin ordering, \`${pluginName}\`.`,
);
}
if (plugins[pluginIndex]) {
continue;
}
if (!pluginModule.extractEvents) {
throw new Error(
'EventPluginRegistry: Event plugins must implement an `extractEvents` ' +
`method, but \`${pluginName}\` does not.`,
);
}
plugins[pluginIndex] = pluginModule;
const publishedEvents = pluginModule.eventTypes;
for (const eventName in publishedEvents) {
if (
!publishEventForPlugin(
publishedEvents[eventName],
pluginModule,
eventName,
)
) {
throw new Error(
`EventPluginRegistry: Failed to publish event \`${eventName}\` for plugin \`${pluginName}\`.`,
);
}
}
}
}
/**
* Publishes an event so that it can be dispatched by the supplied plugin.
*
* @param {object} dispatchConfig Dispatch configuration for the event.
* @param {object} PluginModule Plugin publishing the event.
* @return {boolean} True if the event was successfully published.
* @private
*/
function publishEventForPlugin(
dispatchConfig: DispatchConfig,
pluginModule: LegacyPluginModule<AnyNativeEvent>,
eventName: string,
): boolean {
if (eventNameDispatchConfigs.hasOwnProperty(eventName)) {
throw new Error(
'EventPluginRegistry: More than one plugin attempted to publish the same ' +
`event name, \`${eventName}\`.`,
);
}
eventNameDispatchConfigs[eventName] = dispatchConfig;
const phasedRegistrationNames = dispatchConfig.phasedRegistrationNames;
if (phasedRegistrationNames) {
for (const phaseName in phasedRegistrationNames) {
if (phasedRegistrationNames.hasOwnProperty(phaseName)) {
// $FlowFixMe[invalid-computed-prop]
const phasedRegistrationName = phasedRegistrationNames[phaseName];
publishRegistrationName(
phasedRegistrationName,
pluginModule,
eventName,
);
}
}
return true;
} else if (dispatchConfig.registrationName) {
publishRegistrationName(
dispatchConfig.registrationName,
pluginModule,
eventName,
);
return true;
}
return false;
}
/**
* Publishes a registration name that is used to identify dispatched events.
*
* @param {string} registrationName Registration name to add.
* @param {object} PluginModule Plugin publishing the event.
* @private
*/
function publishRegistrationName(
registrationName: string,
pluginModule: LegacyPluginModule<AnyNativeEvent>,
eventName: string,
): void {
if (registrationNameModules[registrationName]) {
throw new Error(
'EventPluginRegistry: More than one plugin attempted to publish the same ' +
`registration name, \`${registrationName}\`.`,
);
}
registrationNameModules[registrationName] = pluginModule;
registrationNameDependencies[registrationName] =
pluginModule.eventTypes[eventName].dependencies;
if (__DEV__) {
const lowerCasedName = registrationName.toLowerCase();
possibleRegistrationNames[lowerCasedName] = registrationName;
if (registrationName === 'onDoubleClick') {
possibleRegistrationNames.ondblclick = registrationName;
}
}
}
/**
* Registers plugins so that they can extract and dispatch events.
*/
/**
* Ordered list of injected plugins.
*/
export const plugins: Array<LegacyPluginModule<AnyNativeEvent>> = [];
/**
* Mapping from event name to dispatch config
*/
export const eventNameDispatchConfigs: {
[eventName: string]: DispatchConfig,
} = {};
/**
* Mapping from registration name to plugin module
*/
export const registrationNameModules: {
[registrationName: string]: LegacyPluginModule<AnyNativeEvent>,
} = {};
/**
* Mapping from registration name to event name
*/
export const registrationNameDependencies: {
[registrationName: string]: Array<TopLevelType> | void,
} = {};
/**
* Mapping from lowercase registration names to the properly cased version,
* used to warn in the case of missing event handlers. Available
* only in __DEV__.
* @type {Object}
*/
export const possibleRegistrationNames: {
[lowerCasedName: string]: string,
} = __DEV__ ? {} : (null: any);
// Trust the developer to only use possibleRegistrationNames in __DEV__
/**
* Injects an ordering of plugins (by plugin name). This allows the ordering
* to be decoupled from injection of the actual plugins so that ordering is
* always deterministic regardless of packaging, on-the-fly injection, etc.
*
* @param {array} InjectedEventPluginOrder
* @internal
*/
export function injectEventPluginOrder(
injectedEventPluginOrder: EventPluginOrder,
): void {
if (eventPluginOrder) {
throw new Error(
'EventPluginRegistry: Cannot inject event plugin ordering more than ' +
'once. You are likely trying to load more than one copy of React.',
);
}
// Clone the ordering so it cannot be dynamically mutated.
// $FlowFixMe[method-unbinding] found when upgrading Flow
eventPluginOrder = Array.prototype.slice.call(injectedEventPluginOrder);
recomputePluginOrdering();
}
/**
* Injects plugins to be used by plugin event system. The plugin names must be
* in the ordering injected by `injectEventPluginOrder`.
*
* Plugins can be injected as part of page initialization or on-the-fly.
*
* @param {object} injectedNamesToPlugins Map from names to plugin modules.
* @internal
*/
export function injectEventPluginsByName(
injectedNamesToPlugins: NamesToPlugins,
): void {
let isOrderingDirty = false;
for (const pluginName in injectedNamesToPlugins) {
if (!injectedNamesToPlugins.hasOwnProperty(pluginName)) {
continue;
}
const pluginModule = injectedNamesToPlugins[pluginName];
if (
!namesToPlugins.hasOwnProperty(pluginName) ||
namesToPlugins[pluginName] !== pluginModule
) {
if (namesToPlugins[pluginName]) {
throw new Error(
'EventPluginRegistry: Cannot inject two different event plugins ' +
`using the same name, \`${pluginName}\`.`,
);
}
namesToPlugins[pluginName] = pluginModule;
isOrderingDirty = true;
}
}
if (isOrderingDirty) {
recomputePluginOrdering();
}
}