Skip to content

Commit c62c2c5

Browse files
committed
Remove event listeners from native dom wrapper components
Not removing them resulted in leaks as we would hold on to removed nodes forever. This really showed up with images and the load event where we would unmount and create a new img with the same react id as the old one. We properly cleared and primed the caches but we would handle the load event for both nodes. We would eventually hit an invariant in that path as we tried to handle the event for the removed node, which no longer matched the node we had in the cache. By forcefully removing the listener, we'll avoid this problem entirely and we should leak fewer DOM nodes.
1 parent e60a893 commit c62c2c5

File tree

4 files changed

+68
-29
lines changed

4 files changed

+68
-29
lines changed

src/browser/ReactEventEmitter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ function getListeningForDocument(mountAt) {
144144
* @param {string} topLevelType Record from `EventConstants`.
145145
* @param {string} handlerBaseName Event name (e.g. "click").
146146
* @param {DOMEventTarget} element Element on which to attach listener.
147+
* @return {object} An object with a remove function which will forcefully
148+
* remove the listener.
147149
* @internal
148150
*/
149151
function trapBubbledEvent(topLevelType, handlerBaseName, element) {
150-
EventListener.listen(
152+
return EventListener.listen(
151153
element,
152154
handlerBaseName,
153155
ReactEventEmitter.TopLevelCallbackCreator.createTopLevelCallback(
@@ -162,10 +164,12 @@ function trapBubbledEvent(topLevelType, handlerBaseName, element) {
162164
* @param {string} topLevelType Record from `EventConstants`.
163165
* @param {string} handlerBaseName Event name (e.g. "click").
164166
* @param {DOMEventTarget} element Element on which to attach listener.
167+
* @return {object} An object with a remove function which will forcefully
168+
* remove the listener.
165169
* @internal
166170
*/
167171
function trapCapturedEvent(topLevelType, handlerBaseName, element) {
168-
EventListener.capture(
172+
return EventListener.capture(
169173
element,
170174
handlerBaseName,
171175
ReactEventEmitter.TopLevelCallbackCreator.createTopLevelCallback(
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2014 Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* @providesModule LocalEventTrapMixin
17+
*/
18+
19+
"use strict";
20+
21+
var ReactEventEmitter = require('ReactEventEmitter');
22+
23+
var accumulate = require('accumulate');
24+
var forEachAccumulated = require('forEachAccumulated');
25+
var invariant = require('invariant');
26+
27+
function remove(event) {
28+
event.remove();
29+
}
30+
31+
var LocalEventTrapMixin = {
32+
trapBubbledEvent(topLevelType, handlerBaseName) {
33+
invariant(this.isMounted(), 'Must be mounted to trap events');
34+
var listener = ReactEventEmitter.trapBubbledEvent(
35+
topLevelType,
36+
handlerBaseName,
37+
this.getDOMNode()
38+
);
39+
this._localEventListeners = accumulate(this._localEventListeners, listener);
40+
},
41+
42+
// trapCapturedEvent would look nearly identical. We don't implement that
43+
// method because it isn't currently needed.
44+
45+
componentWillUnmount() {
46+
if (this._localEventListeners) {
47+
forEachAccumulated(this._localEventListeners, remove);
48+
}
49+
}
50+
};
51+
52+
module.exports = LocalEventTrapMixin;

src/browser/ui/dom/components/ReactDOMForm.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
"use strict";
2020

21+
var EventConstants = require('EventConstants');
22+
var LocalEventTrapMixin = require('LocalEventTrapMixin');
2123
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
2224
var ReactCompositeComponent = require('ReactCompositeComponent');
2325
var ReactDOM = require('ReactDOM');
24-
var ReactEventEmitter = require('ReactEventEmitter');
25-
var EventConstants = require('EventConstants');
2626

2727
// Store a reference to the <form> `ReactDOMComponent`.
2828
var form = ReactDOM.form;
@@ -36,7 +36,7 @@ var form = ReactDOM.form;
3636
var ReactDOMForm = ReactCompositeComponent.createClass({
3737
displayName: 'ReactDOMForm',
3838

39-
mixins: [ReactBrowserComponentMixin],
39+
mixins: [ReactBrowserComponentMixin, LocalEventTrapMixin],
4040

4141
render: function() {
4242
// TODO: Instead of using `ReactDOM` directly, we should use JSX. However,
@@ -46,16 +46,8 @@ var ReactDOMForm = ReactCompositeComponent.createClass({
4646
},
4747

4848
componentDidMount: function() {
49-
ReactEventEmitter.trapBubbledEvent(
50-
EventConstants.topLevelTypes.topReset,
51-
'reset',
52-
this.getDOMNode()
53-
);
54-
ReactEventEmitter.trapBubbledEvent(
55-
EventConstants.topLevelTypes.topSubmit,
56-
'submit',
57-
this.getDOMNode()
58-
);
49+
this.trapBubbledEvent(EventConstants.topLevelTypes.topReset, 'reset');
50+
this.trapBubbledEvent(EventConstants.topLevelTypes.topSubmit, 'submit');
5951
}
6052
});
6153

src/browser/ui/dom/components/ReactDOMImg.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
"use strict";
2020

21+
var EventConstants = require('EventConstants');
22+
var LocalEventTrapMixin = require('LocalEventTrapMixin');
2123
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
2224
var ReactCompositeComponent = require('ReactCompositeComponent');
2325
var ReactDOM = require('ReactDOM');
24-
var ReactEventEmitter = require('ReactEventEmitter');
25-
var EventConstants = require('EventConstants');
2626

2727
// Store a reference to the <img> `ReactDOMComponent`.
2828
var img = ReactDOM.img;
@@ -37,24 +37,15 @@ var ReactDOMImg = ReactCompositeComponent.createClass({
3737
displayName: 'ReactDOMImg',
3838
tagName: 'IMG',
3939

40-
mixins: [ReactBrowserComponentMixin],
40+
mixins: [ReactBrowserComponentMixin, LocalEventTrapMixin],
4141

4242
render: function() {
4343
return img(this.props);
4444
},
4545

4646
componentDidMount: function() {
47-
var node = this.getDOMNode();
48-
ReactEventEmitter.trapBubbledEvent(
49-
EventConstants.topLevelTypes.topLoad,
50-
'load',
51-
node
52-
);
53-
ReactEventEmitter.trapBubbledEvent(
54-
EventConstants.topLevelTypes.topError,
55-
'error',
56-
node
57-
);
47+
this.trapBubbledEvent(EventConstants.topLevelTypes.topReset, 'load');
48+
this.trapBubbledEvent(EventConstants.topLevelTypes.topSubmit, 'error');
5849
}
5950
});
6051

0 commit comments

Comments
 (0)