Skip to content

Instantly share code, notes, and snippets.

@brentvatne
Created January 18, 2016 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentvatne/80675663483c2015bbe2 to your computer and use it in GitHub Desktop.
Save brentvatne/80675663483c2015bbe2 to your computer and use it in GitHub Desktop.
/**
* @providesModule FeedView
*/
'use strict';
var ListViewDataSource = require('ListViewDataSource');
var React = require('react-native');
var RCTScrollViewManager = require('NativeModules').ScrollViewManager;
var ScrollView = require('ScrollView');
var ScrollResponder = require('ScrollResponder');
var StaticRenderer = require('StaticRenderer');
var TimerMixin = require('react-timer-mixin');
var View = require('View');
var InteractionManager = require('InteractionManager');
InteractionManager.setDeadline(100);
var isEmpty = require('isEmpty');
var logError = require('logError');
var merge = require('merge');
var PropTypes = React.PropTypes;
var DEFAULT_PAGE_SIZE = 1;
var DEFAULT_INITIAL_ROWS = 6;
var DEFAULT_SCROLL_RENDER_AHEAD = 1000;
var DEFAULT_END_REACHED_THRESHOLD = 1000;
var DEFAULT_SCROLL_CALLBACK_THROTTLE = 50;
var SCROLLVIEW_REF = 'listviewscroll';
const DEBUG = false;
var FeedView = React.createClass({
mixins: [ScrollResponder.Mixin, TimerMixin],
statics: {
DataSource: ListViewDataSource,
},
/**
* You must provide a renderRow function. If you omit any of the other render
* functions, ListView will simply skip rendering them.
*
* - renderRow(rowData, sectionID, rowID, highlightRow);
* - renderSectionHeader(sectionData, sectionID);
*/
propTypes: {
...ScrollView.propTypes,
dataSource: PropTypes.instanceOf(ListViewDataSource).isRequired,
/**
* (sectionID, rowID, adjacentRowHighlighted) => renderable
*
* If provided, a renderable component to be rendered as the separator
* below each row but not the last row if there is a section header below.
* Take a sectionID and rowID of the row above and whether its adjacent row
* is highlighted.
*/
renderSeparator: PropTypes.func,
/**
* (rowData, sectionID, rowID, highlightRow) => renderable
*
* Takes a data entry from the data source and its ids and should return
* a renderable component to be rendered as the row. By default the data
* is exactly what was put into the data source, but it's also possible to
* provide custom extractors. ListView can be notified when a row is
* being highlighted by calling highlightRow function. The separators above and
* below will be hidden when a row is highlighted. The highlighted state of
* a row can be reset by calling highlightRow(null).
*/
renderRow: PropTypes.func.isRequired,
/**
* Called when all rows have been rendered and the list has been scrolled
* to within onEndReachedThreshold of the bottom. The native scroll
* event is provided.
*/
onEndReached: PropTypes.func,
/**
* Threshold in pixels for onEndReached.
*/
onEndReachedThreshold: PropTypes.number,
/**
* Number of rows to render per event loop.
*/
pageSize: PropTypes.number,
/**
* () => renderable
*
* The header and footer are always rendered (if these props are provided)
* on every render pass. If they are expensive to re-render, wrap them
* in StaticContainer or other mechanism as appropriate. Footer is always
* at the bottom of the list, and header at the top, on every render pass.
*/
renderFooter: PropTypes.func,
renderHeader: PropTypes.func,
/**
* (sectionData, sectionID) => renderable
*
* If provided, a sticky header is rendered for this section. The sticky
* behavior means that it will scroll with the content at the top of the
* section until it reaches the top of the screen, at which point it will
* stick to the top until it is pushed off the screen by the next section
* header.
*/
renderSectionHeader: PropTypes.func,
/**
* (props) => renderable
*
* A function that returns the scrollable component in which the list rows
* are rendered. Defaults to returning a ScrollView with the given props.
*/
renderScrollComponent: React.PropTypes.func.isRequired,
/**
* How early to start rendering rows before they come on screen, in
* pixels.
*/
scrollRenderAheadDistance: React.PropTypes.number,
/**
* (visibleRows, changedRows) => void
*
* Called when the set of visible rows changes. `visibleRows` maps
* { sectionID: { rowID: true }} for all the visible rows, and
* `changedRows` maps { sectionID: { rowID: true | false }} for the rows
* that have changed their visibility, with true indicating visible, and
* false indicating the view has moved out of view.
*/
onChangeVisibleRows: React.PropTypes.func,
/**
* A performance optimization for improving scroll perf of
* large lists, used in conjunction with overflow: 'hidden' on the row
* containers. This is enabled by default.
*/
removeClippedSubviews: React.PropTypes.bool,
/**
* An array of child indices determining which children get docked to the
* top of the screen when scrolling. For example, passing
* `stickyHeaderIndices={[0]}` will cause the first child to be fixed to the
* top of the scroll view. This property is not supported in conjunction
* with `horizontal={true}`.
* @platform ios
*/
stickyHeaderIndices: PropTypes.arrayOf(PropTypes.number),
/**
* How many rows to render synchronously on initial component mount. Use
* this to make it so that the first screen worth of data appears more quickly
* than it would to render the full pageSize
*/
initialListSize: PropTypes.number,
},
/**
* Exports some data, e.g. for perf investigations or analytics.
*/
getMetrics: function() {
return {
contentLength: this.scrollProperties.contentLength,
totalRows: this.props.dataSource.getRowCount(),
renderedRows: this.state.curRenderedRowsCount,
visibleRows: Object.keys(this._visibleRows).length,
};
},
/**
* Provides a handle to the underlying scroll responder to support operations
* such as scrollTo.
*/
getScrollResponder: function() {
return this.refs[SCROLLVIEW_REF] &&
this.refs[SCROLLVIEW_REF].getScrollResponder &&
this.refs[SCROLLVIEW_REF].getScrollResponder();
},
scrollTo: function(destY, destX) {
this.getScrollResponder().scrollResponderScrollTo(destX || 0, destY || 0);
},
setNativeProps: function(props) {
this.refs[SCROLLVIEW_REF].setNativeProps(props);
},
/**
* React life cycle hooks.
*/
getDefaultProps: function() {
return {
initialListSize: DEFAULT_INITIAL_ROWS,
pageSize: DEFAULT_PAGE_SIZE,
renderScrollComponent: props => <ScrollView {...props} />,
scrollRenderAheadDistance: DEFAULT_SCROLL_RENDER_AHEAD,
onEndReachedThreshold: DEFAULT_END_REACHED_THRESHOLD,
stickyHeaderIndices: [],
};
},
getInitialState: function() {
return {
curRenderedRowsCount: 0,
updateBatchId: 0,
highlightedRow: {},
};
},
getInnerViewNode: function() {
return this.refs[SCROLLVIEW_REF].getInnerViewNode();
},
componentWillMount: function() {
// this data should never trigger a render pass, so don't put in state
this.scrollProperties = {
visibleLength: null,
contentLength: null,
offset: 0
};
this._childFrames = [];
this._visibleRows = {};
this._prevRenderedRowsCount = 0;
this._sentEndForContentLength = null;
this._updateBatches = {};
this._rowRefs = {};
this._separatorRefs = {};
this._pageInNewRows(this.props);
},
componentDidMount: function() {
// do this in animation frame until componentDidMount actually runs after
// the component is laid out
this.requestAnimationFrame(() => {
this._measureAndUpdateScrollProps();
});
},
componentWillReceiveProps: function(nextProps) {
if (this.props.dataSource !== nextProps.dataSource) {
this._resetRowCount = true;
this._pageInNewRows(nextProps);
}
},
componentDidUpdate: function() {
this.requestAnimationFrame(() => {
this._measureAndUpdateScrollProps();
});
},
onRowHighlighted: function(sectionID, rowID) {
this.setState({highlightedRow: {sectionID, rowID}});
},
render: function() {
DEBUG && console.log({
curRenderedRowsCount: this.state.curRenderedRowsCount
})
var bodyComponents = [];
var dataSource = this.props.dataSource;
var allRowIDs = dataSource.rowIdentities;
var rowCount = 0;
var sectionHeaderIndices = [];
var header = this.props.renderHeader && this.props.renderHeader();
var footer = this.props.renderFooter && this.props.renderFooter();
var totalIndex = header ? 1 : 0;
for (var sectionIdx = 0; sectionIdx < allRowIDs.length; sectionIdx++) {
var sectionID = dataSource.sectionIdentities[sectionIdx];
var rowIDs = allRowIDs[sectionIdx];
if (rowIDs.length === 0) {
continue;
}
if (this.props.renderSectionHeader) {
var shouldUpdateHeader = rowCount >= this._prevRenderedRowsCount &&
dataSource.sectionHeaderShouldUpdate(sectionIdx);
bodyComponents.push(
<StaticRenderer
key={'s_' + sectionID}
shouldUpdate={!!shouldUpdateHeader}
render={this.props.renderSectionHeader.bind(
null,
dataSource.getSectionHeaderData(sectionIdx),
sectionID
)}
/>
);
sectionHeaderIndices.push(totalIndex++);
}
for (var rowIdx = 0; rowIdx < rowIDs.length; rowIdx++) {
var rowID = rowIDs[rowIdx];
var comboID = sectionID + '_' + rowID;
var shouldUpdateRow = rowCount >= this._prevRenderedRowsCount &&
dataSource.rowShouldUpdate(sectionIdx, rowIdx);
var key = 'r_' + comboID;
var row =
<StaticRenderer
key={key}
shouldUpdate={!!shouldUpdateRow}
render={this._renderRow.bind(
this,
key,
this.state.updateBatchId,
dataSource.getRowData(sectionIdx, rowIdx),
sectionID,
rowID,
this.onRowHighlighted
)}
/>;
bodyComponents.push(row);
totalIndex++;
if (this.props.renderSeparator &&
(rowIdx !== rowIDs.length - 1 || sectionIdx === allRowIDs.length - 1)) {
var adjacentRowHighlighted =
this.state.highlightedRow.sectionID === sectionID && (
this.state.highlightedRow.rowID === rowID ||
this.state.highlightedRow.rowID === rowIDs[rowIdx + 1]
);
var separator = this._renderSeparator(
this.state.updateBatchId,
sectionID,
rowID,
adjacentRowHighlighted
);
bodyComponents.push(separator);
totalIndex++;
}
if (++rowCount === this.state.curRenderedRowsCount) {
break;
}
}
if (rowCount >= this.state.curRenderedRowsCount) {
break;
}
}
var {
renderScrollComponent,
...props,
} = this.props;
if (!props.scrollEventThrottle) {
props.scrollEventThrottle = DEFAULT_SCROLL_CALLBACK_THROTTLE;
}
if (props.removeClippedSubviews === undefined) {
props.removeClippedSubviews = true;
}
Object.assign(props, {
onScroll: this._onScroll,
stickyHeaderIndices: this.props.stickyHeaderIndices.concat(sectionHeaderIndices),
// Do not pass these events downstream to ScrollView since they will be
// registered in ListView's own ScrollResponder.Mixin
onKeyboardWillShow: undefined,
onKeyboardWillHide: undefined,
onKeyboardDidShow: undefined,
onKeyboardDidHide: undefined,
});
// TODO(ide): Use function refs so we can compose with the scroll
// component's original ref instead of clobbering it
return React.cloneElement(renderScrollComponent(props), {
ref: SCROLLVIEW_REF,
onContentSizeChange: this._onContentSizeChange,
onLayout: this._onLayout,
}, header, bodyComponents, footer);
},
_renderSeparator(updateBatchId, sectionID, rowID, adjacentRowHighlighted) {
DEBUG && console.log({rowID, separator: true});
return (
<IncrementalSeparatorRenderer
key={'sp_' + rowID}
ref={view => { this._separatorRefs[rowID] = view; }}
synchronous={!this._hasRenderedInitialListSize}
isBatchComplete={this._isUpdatedBatchComplete(updateBatchId)}
render={this.props.renderSeparator.bind(null, sectionID, rowID, adjacentRowHighlighted)}
/>
);
},
_renderRow(key, updateBatchId, rowData, sectionID, rowID, onRowHighlighted) {
DEBUG && console.log({rowID});
return (
<IncrementalRowRenderer
isBatchComplete={this._isUpdatedBatchComplete(updateBatchId)}
onRender={() => { this._onRenderRowInBatch(updateBatchId) }}
rowID={rowID}
synchronous={!this._hasRenderedInitialListSize}
ref={view => { this._rowRefs[rowID] = view; }}
render={this.props.renderRow.bind(null, rowData, sectionID, rowID, onRowHighlighted)}
/>
);
},
_isUpdatedBatchComplete(updateBatchId) {
if (!this._updateBatches[updateBatchId]) {
return true;
}
return (
this._updateBatches[updateBatchId].complete ===
this._updateBatches[updateBatchId].rows
);
},
_onRenderRowInBatch(updateBatchId) {
if (!this._updateBatches[updateBatchId]) {
return;
}
this._updateBatches[updateBatchId].complete += 1;
DEBUG && console.log({updateBatchId, complete: this._updateBatches[updateBatchId].complete});
if (this._isUpdatedBatchComplete(updateBatchId)) {
DEBUG && console.log({finished: updateBatchId});
this._presentBatch(updateBatchId);
this.props.onPresentBatch && this.props.onPresentBatch();
}
},
_presentBatch(updateBatchId) {
let {
rows,
firstRow,
} = this._updateBatches[updateBatchId];
for (var i = firstRow; i <= firstRow + rows; i++) {
this._rowRefs[i] && this._rowRefs[i].batchIsComplete();
this._separatorRefs[i] && this._separatorRefs[i].batchIsComplete();
}
},
/**
* Private methods
*/
_measureAndUpdateScrollProps: function() {
var scrollComponent = this.getScrollResponder();
if (!scrollComponent || !scrollComponent.getInnerViewNode) {
return;
}
// RCTScrollViewManager.calculateChildFrames is not available on
// every platform
RCTScrollViewManager && RCTScrollViewManager.calculateChildFrames &&
RCTScrollViewManager.calculateChildFrames(
React.findNodeHandle(scrollComponent),
this._updateVisibleRows,
);
},
_onContentSizeChange: function(width, height) {
var contentLength = !this.props.horizontal ? height : width;
if (contentLength !== this.scrollProperties.contentLength) {
this.scrollProperties.contentLength = contentLength;
this._updateVisibleRows();
this._renderMoreRowsIfNeeded();
}
this.props.onContentSizeChange && this.props.onContentSizeChange(width, height);
},
_onLayout: function(event) {
var {width, height} = event.nativeEvent.layout;
var visibleLength = !this.props.horizontal ? height : width;
if (visibleLength !== this.scrollProperties.visibleLength) {
this.scrollProperties.visibleLength = visibleLength;
this._updateVisibleRows();
this._renderMoreRowsIfNeeded();
}
this.props.onLayout && this.props.onLayout(event);
},
_maybeCallOnEndReached: function(event) {
if (this.props.onEndReached &&
this.scrollProperties.contentLength !== this._sentEndForContentLength &&
this._getDistanceFromEnd(this.scrollProperties) < this.props.onEndReachedThreshold &&
this.state.curRenderedRowsCount === this.props.dataSource.getRowCount()) {
this._sentEndForContentLength = this.scrollProperties.contentLength;
this.props.onEndReached(event);
return true;
}
return false;
},
_renderMoreRowsIfNeeded: function() {
if (this.scrollProperties.contentLength === null ||
this.scrollProperties.visibleLength === null ||
this.state.curRenderedRowsCount === this.props.dataSource.getRowCount()) {
this._maybeCallOnEndReached();
return;
}
var distanceFromEnd = this._getDistanceFromEnd(this.scrollProperties);
if (distanceFromEnd < this.props.scrollRenderAheadDistance) {
this._pageInNewRows();
}
},
_pageInNewRows: function(props = this.props) {
if (this._isUpdatedBatchComplete(this.state.updateBatchId)) {
DEBUG && console.log({updateBatchId: this.state.updateBatchId});
var rowsToRender;
var actualPageSize;
var updateBatchId = this.state.updateBatchId;
if (!this._hasRenderedInitialListSize) {
actualPageSize = rowsToRender = Math.min(
props.initialListSize,
props.dataSource.getRowCount()
);
} else {
rowsToRender = Math.min(
this.state.curRenderedRowsCount + props.pageSize,
props.dataSource.getRowCount()
);
actualPageSize = rowsToRender - this.state.curRenderedRowsCount;
}
if (actualPageSize > 0) {
updateBatchId += 1;
this._updateBatches[updateBatchId] = {
complete: 0,
firstRow: this.state.curRenderedRowsCount,
rows: actualPageSize,
};
this.setState((state, props) => {
if (this._resetRowCount) {
this._prevRenderedRowsCount = 0;
this._resetRowCount = false;
} else {
this._prevRenderedRowsCount = state.curRenderedRowsCount;
}
return {
curRenderedRowsCount: rowsToRender,
updateBatchId,
};
}, () => {
if (!this._hasRenderedInitialListSize) {
this._hasRenderedInitialListSize = true;
this._updateBatches[updateBatchId].complete = this._updateBatches[updateBatchId].rows;
}
this._measureAndUpdateScrollProps();
this._prevRenderedRowsCount = this.state.curRenderedRowsCount;
});
}
} else {
if (!this._pageInTimeout) {
this._pageInTimeout = setTimeout(() => {
this._pageInNewRows();
this._pageInTimeout = null;
}, 100);
}
}
},
_getDistanceFromEnd: function(scrollProperties) {
var maxLength = Math.max(
scrollProperties.contentLength,
scrollProperties.visibleLength
);
return maxLength - scrollProperties.visibleLength - scrollProperties.offset;
},
_updateVisibleRows: function(updatedFrames) {
if (!this.props.onChangeVisibleRows) {
return; // No need to compute visible rows if there is no callback
}
if (updatedFrames) {
updatedFrames.forEach((newFrame) => {
this._childFrames[newFrame.index] = merge(newFrame);
});
}
var isVertical = !this.props.horizontal;
var dataSource = this.props.dataSource;
var visibleMin = this.scrollProperties.offset;
var visibleMax = visibleMin + this.scrollProperties.visibleLength;
var allRowIDs = dataSource.rowIdentities;
var header = this.props.renderHeader && this.props.renderHeader();
var totalIndex = header ? 1 : 0;
var visibilityChanged = false;
var changedRows = {};
for (var sectionIdx = 0; sectionIdx < allRowIDs.length; sectionIdx++) {
var rowIDs = allRowIDs[sectionIdx];
if (rowIDs.length === 0) {
continue;
}
var sectionID = dataSource.sectionIdentities[sectionIdx];
if (this.props.renderSectionHeader) {
totalIndex++;
}
var visibleSection = this._visibleRows[sectionID];
if (!visibleSection) {
visibleSection = {};
}
for (var rowIdx = 0; rowIdx < rowIDs.length; rowIdx++) {
var rowID = rowIDs[rowIdx];
var frame = this._childFrames[totalIndex];
totalIndex++;
if (!frame) {
break;
}
var rowVisible = visibleSection[rowID];
var min = isVertical ? frame.y : frame.x;
var max = min + (isVertical ? frame.height : frame.width);
if (min > visibleMax || max < visibleMin) {
if (rowVisible) {
visibilityChanged = true;
delete visibleSection[rowID];
if (!changedRows[sectionID]) {
changedRows[sectionID] = {};
}
changedRows[sectionID][rowID] = false;
}
} else if (!rowVisible) {
visibilityChanged = true;
visibleSection[rowID] = true;
if (!changedRows[sectionID]) {
changedRows[sectionID] = {};
}
changedRows[sectionID][rowID] = true;
}
}
if (!isEmpty(visibleSection)) {
this._visibleRows[sectionID] = visibleSection;
} else if (this._visibleRows[sectionID]) {
delete this._visibleRows[sectionID];
}
}
visibilityChanged && this.props.onChangeVisibleRows(this._visibleRows, changedRows);
},
_onScroll: function(e) {
var isVertical = !this.props.horizontal;
this.scrollProperties.visibleLength = e.nativeEvent.layoutMeasurement[
isVertical ? 'height' : 'width'
];
this.scrollProperties.contentLength = e.nativeEvent.contentSize[
isVertical ? 'height' : 'width'
];
this.scrollProperties.offset = e.nativeEvent.contentOffset[
isVertical ? 'y' : 'x'
];
this._updateVisibleRows(e.nativeEvent.updatedChildFrames);
if (!this._maybeCallOnEndReached(e)) {
this._renderMoreRowsIfNeeded();
}
if (this.props.onEndReached &&
this._getDistanceFromEnd(this.scrollProperties) > this.props.onEndReachedThreshold) {
// Scrolled out of the end zone, so it should be able to trigger again.
this._sentEndForContentLength = null;
}
this.props.onScroll && this.props.onScroll(e);
},
});
class IncrementalSeparatorRenderer extends React.Component {
render() {
let {
isBatchComplete,
synchronous,
} = this.props;
return (
<View
ref={view => { this._view = view; }}
style={isBatchComplete || synchronous ? {} : {position: 'absolute', left: 0, right: 0, opacity: 0}}>
{this.props.render()}
</View>
);
}
batchIsComplete() {
if (this._view) {
this._view.setNativeProps({style: {position: 'relative', opacity: 1}});
}
}
}
class IncrementalRowRenderer extends React.Component {
constructor(props) {
super(props);
this.state = {
shouldRender: this.props.synchronous,
isBatchComplete: this.props.isBatchComplete,
};
}
batchIsComplete() {
if (this._view) {
this._view.setNativeProps({style: {position: 'relative', opacity: 1}});
}
}
componentDidMount() {
this._scheduleRender();
}
render() {
if (this.state.shouldRender) {
return (
<View
ref={view => { this._view = view; }}
style={this.state.isBatchComplete || this.props.synchronous ? {} : {position: 'absolute', left: 0, right: 0, opacity: 0}}>
{this.props.render()}
</View>
);
} else {
return null;
}
}
_scheduleRender() {
InteractionManager.runAfterInteractions({
name: 'FeedView row: ' + this.props.rowID,
gen: () => new Promise(resolve => {
this.setState({shouldRender: true}, resolve);
}),
}).then(() => {
this.props.onRender && this.props.onRender();
});
}
}
module.exports = FeedView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment