-
Notifications
You must be signed in to change notification settings - Fork 47.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
68 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters