Undo fbjs/lib/EventListener inlining (#11431)
Dan Abramov committed
Nov 2, 2017 at 13:43 UTC
c97c92061b39a05c05ea807c4f40b0cb96061b24
3 files changed
+10
-57
packages/react-dom/src/events/ReactDOMEventListener.js
+8
-29
@@ -8,9 +8,9 @@
8
'use strict';
9
10
var ReactGenericBatching = require('events/ReactGenericBatching');
11
-var ReactErrorUtils = require('shared/ReactErrorUtils');
11
var ReactFiberTreeReflection = require('shared/ReactFiberTreeReflection');
12
var ReactTypeOfWork = require('shared/ReactTypeOfWork');
13
+var EventListener = require('fbjs/lib/EventListener');
14
var warning = require('fbjs/lib/warning');
15
var {HostRoot} = ReactTypeOfWork;
16
@@ -128,18 +128,11 @@ var ReactDOMEventListener = {
128
if (!element) {
129
return null;
130
}
131
- // TODO: Once we have static injection we should just wrap
132
- // ReactDOMEventListener.dispatchEvent statically so we don't have to do
133
- // it for every event type.
134
- var callback = ReactErrorUtils.wrapEventListener(
131
+ return EventListener.listen(
132
+ element,
133
handlerBaseName,
134
ReactDOMEventListener.dispatchEvent.bind(null, topLevelType),
135
);
138
- if (element.addEventListener) {
139
- element.addEventListener(handlerBaseName, callback, false);
140
- } else if (element.attachEvent) {
141
- element.attachEvent('on' + handlerBaseName, callback);
142
- }
136
},
137
138
/**
@@ -156,25 +149,11 @@ var ReactDOMEventListener = {
149
if (!element) {
150
return null;
151
}
159
- if (element.addEventListener) {
160
- // TODO: Once we have static injection we should just wrap
161
- // ReactDOMEventListener.dispatchEvent statically so we don't have to do
162
- // it for every event type.
163
- var callback = ReactErrorUtils.wrapEventListener(
164
- handlerBaseName,
165
- ReactDOMEventListener.dispatchEvent.bind(null, topLevelType),
166
- );
167
- element.addEventListener(handlerBaseName, callback, true);
168
- } else {
169
- if (__DEV__) {
170
- warning(
171
- false,
172
- 'Attempted to listen to events during the capture phase on a ' +
173
- 'browser that does not support the capture phase. Your application ' +
174
- 'will not receive some events.',
175
- );
176
- }
177
- }
152
+ return EventListener.capture(
153
+ element,
154
+ handlerBaseName,
155
+ ReactDOMEventListener.dispatchEvent.bind(null, topLevelType),
156
+ );
157
},
158
159
dispatchEvent: function(topLevelType, nativeEvent) {
packages/react-reconciler/src/ReactFiberScheduler.js
+2
-16
@@ -19,7 +19,6 @@ var {
19
getStackAddendumByWorkInProgressFiber,
20
} = require('shared/ReactFiberComponentTreeHook');
21
var {
22
- wrapEventListener,
22
invokeGuardedCallback,
23
hasCaughtError,
24
clearCaughtError,
@@ -1286,17 +1285,6 @@ module.exports = function<T, P, I, TI, PI, C, CC, CX, PL>(
1285
// TODO: Everything below this is written as if it has been lifted to the
1286
// renderers. I'll do this in a follow-up.
1287
1289
- // Ensure performAsyncWork gets wrapped. This currently needs to be lazy because
1290
- // we use dynamic injection. TODO: Make this eagerly wrapping this callback once
1291
- // we have static injection.
1292
- let hasCallbackBeenScheduled: boolean = false;
1293
- function ensureCallbackWrapped() {
1294
- if (!hasCallbackBeenScheduled) {
1295
- performAsyncWork = wrapEventListener('reactAsyncWork', performAsyncWork);
1296
- hasCallbackBeenScheduled = true;
1297
- }
1298
- }
1299
-
1288
// Linked-list of roots
1289
let firstScheduledRoot: FiberRoot | null = null;
1290
let lastScheduledRoot: FiberRoot | null = null;
@@ -1378,7 +1366,6 @@ module.exports = function<T, P, I, TI, PI, C, CC, CX, PL>(
1366
performWork(Sync, null);
1367
} else if (!isCallbackScheduled) {
1368
isCallbackScheduled = true;
1381
- ensureCallbackWrapped();
1369
scheduleDeferredCallback(performAsyncWork);
1370
}
1371
}
@@ -1459,9 +1446,9 @@ module.exports = function<T, P, I, TI, PI, C, CC, CX, PL>(
1446
nextFlushedExpirationTime = highestPriorityWork;
1447
}
1448
1462
- let performAsyncWork = function(dl) {
1449
+ function performAsyncWork(dl) {
1450
performWork(NoWork, dl);
1464
- };
1451
+ }
1452
1453
function performWork(minExpirationTime: ExpirationTime, dl: Deadline | null) {
1454
deadline = dl;
@@ -1491,7 +1478,6 @@ module.exports = function<T, P, I, TI, PI, C, CC, CX, PL>(
1478
// If there's work left over, schedule a new callback.
1479
if (nextFlushedRoot !== null && !isCallbackScheduled) {
1480
isCallbackScheduled = true;
1494
- ensureCallbackWrapped();
1481
scheduleDeferredCallback(performAsyncWork);
1482
}
1483
packages/shared/ReactErrorUtils.js
-12
@@ -27,7 +27,6 @@ const ReactErrorUtils = {
27
'Injected invokeGuardedCallback() must be a function.',
28
);
29
invokeGuardedCallback = injectedErrorUtils.invokeGuardedCallback;
30
- wrapEventListener = injectedErrorUtils.wrapEventListener;
30
},
31
},
32
@@ -58,13 +57,6 @@ const ReactErrorUtils = {
57
invokeGuardedCallback.apply(ReactErrorUtils, arguments);
58
},
59
61
- /**
62
- * Wrap a callback in whatever logic it needs. Used in FB to polyfill Promises.
63
- */
64
- wrapEventListener: function<T>(name: string, callback: T): T {
65
- return wrapEventListener(name, callback);
66
- },
67
-
60
/**
61
* Same as invokeGuardedCallback, but instead of returning an error, it stores
62
* it in a global so it can be rethrown by `rethrowCaughtError` later.
@@ -124,10 +116,6 @@ const ReactErrorUtils = {
116
},
117
};
118
127
-let wrapEventListener = function<T>(name: string, callback: T): T {
128
- return callback;
129
-};
130
-
119
let invokeGuardedCallback = function(name, func, context, a, b, c, d, e, f) {
120
ReactErrorUtils._hasCaughtError = false;
121
ReactErrorUtils._caughtError = null;