This is a bit of a convoluted example, but I was wondering how to do the following in RxJs: After a certain part in the pipeline, feed the value back into an earlier part of the pipeline, so it will get processed again.
of(1, 2, 3, 4, 5).pipe(
map(x => x + 1),
map(x => x + 1),
filter(x => x % 2),
// refeed after the first map but before the second
)
Here's an example of how you might use expand to do this:
function isEven(x) {
return x % 2 == 0
}
of(1, 2, 3, 4, 5).pipe(
map(x => x + 1),
expand(x => of(x).pipe(
map(x => x + 1),
filter(isEven)
))
);
Here's how you might write a recursive function to do this:
function isEven(x) {
return x % 2 == 0
}
function loopBack(x : number) {
return of(x).pipe(
map(x => x + 1),
filter(isEven),
mergeMap(loopBack),
startWith(x)
);
}
of(1, 2, 3, 4, 5).pipe(
map(x => x + 1),
mergeMap(loopBack),
);