Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix ensure reference to getStableId is updated #348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/core/RecyclerListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,14 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends

constructor(props: P, context?: any) {
super(props, context);
this._virtualRenderer = new VirtualRenderer(this._renderStackWhenReady, (offset) => {
this._pendingScrollToOffset = offset;
}, (index) => {
return this.props.dataProvider.getStableId(index);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to do what you expect. This inline function seems to be permanently bound to the first dataProvider.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think another way to look at this is stableId inside virtualRenderer is updated regardless if the inline method permanently bound it or not, therefore the inline method has no effect. I agree this change is 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem here is the fact that look up will happen in old dataProvider till react lifecycle is completed. Only post that this.props will point to the new one.

}, !props.disableRecycling);
this._virtualRenderer = new VirtualRenderer(
this._renderStackWhenReady,
(offset) => {
this._pendingScrollToOffset = offset;
},
this.props.dataProvider.getStableId,
!props.disableRecycling,
);

this.state = {
renderStack: {},
Expand Down Expand Up @@ -367,9 +370,19 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
this._params.itemCount = newProps.dataProvider.getSize();
this._virtualRenderer.setParamsAndDimensions(this._params, this._layout);
this._virtualRenderer.setLayoutProvider(newProps.layoutProvider);
if (newProps.dataProvider.hasStableIds() && this.props.dataProvider !== newProps.dataProvider && newProps.dataProvider.requiresDataChangeHandling()) {
Copy link

@kwketh kwketh May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a small snippet to verify that this change is necessary.

On this line 370, requiresDataChangeHandling() value returns false (but should be true) when:

new DataProvider((a, b) => a !== b)
  .cloneWithRows([1, 2])
  .cloneWithRows([1, 2, 3])
  .requiresDataChangeHandling() // returns false

or

new DataProvider((a, b) => a !== b)
    .cloneWithRows([])
    .cloneWithRows([1, 2, 3])
    .requiresDataChangeHandling() // returns false

(where result of first clone is the initial data provider and second is the next data provider)

In both instances, handleDataSetChange it not called and virtual renderer will continue using stable id method from the previous data provider, and so getStableId(2) would be out of bounds.

This potentially means another patch is required inside cloneWithRows implementation, however regardless of where the bug originated from, I agree that updating stableId method is always necessary when data provider has changed.

I could have misunderstood the purpose of requiresDataChangeHandling, but I assume cloning data provider with new items inserted, it should return true and end up calling handleDataSetChange?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requiresDataChangeHandling is an optimization to avoid managing recycling keys in case of pagination where items are just getting added and no modification is there in existing ones.


const hasStableIds = newProps.dataProvider.hasStableIds();
const isNewDataProvider = this.props.dataProvider !== newProps.dataProvider;
const requiresDataChangeHandling = newProps.dataProvider.requiresDataChangeHandling();
if (hasStableIds && isNewDataProvider && requiresDataChangeHandling) {
this._virtualRenderer.handleDataSetChange(newProps.dataProvider, this.props.optimizeForInsertDeleteAnimations);
}

if (hasStableIds && isNewDataProvider) {
// Future calls to get stable IDs must use the function provided by the new data provider.
this._virtualRenderer.setStableIdProvider(newProps.dataProvider.getStableId);
}

if (forceFullRender || this.props.layoutProvider !== newProps.layoutProvider || this.props.isHorizontal !== newProps.isHorizontal) {
//TODO:Talha use old layout manager
this._virtualRenderer.setLayoutManager(newProps.layoutProvider.newLayoutManager(this._layout, newProps.isHorizontal));
Expand Down
4 changes: 4 additions & 0 deletions src/core/VirtualRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ export default class VirtualRenderer {
}
}

public setStableIdProvider(getStableId: StableIdProvider): void {
this._fetchStableId = getStableId;
}

private _getCollisionAvoidingKey(): string {
return "#" + this._startKey++ + "_rlv_c";
}
Expand Down