Simplify event dispatching
This simplifies event dispatching by removing the `return false` special case for the SimpleEventPlugin which allow us to inline much more here and the code becomes easier to follow.
Sebastian Markbage committed
Jun 19, 2015 at 21:53 UTC
216026418cf47787f5393e4db734281ae9b6c5c8
4 files changed
+23
-70
src/renderers/dom/client/__tests__/ReactBrowserEventEmitter-test.js
+5
-8
@@ -264,7 +264,7 @@ describe('ReactBrowserEventEmitter', function() {
264
expect(idCallOrder[0]).toBe(getID(CHILD));
265
});
266
267
- it('should stopPropagation if false is returned, but warn', function() {
267
+ it('should not stopPropagation if false is returned', function() {
268
ReactBrowserEventEmitter.putListener(
269
getID(CHILD),
270
ON_CLICK_KEY,
@@ -282,14 +282,11 @@ describe('ReactBrowserEventEmitter', function() {
282
);
283
spyOn(console, 'error');
284
ReactTestUtils.Simulate.click(CHILD);
285
- expect(idCallOrder.length).toBe(1);
285
+ expect(idCallOrder.length).toBe(3);
286
expect(idCallOrder[0]).toBe(getID(CHILD));
287
- expect(console.error.calls.length).toEqual(1);
288
- expect(console.error.calls[0].args[0]).toBe(
289
- 'Warning: Returning `false` from an event handler is deprecated and ' +
290
- 'will be ignored in a future release. Instead, manually call ' +
291
- 'e.stopPropagation() or e.preventDefault(), as appropriate.'
292
- );
287
+ expect(idCallOrder[1]).toBe(getID(PARENT));
288
+ expect(idCallOrder[2]).toBe(getID(GRANDPARENT));
289
+ expect(console.error.calls.length).toEqual(0);
290
});
291
292
/**
src/renderers/dom/client/eventPlugins/SimpleEventPlugin.js
-26
@@ -13,7 +13,6 @@
13
14
var EventConstants = require('EventConstants');
15
var EventListener = require('EventListener');
16
-var EventPluginUtils = require('EventPluginUtils');
16
var EventPropagators = require('EventPropagators');
17
var ReactMount = require('ReactMount');
18
var SyntheticClipboardEvent = require('SyntheticClipboardEvent');
@@ -30,7 +29,6 @@ var emptyFunction = require('emptyFunction');
29
var getEventCharCode = require('getEventCharCode');
30
var invariant = require('invariant');
31
var keyOf = require('keyOf');
33
-var warning = require('warning');
32
33
var topLevelTypes = EventConstants.topLevelTypes;
34
@@ -452,30 +450,6 @@ var SimpleEventPlugin = {
450
451
eventTypes: eventTypes,
452
455
- /**
456
- * Same as the default implementation, except cancels the event when return
457
- * value is false. This behavior will be disabled in a future release.
458
- *
459
- * @param {object} event Event to be dispatched.
460
- * @param {function} listener Application-level callback.
461
- * @param {string} domID DOM ID to pass to the callback.
462
- */
463
- executeDispatch: function(event, listener, domID) {
464
- var returnValue = EventPluginUtils.executeDispatch(event, listener, domID);
465
-
466
- warning(
467
- typeof returnValue !== 'boolean',
468
- 'Returning `false` from an event handler is deprecated and will be ' +
469
- 'ignored in a future release. Instead, manually call ' +
470
- 'e.stopPropagation() or e.preventDefault(), as appropriate.'
471
- );
472
-
473
- if (returnValue === false) {
474
- event.stopPropagation();
475
- event.preventDefault();
476
- }
477
- },
478
-
453
/**
454
* @param {string} topLevelType Record from `EventConstants`.
455
* @param {DOMEventTarget} topLevelTarget The listening component root node.
src/renderers/shared/event/EventPluginHub.js
+1
-7
@@ -39,13 +39,7 @@ var eventQueue = null;
39
*/
40
var executeDispatchesAndRelease = function(event) {
41
if (event) {
42
- var executeDispatch = EventPluginUtils.executeDispatch;
43
- // Plugins can provide custom behavior when dispatching events.
44
- var PluginModule = EventPluginRegistry.getPluginModuleForEvent(event);
45
- if (PluginModule && PluginModule.executeDispatch) {
46
- executeDispatch = PluginModule.executeDispatch;
47
- }
48
- EventPluginUtils.executeDispatchesInOrder(event, executeDispatch);
42
+ EventPluginUtils.executeDispatchesInOrder(event);
43
44
if (!event.isPersistent()) {
45
event.constructor.release(event);
src/renderers/shared/event/EventPluginUtils.js
+17
-29
@@ -78,11 +78,22 @@ if (__DEV__) {
78
}
79
80
/**
81
- * Invokes `cb(event, listener, id)`. Avoids using call if no scope is
82
- * provided. The `(listener,id)` pair effectively forms the "dispatch" but are
83
- * kept separate to conserve memory.
81
+ * Dispatch the event to the listener.
82
+ * @param {SyntheticEvent} event SyntheticEvent to handle
83
+ * @param {function} listener Application-level callback
84
+ * @param {string} domID DOM id to pass to the callback.
85
+ */
86
+function executeDispatch(event, listener, domID) {
87
+ var type = event.type || 'unknown-event';
88
+ event.currentTarget = injection.Mount.getNode(domID);
89
+ ReactErrorUtils.invokeGuardedCallback(type, listener, event, domID);
90
+ event.currentTarget = null;
91
+}
92
+
93
+/**
94
+ * Standard/simple iteration through an event's collected dispatches.
95
*/
85
-function forEachEventDispatch(event, cb) {
96
+function executeDispatchesInOrder(event) {
97
var dispatchListeners = event._dispatchListeners;
98
var dispatchIDs = event._dispatchIDs;
99
if (__DEV__) {
@@ -94,33 +105,11 @@ function forEachEventDispatch(event, cb) {
105
break;
106
}
107
// Listeners and IDs are two parallel arrays that are always in sync.
97
- cb(event, dispatchListeners[i], dispatchIDs[i]);
108
+ executeDispatch(event, dispatchListeners[i], dispatchIDs[i]);
109
}
110
} else if (dispatchListeners) {
100
- cb(event, dispatchListeners, dispatchIDs);
111
+ executeDispatch(event, dispatchListeners, dispatchIDs);
112
}
102
-}
103
-
104
-/**
105
- * Default implementation of PluginModule.executeDispatch().
106
- * @param {SyntheticEvent} event SyntheticEvent to handle
107
- * @param {function} listener Application-level callback
108
- * @param {string} domID DOM id to pass to the callback.
109
- */
110
-function executeDispatch(event, listener, domID) {
111
- event.currentTarget = injection.Mount.getNode(domID);
112
- var type = event.type || 'unknown-event';
113
- var returnValue =
114
- ReactErrorUtils.invokeGuardedCallback(type, listener, event, domID);
115
- event.currentTarget = null;
116
- return returnValue;
117
-}
118
-
119
-/**
120
- * Standard/simple iteration through an event's collected dispatches.
121
- */
122
-function executeDispatchesInOrder(event, cb) {
123
- forEachEventDispatch(event, cb);
113
event._dispatchListeners = null;
114
event._dispatchIDs = null;
115
}
@@ -210,7 +199,6 @@ var EventPluginUtils = {
199
isStartish: isStartish,
200
201
executeDirectDispatch: executeDirectDispatch,
213
- executeDispatch: executeDispatch,
202
executeDispatchesInOrder: executeDispatchesInOrder,
203
executeDispatchesInOrderStopAtTrue: executeDispatchesInOrderStopAtTrue,
204
hasDispatches: hasDispatches,