2

I'm trying to download data from server in chunks. I don't know how many iterations there will be, I just have to get chunks until the server responds with "no-more-data".

In v4 there was doWhile operator but it was experimental and is removed in latest version.

There is a similar question (though I don't think this is a duplicate!) that suggests using expand but I'm not sure if it is still the best or only way to do it in v5.

Or maybe I'm solving the wrong problem to begin with?... I.e. is it a good practice to use RxJs in such scenarios, or am I discouraged to use it this way (e.g. using expand operator to achieve recursion, assuming no better way exists)???

If this is not something unsuitable to Reactive Extensions style in general, why did they remove the doWhile operator? (It's not even on any roadmap)

Community
  • 1
  • 1
Titan
  • 2,875
  • 5
  • 23
  • 34
  • 1
    I'd suggest using `expand()` as well but it's hard to give any advice without any demo showing what exactly you're trying to solve. – martin Dec 28 '16 at 15:17
  • 1
    Note v5 is not a direct follow-on to v4 it was a complete re-implementation from the ground up. The fact that the operator doesn't exist may just mean that it wasn't yet ported or that it was deemed as unnecessarily bloating the API surface. That would likely be a question that could be pointed at the GitHub issues list. – paulpdaniels Dec 31 '16 at 04:19

1 Answers1

0

expand passes the input first, followed by itself as input. Repeat this process until there is no input(Observable.empty()).

I wrote the demonstration in TypeScript.

interface RSP {
    header:string;
    value:string;
};

function requestMaker(numChunk:number) {
    return () => {
        numChunk > 0 ? numChunk-- : 0;
        return Observable.of(numChunk > 0 ? ['CONT', 'value'] : ['END', 'value']).delay(300);
    }
}

let simulateRequest = requestMaker(5);

simulateRequest()
    .expand(([header, value]) => {
        if (header == 'CONT') {
            return simulateRequest();
        }
        else {
            return Observable.empty();
        }
    })
    .subscribe(
        ([header, value]) => console.log(`NEXT: ${header}-${value}`),
        error => console.log(`ERROR: ${error}`),
        () => console.log('completed')
    )

Result:

NEXT: CONT-value
NEXT: CONT-value
NEXT: CONT-value
NEXT: CONT-value
NEXT: END-value
completed
thatseeyou
  • 1,822
  • 13
  • 10