Skip to content

Commit

Permalink
feat: buffer while scrolling with progressive loading. (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
amcgee authored Apr 3, 2019
1 parent 3f8ec59 commit 7bea49b
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions src/components/Item/ProgressiveLoadingContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import PropTypes from 'prop-types';
import debounce from 'lodash/debounce';

const defaultDebounceMs = 100;
const defaultBufferPx = 0;
const defaultInitialBufferFactor = 0.5;
const defaultBufferFactor = 0.25;

class ProgressiveLoadingContainer extends Component {
static propTypes = {
children: PropTypes.node.isRequired,
debounceMs: PropTypes.number,
bufferPx: PropTypes.number,
initialBufferFactor: PropTypes.number,
bufferFactor: PropTypes.number,
};
static defaultProps = {
debounceMs: defaultDebounceMs,
bufferPx: defaultBufferPx,
initialBufferFactor: defaultInitialBufferFactor,
bufferFactor: defaultBufferFactor,
};

state = {
Expand All @@ -25,8 +22,8 @@ class ProgressiveLoadingContainer extends Component {
containerRef = null;
shouldLoadHandler = null;

checkShouldLoad(customBufferPx) {
const bufferPx = customBufferPx || this.props.bufferPx;
checkShouldLoad() {
const bufferPx = this.props.bufferFactor * window.innerHeight;

if (!this.containerRef) {
return;
Expand Down Expand Up @@ -59,25 +56,15 @@ class ProgressiveLoadingContainer extends Component {

componentDidMount() {
this.registerHandler();

const initialBufferPx = this.props.initialBufferFactor
? this.props.initialBufferFactor * window.innerHeight
: undefined;
this.checkShouldLoad(initialBufferPx);
this.checkShouldLoad();
}

componentWillUnmount() {
this.removeHandler();
}

render() {
const {
children,
debounceMs,
bufferPx,
initialBufferFactor,
...props
} = this.props;
const { children, debounceMs, bufferFactor, ...props } = this.props;
const { shouldLoad } = this.state;

return (
Expand Down

0 comments on commit 7bea49b

Please sign in to comment.