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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FlatScan(First/Latest) operators #257

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

jegnux
Copy link
Member

@jegnux jegnux commented Nov 23, 2020

Hi community!

Here's a new Rx operator I made for a specific use case, but I've seen myself using it for more use cases so I guess it was a good candidate for this repository.

Basically, flatScan is a scan operator that returns an Observable<T> instead of just T and flatten the result. It's declined in flatScanFirst and flatScanLatest versions that works the same as their flatMap counterparts.

The most useful use case is when working with paginated content when you want to make a new API request with an "offset" and append your new results to current local results. Something like that:

loadMoreTrigger
    .withLatestFrom(searchQuery)
    .flatScan(SearchResults()) { previous, query in
        searchResults(
            for: query,
            start: previous.elements.count,
            count: 20
        )
        .map(previous.merging)
    }

I know I should have discussed this operator in an issue first but the code was already existing in my project but without unit tests (馃槺) and working on a PR directly encouraged me to write unit tests for it. If you feel like this operator doesn't fit in this repository I wouldn't mind closing this PR. At least I'll be able to add the tests to my project 馃槆

PS: In the second commit I fixed swift lint issues unrelated to the flatScan operator. Feel free to focus your review on the first commit 7332d10 馃槈

@jegnux jegnux requested a review from freak4pc November 23, 2020 09:59
@jegnux
Copy link
Member Author

jegnux commented Nov 23, 2020

Looks like Github automatically closed #254 when renaming master branch to main, so I'm reopening it here 馃槈

@danielt1263
Copy link

danielt1263 commented Dec 18, 2020

This is a great way to handle the very common use case of paginated data coming from a server. The basic flatScan is probably never what you want though, since you run the risk of loosing data if your inputs fire while your requests are in flight, I think that would surprise developers with less experienced with the Rx ecosystem. I have copied the code into one of my projects already and look forward to this being approved.

@jegnux
Copy link
Member Author

jegnux commented Dec 18, 2020

The basic flatScan is probably never what you want though

@danielt1263 How do you think it differs from this same issue but with flatMap? Because I almost never use the default flatMat. Most of the time I use flatMapLatest and sometimes flatMapFirst.

@danielt1263
Copy link

The issue doesn't exist with flatMap because there is no chance of loosing data in that case like there is with flatScan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants