| 12345678910111213141516171819202122232425 |
- /** Keep only identity-matched values when this refresh explicitly fails; EMPTY is authoritative. */
- export function retainEquipmentRefresh(previous, next, deviceCode) {
- if (!previous?.data || previous.data.deviceCode !== deviceCode) return next
- const failed = (value) => ['failed', 'unavailable'].includes(value?.state)
- if (!next?.data) return failed(next)
- ? { ...next, state: 'stale', data: previous.data, retained: true } : next
- if (next.data.deviceCode !== deviceCode) return next
- let retained = false
- const retainSection = (old, current) => {
- if (!failed(current) || old?.data == null || !['success', 'stale', 'empty'].includes(old.state)) return current
- retained = true
- return { ...current, state: 'stale', data: old.data, retained: true }
- }
- const oldSections = previous.data.sections || {}
- const sections = { ...next.data.sections }
- for (const [name, current] of Object.entries(sections)) {
- sections[name] = retainSection(oldSections[name], current)
- }
- // The record feed is independent of legacy metrics; a healthy feed must still replace an old one.
- if (sections.metrics && next.data.sections?.metrics?.collectionRecords) {
- const collectionRecords = retainSection(oldSections.metrics?.collectionRecords, next.data.sections.metrics.collectionRecords)
- sections.metrics = { ...sections.metrics, collectionRecords }
- }
- return retained ? { ...next, retained: true, data: { ...next.data, sections } } : next
- }
|