Skip to content

Commit

Permalink
Remove event listeners from native dom wrapper components
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zpao committed May 23, 2014
1 parent e60a893 commit c62c2c5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
8 changes: 6 additions & 2 deletions src/browser/ReactEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ function getListeningForDocument(mountAt) {
* @param {string} topLevelType Record from `EventConstants`.
* @param {string} handlerBaseName Event name (e.g. "click").
* @param {DOMEventTarget} element Element on which to attach listener.
* @return {object} An object with a remove function which will forcefully
* remove the listener.
* @internal
*/
function trapBubbledEvent(topLevelType, handlerBaseName, element) {
EventListener.listen(
return EventListener.listen(
element,
handlerBaseName,
ReactEventEmitter.TopLevelCallbackCreator.createTopLevelCallback(
Expand All @@ -162,10 +164,12 @@ function trapBubbledEvent(topLevelType, handlerBaseName, element) {
* @param {string} topLevelType Record from `EventConstants`.
* @param {string} handlerBaseName Event name (e.g. "click").
* @param {DOMEventTarget} element Element on which to attach listener.
* @return {object} An object with a remove function which will forcefully
* remove the listener.
* @internal
*/
function trapCapturedEvent(topLevelType, handlerBaseName, element) {
EventListener.capture(
return EventListener.capture(
element,
handlerBaseName,
ReactEventEmitter.TopLevelCallbackCreator.createTopLevelCallback(
Expand Down
52 changes: 52 additions & 0 deletions src/browser/ui/dom/components/LocalEventTrapMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule LocalEventTrapMixin
*/

"use strict";

var ReactEventEmitter = require('ReactEventEmitter');

var accumulate = require('accumulate');
var forEachAccumulated = require('forEachAccumulated');
var invariant = require('invariant');

function remove(event) {
event.remove();
}

var LocalEventTrapMixin = {
trapBubbledEvent(topLevelType, handlerBaseName) {
invariant(this.isMounted(), 'Must be mounted to trap events');
var listener = ReactEventEmitter.trapBubbledEvent(
topLevelType,
handlerBaseName,
this.getDOMNode()
);
this._localEventListeners = accumulate(this._localEventListeners, listener);
},

// trapCapturedEvent would look nearly identical. We don't implement that
// method because it isn't currently needed.

componentWillUnmount() {
if (this._localEventListeners) {
forEachAccumulated(this._localEventListeners, remove);
}
}
};

module.exports = LocalEventTrapMixin;
18 changes: 5 additions & 13 deletions src/browser/ui/dom/components/ReactDOMForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

"use strict";

var EventConstants = require('EventConstants');
var LocalEventTrapMixin = require('LocalEventTrapMixin');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDOM = require('ReactDOM');
var ReactEventEmitter = require('ReactEventEmitter');
var EventConstants = require('EventConstants');

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

mixins: [ReactBrowserComponentMixin],
mixins: [ReactBrowserComponentMixin, LocalEventTrapMixin],

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

componentDidMount: function() {
ReactEventEmitter.trapBubbledEvent(
EventConstants.topLevelTypes.topReset,
'reset',
this.getDOMNode()
);
ReactEventEmitter.trapBubbledEvent(
EventConstants.topLevelTypes.topSubmit,
'submit',
this.getDOMNode()
);
this.trapBubbledEvent(EventConstants.topLevelTypes.topReset, 'reset');
this.trapBubbledEvent(EventConstants.topLevelTypes.topSubmit, 'submit');
}
});

Expand Down
19 changes: 5 additions & 14 deletions src/browser/ui/dom/components/ReactDOMImg.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

"use strict";

var EventConstants = require('EventConstants');
var LocalEventTrapMixin = require('LocalEventTrapMixin');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactDOM = require('ReactDOM');
var ReactEventEmitter = require('ReactEventEmitter');
var EventConstants = require('EventConstants');

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

mixins: [ReactBrowserComponentMixin],
mixins: [ReactBrowserComponentMixin, LocalEventTrapMixin],

render: function() {
return img(this.props);
},

componentDidMount: function() {
var node = this.getDOMNode();
ReactEventEmitter.trapBubbledEvent(
EventConstants.topLevelTypes.topLoad,
'load',
node
);
ReactEventEmitter.trapBubbledEvent(
EventConstants.topLevelTypes.topError,
'error',
node
);
this.trapBubbledEvent(EventConstants.topLevelTypes.topReset, 'load');
this.trapBubbledEvent(EventConstants.topLevelTypes.topSubmit, 'error');
}
});

Expand Down

0 comments on commit c62c2c5

Please sign in to comment.