ReactiveX/rxjs

Deprecate partition, add splitBy method that multicasts the source sequence

trxcllnt opened this issue ยท 6 comments

Related to #3797, the ideal replacement would be a method that mutlicasts the source sequence and returns two Observable "lanes", and sends values down one or the other lane based on the predicate. This is conceptually similar to groupBy with only two groups, except it returns an Array of the grouped Observables instead of an Observable of the groups since the groups can be known ahead of time.

Seconded. To my mind, the partition operator is not fully useful yet: There needs to be another one that does not subscribe twice.

For trivial examples like in the docs, it works totally fine. Behind the scenes however, it subscribes to the observable behind partition twice which might double the work the code does.

In my concrete case, I followed the docs to split success and error cases. However, instead of calculating if a number is odd or even, I did an API call (with flatMap / catchError in case anything goes wrong). In my opinion, this is quite a common use case. If you now partition this by success / failure and subscribe to both partitioned streams, the API call is actually performed twice because there are two subscriptions that are merged back into one. This might be undesirable because it could double the load on the server (if there is no caching in place).

For this scenario, I might be missing a better operator. In this case, I am sorry. However, the splitBy that is described here seems like a perfect match for the situation I'm facing. As a workaround, i am using a custom operator that uses two subjects and passes elements on correctly, but I doubt this is production-ready yet.

FWIW, I implemented this, but never got around to discussing it or opening a PR: https://github.com/cartant/rxjs-etc/blob/master/source/operators/splitBy.ts

That looks very promising, thanks. Maybe it would be a good idea to merge it back ๐Ÿ˜„

Would be good to get splitBy into rxjs.

Core Team Meeting: Have @cartant move this into core, whenever he gets the chance. It seems like there's a good deal of interest.

Okay, after a PR and some careful deliberation, we decided we aren't going to pursue getting this into core right now. For reasons that are outlined here: #6388 (comment)