most-merge-select
mostMergeSelect(selectorFunction, factoryFunction, stream) -> Stream
Create a new stream by merging streams created at runtime. A selector function is used to find the appropriate stream to receive a new event. A factory function is used to create a new stream if one is not found.
Use case
When you want to merge a bunch of streams together but you don't know what those streams are before hand.
Usage
import { from, scan, skip } from 'most';
import mergeSelect from 'most-merge-select';
const event$ = from([
{ groupId: 1 },
{ groupId: 2 },
{ groupId: 1 },
{ groupId: 2 },
]);
const count = (acc, x) => ({
groupId: x.groupId,
count: acc.count + 1,
});
const eventGroupCount$ = mergeSelect(
event => event.groupId,
eventByGroup$ => skip(1, scan(count, { count: 0 }, eventByGroup$)),
event$,
);
// { groupId: 1, count: 1 }
// { groupId: 2, count: 1 }
// { groupId: 1, count: 2 }
// { groupId: 2, count: 2 }
eventGroupCount$.subscribe({
next: x => console.log(x),
});