fix(utils): combineKeys allow for reentrant keys

This commit is contained in:
Josep M Sobrepere 2021-05-10 15:44:41 +02:00
parent c6b4010cd0
commit d3fb725015
2 changed files with 29 additions and 0 deletions

View File

@ -166,4 +166,32 @@ describe("combineKeys", () => {
})
})
})
it("accounts for reentrant keys", () => {
scheduler().run(({ expectObservable, cold }) => {
const activeKeys = {
a: ["a"],
b: ["a", "b"],
z: [],
}
const keys = cold(" abzab", activeKeys)
const a = cold(" 1----")
const b = cold(" 2---")
const expectedStr = "efgef"
const innerStreams = { a, b }
const result = combineKeys(
keys,
(v): Observable<string> => innerStreams[v],
).pipe(map((x) => Object.fromEntries(x.entries())))
expectObservable(result).toBe(expectedStr, {
e: { a: "1" },
f: { a: "1", b: "2" },
g: {},
})
})
})
})

View File

@ -27,6 +27,7 @@ export const combineKeys = <K, T>(
innerSubscriptions.forEach((sub, key) => {
if (!nextKeys.has(key)) {
sub.unsubscribe()
innerSubscriptions.delete(key)
if (currentValue.has(key)) {
changes = true
currentValue.delete(key)