7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule EventPluginRegistry
10
+ * @flow
11
*/
12
13
'use strict';
14
15
+import type {
16
+ DispatchConfig,
17
+ ReactSyntheticEvent,
18
+} from 'ReactSyntheticEvent';
19
+
20
+type PluginName = string;
21
+
22
+type PluginModule = {
23
+ eventTypes: any,
24
+};
25
+
26
+type NamesToPlugins = {[key: PluginName]: PluginModule};
27
+
28
+type EventPluginOrder = null | Array<PluginName>;
29
+
30
var invariant = require('invariant');
31
32
/**
33
* Injectable ordering of event plugins.
34
*/
19
-var EventPluginOrder = null;
35
+var eventPluginOrder: EventPluginOrder = null;
36
37
/**
38
* Injectable mapping from names to event plugin modules.
39
*/
24
-var namesToPlugins = {};
40
+var namesToPlugins: NamesToPlugins = {};
41
42
/**
43
* Recomputes the plugin list using the injected plugins and plugin ordering.
44
*
45
* @private
46
*/
31
-function recomputePluginOrdering() {
32
- if (!EventPluginOrder) {
33
- // Wait until an `EventPluginOrder` is injected.
47
+function recomputePluginOrdering(): void {
48
+ if (!eventPluginOrder) {
49
+ // Wait until an `eventPluginOrder` is injected.
50
return;
51
}
52
for (var pluginName in namesToPlugins) {
37
- var PluginModule = namesToPlugins[pluginName];
38
- var pluginIndex = EventPluginOrder.indexOf(pluginName);
53
+ var pluginModule = namesToPlugins[pluginName];
54
+ var pluginIndex = eventPluginOrder.indexOf(pluginName);
55
invariant(
56
pluginIndex > -1,
57
'EventPluginRegistry: Cannot inject event plugins that do not exist in ' +
58
'the plugin ordering, `%s`.',
43
- pluginName
59
+ pluginName,
60
);
61
if (EventPluginRegistry.plugins[pluginIndex]) {
62
continue;
63
}
64
invariant(
49
- PluginModule.extractEvents,
65
+ pluginModule.extractEvents,
66
'EventPluginRegistry: Event plugins must implement an `extractEvents` ' +
67
'method, but `%s` does not.',
52
- pluginName
68
+ pluginName,
69
);
54
- EventPluginRegistry.plugins[pluginIndex] = PluginModule;
55
- var publishedEvents = PluginModule.eventTypes;
70
+ EventPluginRegistry.plugins[pluginIndex] = pluginModule;
71
+ var publishedEvents = pluginModule.eventTypes;
72
for (var eventName in publishedEvents) {
73
invariant(
74
publishEventForPlugin(
75
publishedEvents[eventName],
60
- PluginModule,
61
- eventName
76
+ pluginModule,
77
+ eventName,
78
),
79
'EventPluginRegistry: Failed to publish event `%s` for plugin `%s`.',
80
eventName,
65
- pluginName
81
+ pluginName,
82
);
83
}
84
}
92
* @return {boolean} True if the event was successfully published.
93
* @private
94
*/
79
-function publishEventForPlugin(dispatchConfig, PluginModule, eventName) {
95
+function publishEventForPlugin(
96
+ dispatchConfig: DispatchConfig,
97
+ pluginModule: PluginModule,
98
+ eventName: string,
99
+): boolean {
100
invariant(
101
!EventPluginRegistry.eventNameDispatchConfigs.hasOwnProperty(eventName),
102
'EventPluginHub: More than one plugin attempted to publish the same ' +
103
'event name, `%s`.',
84
- eventName
104
+ eventName,
105
);
106
EventPluginRegistry.eventNameDispatchConfigs[eventName] = dispatchConfig;
107
112
var phasedRegistrationName = phasedRegistrationNames[phaseName];
113
publishRegistrationName(
114
phasedRegistrationName,
95
- PluginModule,
96
- eventName
115
+ pluginModule,
116
+ eventName,
117
);
118
}
119
}
121
} else if (dispatchConfig.registrationName) {
122
publishRegistrationName(
123
dispatchConfig.registrationName,
104
- PluginModule,
105
- eventName
124
+ pluginModule,
125
+ eventName,
126
);
127
return true;
128
}
137
* @param {object} PluginModule Plugin publishing the event.
138
* @private
139
*/
120
-function publishRegistrationName(registrationName, PluginModule, eventName) {
140
+function publishRegistrationName(
141
+ registrationName: string,
142
+ pluginModule: PluginModule,
143
+ eventName: string,
144
+): void {
145
invariant(
146
!EventPluginRegistry.registrationNameModules[registrationName],
147
'EventPluginHub: More than one plugin attempted to publish the same ' +
148
'registration name, `%s`.',
125
- registrationName
149
+ registrationName,
150
);
127
- EventPluginRegistry.registrationNameModules[registrationName] = PluginModule;
151
+ EventPluginRegistry.registrationNameModules[registrationName] = pluginModule;
152
EventPluginRegistry.registrationNameDependencies[registrationName] =
129
- PluginModule.eventTypes[eventName].dependencies;
153
+ pluginModule.eventTypes[eventName].dependencies;
154
155
if (__DEV__) {
156
var lowerCasedName = registrationName.toLowerCase();
197
* only in __DEV__.
198
* @type {Object}
199
*/
176
- possibleRegistrationNames: __DEV__ ? {} : null,
200
+ possibleRegistrationNames: __DEV__ ? {} : (null: any),
201
+ // Trust the developer to only use possibleRegistrationNames in __DEV__
202
203
/**
204
* Injects an ordering of plugins (by plugin name). This allows the ordering
209
* @internal
210
* @see {EventPluginHub.injection.injectEventPluginOrder}
211
*/
187
- injectEventPluginOrder: function(InjectedEventPluginOrder) {
212
+ injectEventPluginOrder: function(
213
+ injectedEventPluginOrder: EventPluginOrder,
214
+ ): void {
215
invariant(
189
- !EventPluginOrder,
216
+ !eventPluginOrder,
217
'EventPluginRegistry: Cannot inject event plugin ordering more than ' +
191
- 'once. You are likely trying to load more than one copy of React.'
218
+ 'once. You are likely trying to load more than one copy of React.',
219
);
220
// Clone the ordering so it cannot be dynamically mutated.
194
- EventPluginOrder = Array.prototype.slice.call(InjectedEventPluginOrder);
221
+ eventPluginOrder = Array.prototype.slice.call(injectedEventPluginOrder);
222
recomputePluginOrdering();
223
},
224
232
* @internal
233
* @see {EventPluginHub.injection.injectEventPluginsByName}
234
*/
208
- injectEventPluginsByName: function(injectedNamesToPlugins) {
235
+ injectEventPluginsByName: function(
236
+ injectedNamesToPlugins: NamesToPlugins
237
+ ): void {
238
var isOrderingDirty = false;
239
for (var pluginName in injectedNamesToPlugins) {
240
if (!injectedNamesToPlugins.hasOwnProperty(pluginName)) {
241
continue;
242
}
214
- var PluginModule = injectedNamesToPlugins[pluginName];
243
+ var pluginModule = injectedNamesToPlugins[pluginName];
244
if (!namesToPlugins.hasOwnProperty(pluginName) ||
216
- namesToPlugins[pluginName] !== PluginModule) {
245
+ namesToPlugins[pluginName] !== pluginModule) {
246
invariant(
247
!namesToPlugins[pluginName],
248
'EventPluginRegistry: Cannot inject two different event plugins ' +
249
'using the same name, `%s`.',
221
- pluginName
250
+ pluginName,
251
);
223
- namesToPlugins[pluginName] = PluginModule;
252
+ namesToPlugins[pluginName] = pluginModule;
253
isOrderingDirty = true;
254
}
255
}
265
* @return {?object} The plugin that created the supplied event.
266
* @internal
267
*/
239
- getPluginModuleForEvent: function(event) {
268
+ getPluginModuleForEvent: function(
269
+ event: ReactSyntheticEvent,
270
+ ): null | PluginModule {
271
var dispatchConfig = event.dispatchConfig;
272
if (dispatchConfig.registrationName) {
273
return EventPluginRegistry.registrationNameModules[
278
if (!dispatchConfig.phasedRegistrationNames.hasOwnProperty(phase)) {
279
continue;
280
}
250
- var PluginModule = EventPluginRegistry.registrationNameModules[
281
+ var pluginModule = EventPluginRegistry.registrationNameModules[
282
dispatchConfig.phasedRegistrationNames[phase]
283
];
253
- if (PluginModule) {
254
- return PluginModule;
284
+ if (pluginModule) {
285
+ return pluginModule;
286
}
287
}
288
return null;
292
* Exposed for unit testing.
293
* @private
294
*/
264
- _resetEventPlugins: function() {
265
- EventPluginOrder = null;
295
+ _resetEventPlugins: function(): void {
296
+ eventPluginOrder = null;
297
for (var pluginName in namesToPlugins) {
298
if (namesToPlugins.hasOwnProperty(pluginName)) {
299
delete namesToPlugins[pluginName];