Skip to content

Commit cd5bda1

Browse files
committed
Run callbacks only if size changed between two frames
An element may change its size multiple times during a frame. Do not emit events anymore if its size returned to its previous size since the last update. This means that events occurring in between two frames - which are never visible to the user - may be skipped.
1 parent 3ff3bd2 commit cd5bda1

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/ResizeSensor.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,20 @@
141141
var dirty, lastWidth, lastHeight;
142142

143143
var updateSize = function() {
144+
var newWidth = element.offsetWidth;
145+
var newHeight = element.offsetHeight;
146+
144147
dirty = false;
145-
lastWidth = element.offsetWidth;
146-
lastHeight = element.offsetHeight;
148+
149+
// The size may stay the same if the element changed size more than once during one frame.
150+
if (newWidth == lastWidth && newHeight == lastHeight) {
151+
return false;
152+
}
153+
154+
lastWidth = newWidth;
155+
lastHeight = newHeight;
156+
157+
return true;
147158
};
148159

149160
updateSize();
@@ -163,11 +174,9 @@
163174

164175
var onResized = function() {
165176
// To prevent layout thrashing: first read from DOM ...
166-
updateSize();
167-
168-
if (!element.resizedAttached) return;
177+
if (!updateSize() || !element.resizedAttached) return;
169178

170-
/// ... then update
179+
/// ... then update.
171180
element.resizedAttached.call();
172181
reset();
173182
};

0 commit comments

Comments
 (0)