diff --git a/src/state.mjs b/src/state.mjs new file mode 100644 index 0000000..6c29dda --- /dev/null +++ b/src/state.mjs @@ -0,0 +1,15 @@ +export function advance(state, event) { + const next = { ...state, anomalies: [...state.anomalies] }; + if (event.type === 'ALARM') { + if (next.open) next.anomalies.push('duplicate alarm'); + else { next.open = true; next.firstAlarm = event.time; next.acknowledged = false; } + } else if (event.type === 'ACK') { + if (!next.open) next.anomalies.push('ack without alarm'); + else if (next.acknowledged) next.anomalies.push('duplicate ack'); + else next.acknowledged = true; + } else if (event.type === 'CLEAR') { + if (!next.open) next.anomalies.push('clear without alarm'); + else { next.open = false; next.closedAt = event.time; } + } + return next; +}