Skip to content

Conversation

@gabriellsh
Copy link
Member

@gabriellsh gabriellsh commented Mar 12, 2025

Proposed changes (including videos or screenshots)

Whenever a callbackRef reference (e.g. pointer to the function) changes, the callbackRef runs again.

In the MessageBox component it is expected that the callbackRef's references can change at any given time, but attached event listeners are never removed, thus duplicating events in certain occasions.

In order to solve this issue in an elegant manner, a new hook is being introduced, called useSafeCallbackRef. This hook allows a cleanup callback to be returned from the callbackRef, and will run such callback whenever the callbackRef is called after the first time. The cleanup callback is optional, in case some condition inside the callback is not required.

If a callbackRef never requires a cleanup effect, there's no need to use it.

PS.: This PR is labaled as chore because currently there are other safeguards around such duplicated events, so there's no behaviour change, and a changeset should not be added. This issue was noticed during the development of #32703 since those safeguards have changed, and breaks messaging behaviour.

Also, having duplicate events is never a good idea, since it can lead to memory leaks.

Issue(s)

CORE-568

Steps to test or reproduce

No change in behaviour so far, only way to test is adding logs.

If you want to ensure proper behaviour, go to apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx and add a log to keyboardEventHandler. Then open an encrypted room, evey keyboard input in the composer will output 2 logs to the console. This should not happen.

Further comments

This is a niche issue, since usually callbackRefs do not change. Most of the time you should prefer to use react events.

@gabriellsh gabriellsh requested a review from a team as a code owner March 12, 2025 17:20
@dionisio-bot
Copy link
Contributor

dionisio-bot bot commented Mar 12, 2025

Looks like this PR is ready to merge! 🎉
If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link

changeset-bot bot commented Mar 12, 2025

⚠️ No Changeset found

Latest commit: 2660e7a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

PR Preview Action v1.6.0

🚀 View preview at
https://RocketChat.github.io/Rocket.Chat/pr-preview/pr-35481/

Built to branch gh-pages at 2025-03-12 17:25 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@codecov
Copy link

codecov bot commented Mar 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.64%. Comparing base (34d96e4) to head (2660e7a).
Report is 13 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #35481      +/-   ##
===========================================
- Coverage    59.64%   59.64%   -0.01%     
===========================================
  Files         2828     2828              
  Lines        68542    68537       -5     
  Branches     15193    15192       -1     
===========================================
- Hits         40884    40879       -5     
  Misses       25010    25010              
  Partials      2648     2648              
Flag Coverage Δ
unit 75.87% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ggazzo ggazzo added this to the 7.5.0 milestone Mar 12, 2025
@ggazzo ggazzo added the stat: QA assured Means it has been tested and approved by a company insider label Mar 12, 2025
@dionisio-bot dionisio-bot bot added the stat: ready to merge PR tested and approved waiting for merge label Mar 12, 2025
@ggazzo ggazzo merged commit d01b3f3 into develop Mar 12, 2025
50 checks passed
@ggazzo ggazzo deleted the fix/duplicateEvents branch March 12, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat: QA assured Means it has been tested and approved by a company insider stat: ready to merge PR tested and approved waiting for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants