Skip to content

Commit f6cf422

Browse files
committed
Improve stack overflow detection, thanks @STRML, 0.8.3
1 parent 5f92a4d commit f6cf422

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 0.8.3
4+
- Use `setTimeout` to detect stack overflow.
5+
36
## 0.8.2
47
- Fix issue with misconfiguration detection. (#160)
58

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-list",
3-
"version": "0.8.2",
3+
"version": "0.8.3",
44
"author": "Casey Foster <[email protected]>",
55
"license": "MIT",
66
"main": "react-list.js",

react-list.es6

+10-14
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ const PASSIVE = (() => {
3434
})() ? {passive: true} : false;
3535

3636
const UNSTABLE_MESSAGE = 'ReactList failed to reach a stable state.';
37-
const MARKER_DURATION = 100;
38-
const MAX_UPDATES_PER_MARKER_DURATION = 20;
37+
const MAX_SYNC_UPDATES = 100;
3938

4039
const isEqualSubset = (a, b) => {
4140
for (let key in b) if (a[key] !== b[key]) return false;
@@ -84,6 +83,7 @@ module.exports = class ReactList extends Component {
8483
this.cache = {};
8584
this.prevPrevState = {};
8685
this.unstable = false;
86+
this.updateCounter = 0;
8787
}
8888

8989
componentWillReceiveProps(next) {
@@ -102,22 +102,18 @@ module.exports = class ReactList extends Component {
102102
// If the list has reached an unstable state, prevent an infinite loop.
103103
if (this.unstable) return;
104104

105-
if (this.updateMarker && Date.now() - this.updateMarker > MARKER_DURATION) {
106-
delete this.updateMarker;
107-
}
108-
109-
if (!this.updateMarker) {
110-
this.updateMarker = Date.now();
111-
this.updatesSinceMarker = 0;
112-
}
113-
114-
++this.updatesSinceMarker;
115-
116-
if (this.updatesSinceMarker > MAX_UPDATES_PER_MARKER_DURATION) {
105+
if (++this.updateCounter > MAX_SYNC_UPDATES) {
117106
this.unstable = true;
118107
return console.error(UNSTABLE_MESSAGE);
119108
}
120109

110+
if (!this.updateCounterTimeoutId) {
111+
this.updateCounterTimeoutId = setTimeout(() => {
112+
this.updateCounter = 0;
113+
delete this.updateCounterTimeoutId;
114+
}, 0);
115+
}
116+
121117
this.updateFrame();
122118
}
123119

react-list.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@
108108
}() ? { passive: true } : false;
109109

110110
var UNSTABLE_MESSAGE = 'ReactList failed to reach a stable state.';
111-
var MARKER_DURATION = 100;
112-
var MAX_UPDATES_PER_MARKER_DURATION = 20;
111+
var MAX_SYNC_UPDATES = 100;
113112

114113
var isEqualSubset = function isEqualSubset(a, b) {
115114
for (var key in b) {
@@ -139,6 +138,7 @@
139138
_this.cache = {};
140139
_this.prevPrevState = {};
141140
_this.unstable = false;
141+
_this.updateCounter = 0;
142142
return _this;
143143
}
144144

@@ -162,26 +162,23 @@
162162
}, {
163163
key: 'componentDidUpdate',
164164
value: function componentDidUpdate() {
165+
var _this2 = this;
165166

166167
// If the list has reached an unstable state, prevent an infinite loop.
167168
if (this.unstable) return;
168169

169-
if (this.updateMarker && Date.now() - this.updateMarker > MARKER_DURATION) {
170-
delete this.updateMarker;
171-
}
172-
173-
if (!this.updateMarker) {
174-
this.updateMarker = Date.now();
175-
this.updatesSinceMarker = 0;
176-
}
177-
178-
++this.updatesSinceMarker;
179-
180-
if (this.updatesSinceMarker > MAX_UPDATES_PER_MARKER_DURATION) {
170+
if (++this.updateCounter > MAX_SYNC_UPDATES) {
181171
this.unstable = true;
182172
return console.error(UNSTABLE_MESSAGE);
183173
}
184174

175+
if (!this.updateCounterTimeoutId) {
176+
this.updateCounterTimeoutId = setTimeout(function () {
177+
_this2.updateCounter = 0;
178+
delete _this2.updateCounterTimeoutId;
179+
}, 0);
180+
}
181+
185182
this.updateFrame();
186183
}
187184
}, {
@@ -583,7 +580,7 @@
583580
}, {
584581
key: 'renderItems',
585582
value: function renderItems() {
586-
var _this2 = this;
583+
var _this3 = this;
587584

588585
var _props7 = this.props,
589586
itemRenderer = _props7.itemRenderer,
@@ -596,7 +593,7 @@
596593
for (var i = 0; i < size; ++i) {
597594
items.push(itemRenderer(from + i, i));
598595
}return itemsRenderer(items, function (c) {
599-
return _this2.items = c;
596+
return _this3.items = c;
600597
});
601598
}
602599
}, {

0 commit comments

Comments
 (0)