mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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.
This commit is contained in:
committed by
Paul O’Shannessy
parent
e60a893d2f
commit
c62c2c59bb
@@ -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(
|
||||
@@ -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(
|
||||
|
||||
@@ -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,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;
|
||||
@@ -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,
|
||||
@@ -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');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user