Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Cancel pending connections when their owner shuts down
Connection teardown must handle connecting and open states, retain safe cancellation error handling and release the transport before discarding its owner.
Bad example
| 1 | socket.removeAllListeners(); |
| 2 | if (socket.readyState === WebSocket.OPEN) socket.close(); |
| 3 | socket = undefined; |
Explanation (EN)
A pending handshake survives and can open without an owner or emit an unhandled error.
Objašnjenje (HR)
Nedovršen handshake ostaje aktivan i može otvoriti vezu bez vlasnika ili emitirati neobrađenu pogrešku.
Good example
| 1 | import WebSocket from 'ws'; |
| 2 |
|
| 3 | function abortOwnedSocket( |
| 4 | socket: WebSocket, |
| 5 | onAbortError: (error: Error) => void, |
| 6 | ): void { |
| 7 | socket.removeAllListeners(); |
| 8 | if (socket.readyState === WebSocket.CLOSED) return; |
| 9 | socket.on('error', onAbortError); |
| 10 | socket.terminate(); |
| 11 | } |
Explanation (EN)
For an explicitly forceful shutdown policy, ws.terminate aborts a pending handshake or destroys the active socket. Expected cancellation errors still have an owner.
Objašnjenje (HR)
Za izričitu politiku prisilnog zatvaranja, ws.terminate prekida handshake ili aktivnu vezu. Očekivane pogreške prekida i dalje imaju vlasnika.
Notes (EN)
Inspect every transport state during explicit disposal, reconnect and shutdown. Cancel timers and prevent cancellation errors from scheduling another reconnect. Make cleanup idempotent and release the underlying resource before dropping references. Verify the actual library cancellation behavior for pending handshakes rather than treating removal of listeners as cancellation.
Bilješke (HR)
Provjeri svako stanje veze pri uklanjanju vlasnika, ponovnom spajanju i gašenju. Otkaži timere i spriječi da očekivani prekid pokrene novo spajanje. Čišćenje treba biti sigurno ponovljivo i osloboditi stvarni resurs prije uklanjanja referenci. Uklanjanje slušatelja nije otkazivanje veze.
Exceptions / Tradeoffs (EN)
Shared connections or work intentionally surviving navigation need a separate owner. Where buffered delivery or a close handshake matters, use a bounded graceful shutdown followed by abort if needed. Do not replace every close operation with immediate termination.
Iznimke / Tradeoffi (HR)
Dijeljene veze i posao koji namjerno preživljava navigaciju trebaju zasebnog vlasnika. Ako treba isporučiti preostale podatke ili završiti protokol zatvaranja, koristi vremenski ograničeno uredno zatvaranje pa prekid po potrebi. Nemoj svako zatvaranje zamijeniti prisilnim prekidom.