Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
On SSE error, don't flag failure for an intentional close
In EventSource onerror, gate the failure/fallback state on readyState !== CLOSED, but always close() and null the ref so reconnection logic stays correct.
Bad example
| 1 | es.onerror = () => { |
| 2 | es.close(); |
| 3 | streamRef.current = null; |
| 4 | setStreamFailed(true); // fires even when the server cleanly closed the stream |
| 5 | }; |
Explanation (EN)
When the server intentionally ends the stream, onerror fires with readyState already CLOSED; unconditionally setting failed=true triggers a spurious polling fallback for a clean close.
Objašnjenje (HR)
Kad server namjerno zavrsi stream, onerror se okida s readyState vec CLOSED; bezuvjetno postavljanje failed=true pokrece laznu polling rezervu za cisto zatvaranje.
Good example
| 1 | es.onerror = () => { |
| 2 | // Only treat as a real failure if it wasn't an intentional close |
| 3 | if (es.readyState !== EventSource.CLOSED) { |
| 4 | setStreamFailed(true); |
| 5 | } |
| 6 | es.close(); |
| 7 | streamRef.current = null; // load-bearing: lets the reconnect guard re-connect |
| 8 | }; |
Explanation (EN)
The guard suppresses the false-alarm fallback on intentional closes, while close() + nulling the ref always run so the reconnect guard can fire again.
Objašnjenje (HR)
Guard suzbija laznu rezervu kod namjernih zatvaranja, dok se close() i postavljanje ref-a na null uvijek izvrse kako bi se reconnect guard mogao ponovno okinuti.
Notes (EN)
Don't drop the ref = null assignment as 'redundant' — it is what lets the connect guard and retry interval reconnect.
Bilješke (HR)
Ne uklanjaj dodjelu ref = null kao 'suvisnu' — upravo ona omogucuje connect guardu i retry intervalu da se ponovno poveze.
Exceptions / Tradeoffs (EN)
If the server holds the stream open indefinitely and never closes intentionally, the readyState guard adds no value and can be omitted.
Iznimke / Tradeoffi (HR)
Ako server drzi stream otvorenim neograniceno i nikad ga namjerno ne zatvara, readyState guard ne donosi vrijednost i moze se izostaviti.