Introducing switchPartition: A Versatile RxJS Operator for Stream Partitioning #7355
Replies: 2 comments 2 replies
-
It's an interesting operator and I can see the effort, but I think it has some shortcomings. Please, don't take this as destructive critsism 🙏
In React-RxJS we have done one operator that deals with this, published into a separate package It doesn't discriminate union types, but it has semantics defined on when a group becomes alive and when it disappears. The way this works is that you access the group through a function. Your example on PRs would become: const [prByType$] = partitionByKey(
pullRequest$,
pr => pr.type;
);
// Later, when consuming
prByType$('Feature').pipe(...) It returns that function within a tuple, because it also returns an additional observable that emits which keys become active or which ones get removed, for cases where you don't know the keys beforehand (e.g. you want to have a partition by User ID) |
Beta Was this translation helpful? Give feedback.
-
I think my feature request is very similair #7420. Mine started out with the idea of partition on base of index returned but later added support for a string array as well. |
Beta Was this translation helpful? Give feedback.
-
The switchPartition operator aims to extend the capabilities of RxJS by providing a more flexible and type-safe way to partition an observable stream into multiple observables based on specified cases. This operator is particularly useful in scenarios where you need to segregate a stream of events or data into different categories for further processing or side-effects.
For example, consider a stream of vehicles that could be Electric, Hybrid, Gasoline, or Diesel. Using switchPartition, you can easily create four separate streams for each type of vehicle. This makes it easier to handle different logic for each category without having to filter the same original stream multiple times.
The operator also supports an optional projection function, allowing for more complex use-cases, such as partitioning based on a property of the emitted values.
The switchPartition operator is designed to be highly type-safe, ensuring that the types of the emitted values in the partitioned observables match the types specified in the cases array. This makes it a robust and valuable addition to the RxJS library.
Beta Was this translation helpful? Give feedback.
All reactions