@@ -2,6 +2,7 @@ import React, { PureComponent, PropTypes } from 'react';
22import ReactDOM from 'react-dom' ;
33
44import getVisibleItemBounds from './utils/getVisibleItemBounds' ;
5+ import throttleWithRAF from './utils/throttleWithRAF' ;
56
67const 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 ;
0 commit comments