Refactor MutationObserver.disconnect (#51128)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51128

Changelog: [internal]

Just a minor refactor of a method in `MutationObserver` which was doing some redundant work that could be avoided.

Reviewed By: javache

Differential Revision: D74247437

fbshipit-source-id: 510d26f2a315582df9cddc215bf516d3d4ebd341
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2025-05-07 04:17:26 -07:00
1 parent 759652b09f
commit da56d57ac7
1 file changed
+11 -27
@@ -132,39 +132,23 @@ export default class MutationObserver {
}
}
_unobserve(target: ReactNativeElement): void {
if (!(target instanceof ReactNativeElement)) {
throw new TypeError(
"Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'ReactNativeElement'.",
);
}
if (!this._observationTargets.has(target)) {
return;
}
const mutationObserverId = this._mutationObserverId;
if (mutationObserverId == null) {
return;
}
MutationObserverManager.unobserve(mutationObserverId, target);
this._observationTargets.delete(target);
if (this._observationTargets.size === 0) {
MutationObserverManager.unregisterObserver(mutationObserverId);
this._mutationObserverId = null;
}
}
/**
* Tells the observer to stop watching for mutations.
* The observer can be reused by calling its `observe()` method again.
*/
disconnect(): void {
for (const target of this._observationTargets.keys()) {
this._unobserve(target);
const mutationObserverId = this._mutationObserverId;
if (mutationObserverId == null) {
return;
}
for (const target of this._observationTargets.keys()) {
MutationObserverManager.unobserve(mutationObserverId, target);
this._observationTargets.delete(target);
}
MutationObserverManager.unregisterObserver(mutationObserverId);
this._mutationObserverId = null;
}
_getOrCreateMutationObserverId(): MutationObserverId {