Skip to content

Commit f7444af

Browse files
Merge pull request #40 from vinnymac/throttle-with-raf
move throttle code to a utility method
2 parents c675b36 + aa3e82e commit f7444af

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-virtual-list",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Super simple virtualized list React higher-order component",
55
"main": "dist/VirtualList.js",
66
"directories": {

src/VirtualList.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { PureComponent, PropTypes } from 'react';
22
import ReactDOM from 'react-dom';
33

44
import getVisibleItemBounds from './utils/getVisibleItemBounds';
5+
import throttleWithRAF from './utils/throttleWithRAF';
56

67
const VirtualList = (options) => (InnerComponent) => {
78
return class vlist extends PureComponent {
@@ -40,19 +41,7 @@ const VirtualList = (options) => (InnerComponent) => {
4041

4142
// if requestAnimationFrame is available, use it to throttle refreshState
4243
if (window && 'requestAnimationFrame' in window) {
43-
const refreshState = this.refreshState;
44-
45-
this.refreshState = () => {
46-
if (this.isRefreshingState) return;
47-
48-
this.isRefreshingState = true;
49-
50-
window.requestAnimationFrame(() => {
51-
refreshState();
52-
53-
this.isRefreshingState = false;
54-
});
55-
};
44+
this.refreshState = throttleWithRAF(this.refreshState);
5645
}
5746
};
5847

@@ -64,13 +53,13 @@ const VirtualList = (options) => (InnerComponent) => {
6453
this.setState(state);
6554
}
6655
}
67-
56+
6857
refreshState() {
6958
const { itemHeight, items, itemBuffer } = this.props;
7059

7160
this.setStateIfNeeded(this.domNode, this.options.container, items, itemHeight, itemBuffer);
7261
};
73-
62+
7463
componentDidMount() {
7564
// cache the DOM node
7665
this.domNode = ReactDOM.findDOMNode(this);
@@ -82,7 +71,7 @@ const VirtualList = (options) => (InnerComponent) => {
8271
this.options.container.addEventListener('scroll', this.refreshState);
8372
this.options.container.addEventListener('resize', this.refreshState);
8473
};
85-
74+
8675
componentWillUnmount() {
8776
// remove events
8877
this.options.container.removeEventListener('scroll', this.refreshState);
@@ -95,7 +84,7 @@ const VirtualList = (options) => (InnerComponent) => {
9584

9685
this.setStateIfNeeded(this.domNode, this.options.container, items, itemHeight, itemBuffer);
9786
};
98-
87+
9988
render() {
10089
const { firstItemIndex, lastItemIndex } = this.state;
10190
const { items, itemHeight } = this.props;

src/utils/throttleWithRAF.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default (fn) => {
2+
let running = false;
3+
4+
return () => {
5+
if (running) return;
6+
7+
running = true;
8+
9+
window.requestAnimationFrame(() => {
10+
fn.apply(this, arguments);
11+
12+
running = false;
13+
});
14+
};
15+
};

0 commit comments

Comments
 (0)