fix(react): cache event handler references for proper cleanup (#1235) - #1265
fix(react): cache event handler references for proper cleanup (#1235)#1265armorbreak001 wants to merge 1 commit into
Conversation
…pi#1235) Problem: setupEventListeners() creates new function references on every call via , so cleanupEventListeners() passes different references to pm.off() — listeners are never removed. This causes: - Duplicate event emissions on re-render - Memory leaks from accumulating listeners - pm.off() is effectively a no-op Fix: Cache event handlers in a Map<string, Function> so that pm.on() and pm.off() always receive the same reference for the same event name. Before: handler(event) → new function each call (off can't match) After: getEventHandler(event) → cached function (off matches exactly) All 177 unit tests pass.
There was a problem hiding this comment.
Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
|
|
This pull request has been automatically marked as stale because it has not had recent activity 😴 It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation. There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model. Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here. Thank you for your patience ❤️ |



Problem
Fixes #1235
Event listeners registered via
pm.on()are never properly removed bypm.off()because each call tosetupEventListeners()creates new function references.Root Cause
Impact
Fix
Cache handler functions in a
Map<string, Function>so the same reference is always used:Now
pm.on(event, getEventHandler(event))andpm.off(event, getEventHandler(event))receive identical function references.Testing