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

[VirtualizedSectionList] - Externalise and handle any sort of data blob #20787

Closed
wants to merge 15 commits into from

Conversation

magrinj
Copy link
Contributor

@magrinj magrinj commented Aug 22, 2018

Summary

Fixes #20770

Test Plan:

I test this class in my project with immutable datas, and it's perfectly working:

                    <VirtualizedSectionList
                        sections={ sections }
                        renderItem={ this._renderItem }
                        renderSectionHeader={ this._renderSectionHeader }
                        getItem={ this.getItem }
                        getItemCount={ this.getItemCount }
                        keyExtractor={ this.keyExtractor }
                        ItemSeparatorComponent={ this._renderSeparator }
                    />

    getItem(data, key) {
        return data.get(key);
    }

    getItemCount(items) {
        return items.size || 0;
    }

    keyExtractor(item, index) {
        return String(index);
    }

Changelog:

[General] [Changed] - Externalise and handle any sort of data blob

In this PR I externalise VirtualizedSectionList and make it working with any sort of data blob, so sections can be an Immutable Map by exemple.
I do this to follow the logic of VirtualizedList.
Hoping to have made the changes correctly!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 22, 2018
@react-native-bot react-native-bot added 🔶Lists Missing Changelog This PR appears to be missing a changelog, or they are incorrectly formatted. labels Aug 22, 2018
@hramos hramos requested a review from sahrens August 22, 2018 18:44
Copy link
Contributor

@sahrens sahrens left a comment

Choose a reason for hiding this comment

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

Cool, thanks for doing this. I'll need to do some pretty extensive testing to make sure everything works before importing, but hopefully it won't take too long.

Libraries/Lists/VirtualizedSectionList.js Outdated Show resolved Hide resolved
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

sahrens has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

sahrens has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@sahrens sahrens left a comment

Choose a reason for hiding this comment

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

The CircleCI failures look quite relevant too - can you fix those?

@magrinj
Copy link
Contributor Author

magrinj commented Aug 24, 2018

Thanks for the review @sahrens, I take a look on the test failure, and try to solve it!

@magrinj
Copy link
Contributor Author

magrinj commented Aug 27, 2018

@sahrens From what I've read the flow errors due to getItem, getItemCount and getItemParam are due because the props are inherited from parent to children, and some are no longer required in child. I saw a comment added to fix this error in VirtualizedList by Gabe Levi in version 0.44.
So I did the same thing, adding this comment:

/* $FlowFixMe - this Props seems to be missing a bunch of stuff. Remove this
 * comment to see the errors */

on the top of Props declaration.
I think for the flow inheritance of these classes, it's better to only inherit of OptionalProps by exporting them, what do you think about that ?

@magrinj
Copy link
Contributor Author

magrinj commented Aug 27, 2018

@sahrens I also try to implement basic test for VirtualizedSectionList, let me know if you don't want it

@sahrens
Copy link
Contributor

sahrens commented Aug 27, 2018

I think exporting the flow types is a good way to go - gives much more control. Do you want to do that in this PR or separately?

Copy link
Contributor

@sahrens sahrens left a comment

Choose a reason for hiding this comment

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

This is looking great, just a few more requested changes - sorry I didn't catch them in the first round.

Libraries/Lists/VirtualizedSectionList.js Outdated Show resolved Hide resolved
Libraries/Lists/VirtualizedSectionList.js Outdated Show resolved Hide resolved
Libraries/Lists/SectionList.js Outdated Show resolved Hide resolved
@sahrens
Copy link
Contributor

sahrens commented Aug 27, 2018

And yes, thank you for adding the test :)

@magrinj
Copy link
Contributor Author

magrinj commented Aug 28, 2018

@sahrens I made the changes, but not for the type of the props, I think it's better to be done in another PR, I try to implement it, but a lot of other errors comes out, like onScroll that is not declared anywhere in Props, but used in the the classes.
I will try to submit another PR later to achieve this :)
By the way, I hope this one is good for you, and that the changes you requested are being properly implemented.

@magrinj
Copy link
Contributor Author

magrinj commented Sep 4, 2018

@sahrens My last commit as you request is ok for you ? :)

@magrinj
Copy link
Contributor Author

magrinj commented Oct 31, 2018

@sahrens Any news on this ?
@javache @nicklockwood

@hramos
Copy link
Contributor

hramos commented Jan 15, 2019

The changes look good. I was about to merge this, but it looks like some conflicts need to be resolved first. Sorry for the trouble!

