-
-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cancelAnimationFrame for reset when detach is called (#282)
In ResizeSensor, we use requestAnimationFrame for reset. However, if an element is added to DOM and removed from DOM very quickly (even before the first reset happens), the reset itself will stuck in an infinite loop. It also causes memory leak because the reset function holds the element forever.
- Loading branch information
Showing
1 changed file
with
9 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,8 @@ | |
* @constructor | ||
*/ | ||
var ResizeSensor = function(element, callback) { | ||
var lastAnimationFrame = 0; | ||
|
||
/** | ||
* | ||
* @constructor | ||
|
@@ -204,7 +206,7 @@ | |
var lastWidth = 0; | ||
var lastHeight = 0; | ||
var initialHiddenCheck = true; | ||
var lastAnimationFrame = 0; | ||
lastAnimationFrame = 0; | ||
|
||
var resetExpandShrink = function () { | ||
var width = element.offsetWidth; | ||
|
@@ -281,14 +283,19 @@ | |
addEvent(shrink, 'scroll', onScroll); | ||
|
||
// Fix for custom Elements | ||
requestAnimationFrame(reset); | ||
lastAnimationFrame = requestAnimationFrame(reset); | ||
} | ||
|
||
forEachElement(element, function(elem){ | ||
attachResizeEvent(elem, callback); | ||
}); | ||
|
||
this.detach = function(ev) { | ||
// clean up the unfinished animation frame to prevent a potential endless requestAnimationFrame of reset | ||
if (!lastAnimationFrame) { | ||
window.cancelAnimationFrame(lastAnimationFrame); | ||
lastAnimationFrame = 0; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
marcj
Owner
|
||
} | ||
ResizeSensor.detach(element, ev); | ||
}; | ||
|
||
|
Shouldn't it be set to 1 here?