Skip to content

Commit d3fb725

Browse files
committed
fix(utils): combineKeys allow for reentrant keys
1 parent c6b4010 commit d3fb725

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/utils/src/combineKeys.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,32 @@ describe("combineKeys", () => {
166166
})
167167
})
168168
})
169+
170+
it("accounts for reentrant keys", () => {
171+
scheduler().run(({ expectObservable, cold }) => {
172+
const activeKeys = {
173+
a: ["a"],
174+
b: ["a", "b"],
175+
z: [],
176+
}
177+
178+
const keys = cold(" abzab", activeKeys)
179+
const a = cold(" 1----")
180+
const b = cold(" 2---")
181+
const expectedStr = "efgef"
182+
183+
const innerStreams = { a, b }
184+
185+
const result = combineKeys(
186+
keys,
187+
(v): Observable<string> => innerStreams[v],
188+
).pipe(map((x) => Object.fromEntries(x.entries())))
189+
190+
expectObservable(result).toBe(expectedStr, {
191+
e: { a: "1" },
192+
f: { a: "1", b: "2" },
193+
g: {},
194+
})
195+
})
196+
})
169197
})

packages/utils/src/combineKeys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const combineKeys = <K, T>(
2727
innerSubscriptions.forEach((sub, key) => {
2828
if (!nextKeys.has(key)) {
2929
sub.unsubscribe()
30+
innerSubscriptions.delete(key)
3031
if (currentValue.has(key)) {
3132
changes = true
3233
currentValue.delete(key)

0 commit comments

Comments
 (0)