Skip to content

Commit 8342c7e

Browse files
committed
fix: prune empty part entries in shared reducer
1 parent ea0817e commit 8342c7e

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

packages/sdk/js/src/event-reducer.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export interface MessageStore {
3232
}
3333

3434
function upsertSorted<T>(array: T[], item: T, id: string, getId: (item: T) => string): T[] {
35-
const result = search(array, id, getId)
36-
const next = [...array]
35+
const next = array.filter((x): x is T => x !== undefined && x !== null)
36+
const result = search(next, id, getId)
3737
if (result.found) {
3838
next[result.index] = item
3939
} else {
@@ -43,9 +43,9 @@ function upsertSorted<T>(array: T[], item: T, id: string, getId: (item: T) => st
4343
}
4444

4545
function removeSorted<T>(array: T[], id: string, getId: (item: T) => string): T[] | null {
46-
const result = search(array, id, getId)
46+
const next = array.filter((x): x is T => x !== undefined && x !== null)
47+
const result = search(next, id, getId)
4748
if (!result.found) return null
48-
const next = [...array]
4949
next.splice(result.index, 1)
5050
return next
5151
}
@@ -118,7 +118,9 @@ export function applyMessageEvent(
118118
if (!parts) return null
119119
const next = removeSorted(parts, partID, (p) => (p as any).id)
120120
if (!next) return null
121-
return { ...store, parts: { ...store.parts, [messageID]: next } }
121+
if (next.length > 0) return { ...store, parts: { ...store.parts, [messageID]: next } }
122+
const { [messageID]: _, ...rest } = store.parts
123+
return { ...store, parts: rest }
122124
}
123125

124126
default:

0 commit comments

Comments
 (0)