diff --git a/app/components/post_list/__snapshots__/post_list.test.js.snap b/app/components/post_list/__snapshots__/post_list.test.js.snap index 81847c7eb..d3223c26a 100644 --- a/app/components/post_list/__snapshots__/post_list.test.js.snap +++ b/app/components/post_list/__snapshots__/post_list.test.js.snap @@ -41,6 +41,7 @@ exports[`PostList setting channel deep link 1`] = ` onEndReachedThreshold={2} onLayout={[Function]} onScroll={[Function]} + onScrollBeginDrag={[Function]} onScrollToIndexFailed={[Function]} refreshControl={ { + this.animationFrameInitialIndex = requestAnimationFrame(() => { + if (!this.cancelScrollToIndex) { + this.flatListRef.current.scrollToIndex({ + animated: false, + index, + viewOffset: 0, + viewPosition: 1, // 0 is at bottom + }); + } + }); } getItemCount = () => { @@ -278,6 +286,10 @@ export default class PostList extends PureComponent { } }; + handleScrollBeginDrag = () => { + this.cancelScrollToIndex = true; + } + handleScrollToIndexFailed = () => { this.animationFrameIndexFailed = requestAnimationFrame(() => { if (this.props.initialIndex > 0 && this.state.contentHeight > 0) { @@ -297,7 +309,7 @@ export default class PostList extends PureComponent { }; loadToFillContent = () => { - setTimeout(() => { + this.fillContentTimer = setTimeout(() => { this.handleContentSizeChange(0, this.state.contentHeight); }); }; @@ -386,27 +398,38 @@ export default class PostList extends PureComponent { }; scrollToBottom = () => { - setTimeout(() => { + this.scrollToBottomTimer = setTimeout(() => { if (this.flatListRef.current) { this.flatListRef.current.scrollToOffset({offset: 0, animated: true}); } }, 250); }; - flatListScrollToIndex = (index) => { - this.animationFrameInitialIndex = requestAnimationFrame(() => { - this.flatListRef.current.scrollToIndex({ - animated: false, - index, - viewOffset: 0, - viewPosition: 1, // 0 is at bottom - }); - }); - } - resetPostList = () => { this.contentOffsetY = 0; this.hasDoneInitialScroll = false; + this.cancelScrollToIndex = false; + + if (this.animationFrameIndexFailed) { + cancelAnimationFrame(this.animationFrameIndexFailed); + } + + if (this.animationFrameInitialIndex) { + cancelAnimationFrame(this.animationFrameInitialIndex); + } + + if (this.fillContentTimer) { + clearTimeout(this.fillContentTimer); + } + + if (this.scrollToBottomTimer) { + clearTimeout(this.scrollToBottomTimer); + } + + if (this.scrollToInitialTimer) { + clearTimeout(this.scrollToInitialTimer); + } + if (this.state.contentHeight !== 0) { this.setState({contentHeight: 0}); } @@ -426,7 +449,7 @@ export default class PostList extends PureComponent { this.hasDoneInitialScroll = true; this.scrollToIndex(index); } else if (count < 3) { - setTimeout(() => { + this.scrollToInitialTimer = setTimeout(() => { this.scrollToInitialIndexIfNeeded(index, count + 1); }, 300); } @@ -496,6 +519,7 @@ export default class PostList extends PureComponent { onContentSizeChange={this.handleContentSizeChange} onLayout={this.handleLayout} onScroll={this.handleScroll} + onScrollBeginDrag={this.handleScrollBeginDrag} onScrollToIndexFailed={this.handleScrollToIndexFailed} removeClippedSubviews={true} renderItem={this.renderItem}