Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
Reviewed By: fkgozali

Differential Revision: D5210279

fbshipit-source-id: 4b376a09b4bcfb42a2dc48b9903849d58929b7db
  • Loading branch information
sahrens authored and facebook-github-bot committed Jun 13, 2017
1 parent 63f7efc commit 0a3e6e0
Show file tree
Hide file tree
Showing 19 changed files with 1,328 additions and 826 deletions.
39 changes: 26 additions & 13 deletions Libraries/Lists/FillRateHelper.js
Expand Up @@ -8,6 +8,7 @@
*
* @providesModule FillRateHelper
* @flow
* @format
*/

/* eslint-disable no-console-disallow */
Expand Down Expand Up @@ -56,17 +57,15 @@ class FillRateHelper {
_mostlyBlankStartTime = (null: ?number);
_samplesStartTime = (null: ?number);

static addListener(
callback: (FillRateInfo) => void
): {remove: () => void} {
static addListener(callback: FillRateInfo => void): {remove: () => void} {
warning(
_sampleRate !== null,
'Call `FillRateHelper.setSampleRate` before `addListener`.'
'Call `FillRateHelper.setSampleRate` before `addListener`.',
);
_listeners.push(callback);
return {
remove: () => {
_listeners = _listeners.filter((listener) => callback !== listener);
_listeners = _listeners.filter(listener => callback !== listener);
},
};
}
Expand Down Expand Up @@ -98,7 +97,8 @@ class FillRateHelper {
}
const start = this._samplesStartTime; // const for flow
if (start == null) {
DEBUG && console.debug('FillRateHelper: bail on deactivate with no start time');
DEBUG &&
console.debug('FillRateHelper: bail on deactivate with no start time');
return;
}
if (this._info.sample_count < _minSampleCount) {
Expand All @@ -115,18 +115,21 @@ class FillRateHelper {
const derived = {
avg_blankness: this._info.pixels_blank / this._info.pixels_sampled,
avg_speed: this._info.pixels_scrolled / (total_time_spent / 1000),
avg_speed_when_any_blank: this._info.any_blank_speed_sum / this._info.any_blank_count,
any_blank_per_min: this._info.any_blank_count / (total_time_spent / 1000 / 60),
avg_speed_when_any_blank: this._info.any_blank_speed_sum /
this._info.any_blank_count,
any_blank_per_min: this._info.any_blank_count /
(total_time_spent / 1000 / 60),
any_blank_time_frac: this._info.any_blank_ms / total_time_spent,
mostly_blank_per_min: this._info.mostly_blank_count / (total_time_spent / 1000 / 60),
mostly_blank_per_min: this._info.mostly_blank_count /
(total_time_spent / 1000 / 60),
mostly_blank_time_frac: this._info.mostly_blank_ms / total_time_spent,
};
for (const key in derived) {
derived[key] = Math.round(1000 * derived[key]) / 1000;
}
console.debug('FillRateHelper deactivateAndFlush: ', {derived, info});
}
_listeners.forEach((listener) => listener(info));
_listeners.forEach(listener => listener(info));
this._resetData();
}

Expand All @@ -147,7 +150,11 @@ class FillRateHelper {
visibleLength: number,
},
): number {
if (!this._enabled || props.getItemCount(props.data) === 0 || this._samplesStartTime == null) {
if (
!this._enabled ||
props.getItemCount(props.data) === 0 ||
this._samplesStartTime == null
) {
return 0;
}
const {dOffset, offset, velocity, visibleLength} = scrollMetrics;
Expand Down Expand Up @@ -180,7 +187,10 @@ class FillRateHelper {
// Only count blankTop if we aren't rendering the first item, otherwise we will count the header
// as blank.
if (firstFrame && first > 0) {
blankTop = Math.min(visibleLength, Math.max(0, firstFrame.offset - offset));
blankTop = Math.min(
visibleLength,
Math.max(0, firstFrame.offset - offset),
);
}
let blankBottom = 0;
let last = state.last;
Expand All @@ -193,7 +203,10 @@ class FillRateHelper {
// footer as blank.
if (lastFrame && last < props.getItemCount(props.data) - 1) {
const bottomEdge = lastFrame.offset + lastFrame.length;
blankBottom = Math.min(visibleLength, Math.max(0, offset + visibleLength - bottomEdge));
blankBottom = Math.min(
visibleLength,
Math.max(0, offset + visibleLength - bottomEdge),
);
}
const pixels_blank = Math.round(blankTop + blankBottom);
const blankness = pixels_blank / visibleLength;
Expand Down
79 changes: 59 additions & 20 deletions Libraries/Lists/FlatList.js
Expand Up @@ -8,6 +8,7 @@
*
* @providesModule FlatList
* @flow
* @format
*/
'use strict';

Expand Down Expand Up @@ -109,8 +110,10 @@ type OptionalProps<ItemT> = {
* Remember to include separator length (height or width) in your offset calculation if you
* specify `ItemSeparatorComponent`.
*/
getItemLayout?: (data: ?Array<ItemT>, index: number) =>
{length: number, offset: number, index: number},
getItemLayout?: (
data: ?Array<ItemT>,
index: number,
) => {length: number, offset: number, index: number},
/**
* If true, renders items next to each other horizontally instead of stacked vertically.
*/
Expand Down Expand Up @@ -184,7 +187,9 @@ type OptionalProps<ItemT> = {
*/
viewabilityConfig?: ViewabilityConfig,
};
type Props<ItemT> = RequiredProps<ItemT> & OptionalProps<ItemT> & VirtualizedListProps;
type Props<ItemT> = RequiredProps<ItemT> &
OptionalProps<ItemT> &
VirtualizedListProps;

const defaultProps = {
...VirtualizedList.defaultProps,
Expand Down Expand Up @@ -295,7 +300,8 @@ type DefaultProps = typeof defaultProps;
* Alternatively, you can provide a custom `keyExtractor` prop.
*
*/
class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, void> {
class FlatList<ItemT>
extends React.PureComponent<DefaultProps, Props<ItemT>, void> {
static defaultProps: DefaultProps = defaultProps;
props: Props<ItemT>;
/**
Expand All @@ -314,7 +320,10 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
* `getItemLayout` prop.
*/
scrollToIndex(params: {
animated?: ?boolean, index: number, viewOffset?: number, viewPosition?: number,
animated?: ?boolean,
index: number,
viewOffset?: number,
viewPosition?: number,
}) {
this._listRef.scrollToIndex(params);
}
Expand All @@ -325,7 +334,11 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
* Note: cannot scroll to locations outside the render window without specifying the
* `getItemLayout` prop.
*/
scrollToItem(params: {animated?: ?boolean, item: ItemT, viewPosition?: number}) {
scrollToItem(params: {
animated?: ?boolean,
item: ItemT,
viewPosition?: number,
}) {
this._listRef.scrollToItem(params);
}

Expand Down Expand Up @@ -379,15 +392,17 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
invariant(
nextProps.numColumns === this.props.numColumns,
'Changing numColumns on the fly is not supported. Change the key prop on FlatList when ' +
'changing the number of columns to force a fresh render of the component.'
'changing the number of columns to force a fresh render of the component.',
);
this._checkProps(nextProps);
}

_hasWarnedLegacy = false;
_listRef: VirtualizedList;

_captureRef = (ref) => { this._listRef = ref; };
_captureRef = ref => {
this._listRef = ref;
};

_checkProps(props: Props<ItemT>) {
const {
Expand All @@ -398,20 +413,29 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
numColumns,
columnWrapperStyle,
} = props;
invariant(!getItem && !getItemCount, 'FlatList does not support custom data formats.');
invariant(
!getItem && !getItemCount,
'FlatList does not support custom data formats.',
);
if (numColumns > 1) {
invariant(!horizontal, 'numColumns does not support horizontal.');
} else {
invariant(!columnWrapperStyle, 'columnWrapperStyle not supported for single column lists');
invariant(
!columnWrapperStyle,
'columnWrapperStyle not supported for single column lists',
);
}
if (legacyImplementation) {
invariant(numColumns === 1, 'Legacy list does not support multiple columns.');
invariant(
numColumns === 1,
'Legacy list does not support multiple columns.',
);
// Warning: may not have full feature parity and is meant more for debugging and performance
// comparison.
if (!this._hasWarnedLegacy) {
console.warn(
'FlatList: Using legacyImplementation - some features not supported and performance ' +
'may suffer'
'may suffer',
);
this._hasWarnedLegacy = true;
}
Expand Down Expand Up @@ -442,10 +466,12 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
invariant(
Array.isArray(items),
'FlatList: Encountered internal consistency error, expected each item to consist of an ' +
'array with 1-%s columns; instead, received a single item.',
'array with 1-%s columns; instead, received a single item.',
numColumns,
);
return items.map((it, kk) => keyExtractor(it, index * numColumns + kk)).join(':');
return items
.map((it, kk) => keyExtractor(it, index * numColumns + kk))
.join(':');
} else {
return keyExtractor(items, index);
}
Expand All @@ -460,16 +486,18 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
});
}

_onViewableItemsChanged = (info) => {
_onViewableItemsChanged = info => {
const {numColumns, onViewableItemsChanged} = this.props;
if (!onViewableItemsChanged) {
return;
}
if (numColumns > 1) {
const changed = [];
const viewableItems = [];
info.viewableItems.forEach((v) => this._pushMultiColumnViewable(viewableItems, v));
info.changed.forEach((v) => this._pushMultiColumnViewable(changed, v));
info.viewableItems.forEach(v =>
this._pushMultiColumnViewable(viewableItems, v),
);
info.changed.forEach(v => this._pushMultiColumnViewable(changed, v));
onViewableItemsChanged({viewableItems, changed});
} else {
onViewableItemsChanged(info);
Expand All @@ -480,7 +508,10 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
const {renderItem, numColumns, columnWrapperStyle} = this.props;
if (numColumns > 1) {
const {item, index} = info;
invariant(Array.isArray(item), 'Expected array of items with numColumns > 1');
invariant(
Array.isArray(item),
'Expected array of items with numColumns > 1',
);
return (
<View style={[{flexDirection: 'row'}, columnWrapperStyle]}>
{item.map((it, kk) => {
Expand All @@ -500,7 +531,13 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo

render() {
if (this.props.legacyImplementation) {
return <MetroListView {...this.props} items={this.props.data} ref={this._captureRef} />;
return (
<MetroListView
{...this.props}
items={this.props.data}
ref={this._captureRef}
/>
);
} else {
return (
<VirtualizedList
Expand All @@ -510,7 +547,9 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
getItemCount={this._getItemCount}
keyExtractor={this._keyExtractor}
ref={this._captureRef}
onViewableItemsChanged={this.props.onViewableItemsChanged && this._onViewableItemsChanged}
onViewableItemsChanged={
this.props.onViewableItemsChanged && this._onViewableItemsChanged
}
/>
);
}
Expand Down

0 comments on commit 0a3e6e0

Please sign in to comment.