chore(utils): flip order of returned tuple of partitionByKey

This commit is contained in:
Josep M Sobrepere 2021-03-30 23:01:03 +02:00
parent 957c4e5346
commit 96a397fbf1
2 changed files with 8 additions and 8 deletions

View File

@ -14,7 +14,7 @@ describe("partitionByKey", () => {
scheduler().run(({ expectObservable, cold }) => {
const source = cold("-ab---cd---")
const expectedStr = "efg---hi---"
const [result] = partitionByKey(
const [, result] = partitionByKey(
source,
(v) => v,
() => NEVER,
@ -38,7 +38,7 @@ describe("partitionByKey", () => {
const c = cold(" 1-|")
const expectedStr = "efg--hi-j"
const innerStreams = { a, b, c }
const [result] = partitionByKey(
const [, result] = partitionByKey(
source,
(v) => v,
(v$) =>
@ -72,7 +72,7 @@ describe("partitionByKey", () => {
const expectC = " ------1-|"
const innerStreams = { a, b, c }
const [, getInstance$] = partitionByKey(
const [getInstance$] = partitionByKey(
source,
(v) => v,
(v$) =>
@ -91,7 +91,7 @@ describe("partitionByKey", () => {
it("replays the latest value for each key", () => {
const source$ = new Subject<string>()
const inner$ = new Subject<number>()
const [, getInstance$] = partitionByKey(
const [getInstance$] = partitionByKey(
source$,
(v) => v,
() => inner$,

View File

@ -10,17 +10,17 @@ import { collect, getGroupedObservable, split } from "./"
* @param keySelector Function that specifies the key for each element in `stream`
* @param streamSelector Function to apply to each resulting group
* @returns [1, 2]
* 1. A stream with the list of active keys
* 2. A function that accepts a key and returns the stream for the group of that key.
* 1. A function that accepts a key and returns the stream for the group of that key.
* 2. A stream with the list of active keys
*/
export function partitionByKey<T, K, R>(
stream: Observable<T>,
keySelector: (value: T) => K,
streamSelector: (grouped: Observable<T>, key: K) => Observable<R>,
): [Observable<K[]>, (key: K) => GroupedObservable<K, R>] {
): [(key: K) => GroupedObservable<K, R>, Observable<K[]>] {
const source$ = stream.pipe(split(keySelector, streamSelector), collect())
return [
source$.pipe(map((x) => Array.from(x.keys()))),
(key: K) => getGroupedObservable(source$, key),
source$.pipe(map((x) => Array.from(x.keys()))),
]
}