@hramos hramos removed the Missing Changelog This PR appears to be missing a changelog, or they are incorrectly formatted. label Feb 26, 2019
@hramos hramos changed the title [INTERNAL][ENHANCEMENT][VirtualizedSectionList] - Externalise and handle any sort of data blob [VirtualizedSectionList] - Externalise and handle any sort of data blob Feb 26, 2019
@magrinj
Copy link
Contributor Author

magrinj commented Feb 27, 2019

@cpojer So I remove getItemParam prop, since getItem should be enough to handle any sort of data blob, I update the tests in consequence.
I think I've also fix the flow errors, I'm not sure if I did it in a good way, but the errors disappear :)

Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • eslint found some issues.

Libraries/Lists/VirtualizedSectionList.js Show resolved Hide resolved
Libraries/Lists/VirtualizedSectionList.js Show resolved Hide resolved
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

// HeaderComponent?: ?ReactClass<any>,
// onViewableItemsChanged?: ({viewableItems: Array<ViewToken>, changed: Array<ViewToken>}) => void,
};
type SectionBase = Object;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a regression. Could you work on putting back the proper Flow Type and running flow on the react-native repo to verify it?

Copy link
Contributor Author

@magrinj magrinj Feb 28, 2019

Choose a reason for hiding this comment

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

The SectionBase type can't be specified, because it can handle different type of datas, like classic js object or Immutable object, by exemple the data type of VirtualizedList is any.
For SectionList component SectionBase can be specified because we know the data must be javascript object.

@@ -115,7 +91,8 @@ type OptionalProps<SectionT: SectionBase> = {
*/
refreshing?: ?boolean,
};

/* $FlowFixMe - this Props seems to be missing a bunch of stuff. Remove this
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we fix this instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not really an expert with complex flow typing, I can try to remove this, but I'm not sure if I'm going to fix something of fix it in a good way :/

@cpojer
Copy link
Contributor

cpojer commented Feb 28, 2019

I think this looks good but I don't feel comfortable with relaxing the flow types. If you can fix that, I'm happy to land this PR.

Overall, I think it would be great if we could change this API to use an iterator protocol, but I assume that is something to discuss later.

@magrinj
Copy link
Contributor Author

magrinj commented Feb 28, 2019

@cpojer I answer in the comment above for the flow type :)

For the iterator it's a good idea, that's should work very well for VirtualizedList, but in VirtualizedSectionList some data need to be extract from item, and an iterator protocol wouldn't be enough, I think.

@cpojer
Copy link
Contributor

cpojer commented Apr 17, 2019

If you can rebase this PR, I can take a look at the flow types.

@magrinj
Copy link
Contributor Author

magrinj commented Apr 18, 2019

@cpojer Thanks ! :) I update the branch to the current master and fix the conflicts

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

if (!sections) {
return null;
}
let itemIdx = index - 1;
for (let ii = 0; ii < sections.length; ii++) {
if (itemIdx === -1 || itemIdx === sections[ii].data.length) {
for (let ii = 0; ii < props.getItemCount(sections); ii++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Working on this at FB. I think this is supposed to stay sections.length here because sections is always an array. I'm changing this internally, no need to do anything, just wanted to put this comment here for your information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cpojer Thanks ! In fact it can be something else, by exemple if you use an immutable list, to get his length the method is size and not length so it's better to keep it outside to handle immutable data

@cpojer
Copy link
Contributor

cpojer commented Apr 18, 2019

Hey @magrinj,

I'm about to land this change from FB's side after fixing up a few things. It seems that there were a bunch of bugs in the code that I fixed, among them the tests for handles separators correctly were not actually working properly but they are now.

I made a larger change to remove getItem and getItemCount calls on sections itself altogether. I think we want to be able to use custom data structure for section.data but I prefer keeping sections as arrays themselves. This should give you most of the value that you were looking for without adding too much dynamism (one getItem function to be called on different data structures, which is hard to type and prone to errors). I hope that works for you :)

@magrinj
Copy link
Contributor Author

magrinj commented Apr 18, 2019

@cpojer Thanks a lot ! Sorry for the bugs, I didn't have time to test everything again between the different merges. I understand that you want to keep sections as a basic javascript array, it will be more easy to maintain in the future, and customisation on section.data should be enough for me :)
Again thank you !

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @magrinj in 1946aee.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added Merged This PR has been merged. and removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Apr 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Component: VirtualizedList Merged This PR has been merged. Type: Breaking Change💥
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VirtualizedSectionList not exposed
7 participants