Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP1universalStack: javascript
memory-leaklistenerslifecycleperformance
Register shared/global listeners once, not on every instance
Hooking up an observer or event listener tied to shared (static) state inside a constructor that runs per instance leaks listeners; register it a single time.
PR: hegnar-web · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | class Widget { |
| 2 | static observer = new ResizeObserver(cb); |
| 3 | constructor() { |
| 4 | Widget.observer.disconnect(); |
| 5 | Widget.observer = new ResizeObserver(cb); // new instance drops the previous one's listeners |
| 6 | } |
| 7 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | class Widget { |
| 2 | static observer = new ResizeObserver(cb); // created once |
| 3 | observe(el: Element) { Widget.observer.observe(el); } |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)