/** * Add a resource to a list, or replace the copy already there. * * A list fed by both a save and a broadcast can receive the same record twice, in either order: the * server broadcasts as it writes, so its event can land before the save's own response. Matching by id * keeps one copy whichever arrives first. A resource with no id cannot match anything and is appended. */ export const upsertById = (list: T[], item: T): T[] => item.id !== undefined && list.some((entry) => entry.id === item.id) ? list.map((entry) => (entry.id === item.id ? item : entry)) : [...list, item];