Skip to content

Commit 5f92a4d

Browse files
committed
Close #160 Fix issue with misconfiguration detection, 0.8.2
1 parent c544fee commit 5f92a4d

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 0.8.2
4+
- Fix issue with misconfiguration detection. (#160)
5+
36
## 0.8.1
47
- Provide an error message if the list reaches an unstable state due to
58
misconfiguration. (#156, #157)

package.json

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

react-list.es6

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ 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;
3739

3840
const isEqualSubset = (a, b) => {
3941
for (let key in b) if (a[key] !== b[key]) return false;
4042

4143
return true;
4244
};
4345

44-
const isEqual = (a, b) => isEqualSubset(a, b) && isEqualSubset(b, a);
45-
4646
module.exports = class ReactList extends Component {
4747
static displayName = 'ReactList';
4848

@@ -97,31 +97,28 @@ module.exports = class ReactList extends Component {
9797
this.updateFrame(this.scrollTo.bind(this, this.props.initialIndex));
9898
}
9999

100-
componentDidUpdate(prevProps, prevState) {
100+
componentDidUpdate() {
101101

102102
// If the list has reached an unstable state, prevent an infinite loop.
103103
if (this.unstable) return;
104104

105-
// Update calculations if props have changed between renders.
106-
const propsEqual = isEqual(this.props, prevProps);
107-
if (!propsEqual) {
108-
this.prevPrevState = {};
109-
return this.updateFrame();
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;
110112
}
111113

112-
// Check for ping-ponging between the same two states.
113-
const stateEqual = isEqual(this.state, prevState);
114-
const pingPong = !stateEqual && isEqual(this.state, this.prevPrevState);
114+
++this.updatesSinceMarker;
115115

116-
// Ping-ponging between states means this list is unstable, log an error.
117-
if (pingPong) {
116+
if (this.updatesSinceMarker > MAX_UPDATES_PER_MARKER_DURATION) {
118117
this.unstable = true;
119118
return console.error(UNSTABLE_MESSAGE);
120119
}
121120

122-
// Update calculations if state has changed between renders.
123-
this.prevPrevState = prevState;
124-
if (!stateEqual) this.updateFrame();
121+
this.updateFrame();
125122
}
126123

127124
maybeSetState(b, cb) {

react-list.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,15 @@
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;
111113

112114
var isEqualSubset = function isEqualSubset(a, b) {
113115
for (var key in b) {
114116
if (a[key] !== b[key]) return false;
115117
}return true;
116118
};
117119

118-
var isEqual = function isEqual(a, b) {
119-
return isEqualSubset(a, b) && isEqualSubset(b, a);
120-
};
121-
122120
_module3.default.exports = (_temp = _class = function (_Component) {
123121
_inherits(ReactList, _Component);
124122

@@ -163,31 +161,28 @@
163161
}
164162
}, {
165163
key: 'componentDidUpdate',
166-
value: function componentDidUpdate(prevProps, prevState) {
164+
value: function componentDidUpdate() {
167165

168166
// If the list has reached an unstable state, prevent an infinite loop.
169167
if (this.unstable) return;
170168

171-
// Update calculations if props have changed between renders.
172-
var propsEqual = isEqual(this.props, prevProps);
173-
if (!propsEqual) {
174-
this.prevPrevState = {};
175-
return this.updateFrame();
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;
176176
}
177177

178-
// Check for ping-ponging between the same two states.
179-
var stateEqual = isEqual(this.state, prevState);
180-
var pingPong = !stateEqual && isEqual(this.state, this.prevPrevState);
178+
++this.updatesSinceMarker;
181179

182-
// Ping-ponging between states means this list is unstable, log an error.
183-
if (pingPong) {
180+
if (this.updatesSinceMarker > MAX_UPDATES_PER_MARKER_DURATION) {
184181
this.unstable = true;
185182
return console.error(UNSTABLE_MESSAGE);
186183
}
187184

188-
// Update calculations if state has changed between renders.
189-
this.prevPrevState = prevState;
190-
if (!stateEqual) this.updateFrame();
185+
this.updateFrame();
191186
}
192187
}, {
193188
key: 'maybeSetState',

0 commit comments

Comments
 (0)