Skip to content

Commit 841c866

Browse files
Fix a race condition where a rapid shrink and expand will cause neither to trigger a change
If a user manages to rapidly change both the shrink and expand, then the first one to fire may reset the last width and height causing the other scroll event to fail the change check, incorrectly. By keeping them on separate variables, we prevent this kind of exception.
1 parent ea503aa commit 841c866

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/ResizeSensor.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,28 @@
8686
var shrink = element.resizeSensor.childNodes[1];
8787
var shrinkChild = shrink.childNodes[0];
8888

89-
var lastWidth, lastHeight;
89+
var lastShrinkWidth, lastShrinkHeight;
90+
var lastExpandWidth, lastExpandHeight;
9091

9192
var reset = function() {
93+
resetExpand();
94+
resetShrink();
95+
};
96+
97+
var resetExpand = function() {
9298
expandChild.style.width = expand.offsetWidth + 10 + 'px';
9399
expandChild.style.height = expand.offsetHeight + 10 + 'px';
94100
expand.scrollLeft = expand.scrollWidth;
95101
expand.scrollTop = expand.scrollHeight;
102+
lastExpandWidth = element.offsetWidth;
103+
lastExpandHeight = element.offsetHeight;
104+
};
105+
106+
var resetShrink = function() {
96107
shrink.scrollLeft = shrink.scrollWidth;
97108
shrink.scrollTop = shrink.scrollHeight;
98-
lastWidth = element.offsetWidth;
99-
lastHeight = element.offsetHeight;
109+
lastShrinkWidth = element.offsetWidth;
110+
lastShrinkHeight = element.offsetHeight;
100111
};
101112

102113
reset();
@@ -116,17 +127,17 @@
116127
};
117128

118129
addEvent(expand, 'scroll', function() {
119-
if (element.offsetWidth > lastWidth || element.offsetHeight > lastHeight) {
130+
if (element.offsetWidth > lastExpandWidth || element.offsetHeight > lastExpandHeight) {
120131
changed();
121132
}
122-
reset();
133+
resetExpand();
123134
});
124135

125136
addEvent(shrink, 'scroll',function() {
126-
if (element.offsetWidth < lastWidth || element.offsetHeight < lastHeight) {
137+
if (element.offsetWidth < lastShrinkWidth || element.offsetHeight < lastShrinkHeight) {
127138
changed();
128139
}
129-
reset();
140+
resetShrink();
130141
});
131142
}
132143

0 commit comments

Comments
 (0)