Clean up compatibility code for MutationObserver (#51309)

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

Changelog: [internal]

Clean up old compatibility code for when NativeMutationObserver.unobserveAll isn't available.

Reviewed By: lenaic

Differential Revision: D74313204

fbshipit-source-id: ef057884236563cc9592c9954d4e3edd24df74eb
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2025-05-27 05:05:26 -07:00
1 parent c59a532950
commit 017b2d0186
6 files changed
+803 -924

No files matched your search

@@ -44,15 +44,6 @@ void NativeMutationObserver::observe(
mutationObserverId, shadowNode, subtree, uiManager);
}
// TODO: remove in the next version
void NativeMutationObserver::unobserve(
jsi::Runtime& /*runtime*/,
MutationObserverId mutationObserverId,
jsi::Object targetShadowNode) {
// This will not be used but cannot be removed yet because the compatibility
// check does not allow it.
}
void NativeMutationObserver::unobserveAll(
jsi::Runtime& runtime,
MutationObserverId mutationObserverId) {
@@ -52,13 +52,6 @@ class NativeMutationObserver
jsi::Runtime& runtime,
NativeMutationObserverObserveOptions options);
// TODO: remove in the next version
[[deprecated("use unobserveAll instead")]]
void unobserve(
jsi::Runtime& runtime,
MutationObserverId mutationObserverId,
jsi::Object targetShadowNode);
void unobserveAll(
jsi::Runtime& runtime,
MutationObserverId mutationObserverId);
@@ -118,15 +118,11 @@ export default class MutationObserver {
const mutationObserverId = this._getOrCreateMutationObserverId();
const didStartObserving = MutationObserverManager.observe({
MutationObserverManager.observe({
mutationObserverId,
target,
subtree: Boolean(options?.subtree),
});
if (didStartObserving && !MutationObserverManager.unobserveAll) {
this._observationTargets.add(target);
}
}
/**
@@ -139,17 +135,7 @@ export default class MutationObserver {
return;
}
if (MutationObserverManager.unobserveAll) {
MutationObserverManager.unobserveAll(mutationObserverId);
} else if (MutationObserverManager.unobserve) {
for (const target of this._observationTargets.keys()) {
nullthrows(MutationObserverManager.unobserve)(
mutationObserverId,
target,
);
}
this._observationTargets.clear();
}
MutationObserverManager.unobserveAll(mutationObserverId);
MutationObserverManager.unregisterObserver(mutationObserverId);
this._mutationObserverId = null;
File diff suppressed because it is too large. Load diff
@@ -41,14 +41,6 @@ const registeredMutationObservers: Map<
$ReadOnly<{observer: MutationObserver, callback: MutationObserverCallback}>,
> = new Map();
// The mapping between ReactNativeElement and their corresponding shadow node
// needs to be kept here because React removes the link when unmounting.
// TODO: remove this code when NativeMutationObserver.unobserveAll is available in all apps
const targetToShadowNodeMap: WeakMap<
ReactNativeElement,
ReturnType<typeof getNativeNodeReference>,
> = new WeakMap();
/**
* Registers the given mutation observer and returns a unique ID for it,
* which is required to start observing targets.
@@ -91,10 +83,10 @@ export function observe({
mutationObserverId: MutationObserverId,
target: ReactNativeElement,
subtree: boolean,
}): boolean {
}): void {
if (NativeMutationObserver == null) {
warnNoNativeMutationObserver();
return false;
return;
}
const registeredObserver =
@@ -103,19 +95,13 @@ export function observe({
console.error(
`MutationObserverManager: could not start observing target because MutationObserver with ID ${mutationObserverId} was not registered.`,
);
return false;
return;
}
const targetShadowNode = getNativeNodeReference(target);
if (targetShadowNode == null) {
// The target is disconnected. We can't observe it anymore.
return false;
}
// We need to keep this temporarily until the changes in the native module have propagated.
// After that, we don't need to keep this mapping that can cause memory leaks.
if (!nativeUnobserveAll) {
targetToShadowNodeMap.set(target, targetShadowNode);
return;
}
if (!isConnected) {
@@ -136,65 +122,25 @@ export function observe({
targetShadowNode,
subtree,
});
return true;
}
const nativeUnobserve = NativeMutationObserver?.unobserve;
export function unobserveAll(mutationObserverId: number): void {
if (NativeMutationObserver == null) {
warnNoNativeMutationObserver();
return;
}
// TODO: delete in the next version, when NativeMutationObserver.unobserveAll is available in all apps
export const unobserve: ?(
mutationObserverId: number,
target: ReactNativeElement,
) => void = nativeUnobserve
? function unobserve(
mutationObserverId: number,
target: ReactNativeElement,
): void {
if (NativeMutationObserver == null) {
warnNoNativeMutationObserver();
return;
}
const registeredObserver =
registeredMutationObservers.get(mutationObserverId);
if (registeredObserver == null) {
console.error(
`MutationObserverManager: could not disconnect MutationObserver with ID ${mutationObserverId} because it was not registered.`,
);
return;
}
const registeredObserver =
registeredMutationObservers.get(mutationObserverId);
if (registeredObserver == null) {
console.error(
`MutationObserverManager: could not stop observing target because MutationObserver with ID ${mutationObserverId} was not registered.`,
);
return;
}
const targetShadowNode = targetToShadowNodeMap.get(target);
if (targetShadowNode == null) {
console.error(
'MutationObserverManager: could not find registration data for target',
);
return;
}
nativeUnobserve(mutationObserverId, targetShadowNode);
}
: null;
const nativeUnobserveAll = NativeMutationObserver?.unobserveAll;
// TODO: clean up as a regular export in the next version, when NativeMutationObserver.unobserveAll is available in all apps
export const unobserveAll: ?(mutationObserverId: number) => void =
nativeUnobserveAll
? function unobserveAll(mutationObserverId: MutationObserverId): void {
const registeredObserver =
registeredMutationObservers.get(mutationObserverId);
if (registeredObserver == null) {
console.error(
`MutationObserverManager: could not stop observing target because MutationObserver with ID ${mutationObserverId} was not registered.`,
);
return;
}
nativeUnobserveAll(mutationObserverId);
}
: null;
NativeMutationObserver.unobserveAll(mutationObserverId);
}
/**
* This function is called from native when there are `MutationObserver`
@@ -36,13 +36,7 @@ export type NativeMutationObserverObserveOptions = {
export interface Spec extends TurboModule {
+observe: (options: NativeMutationObserverObserveOptions) => void;
// TODO: remove in the next version
+unobserve?: (
mutationObserverId: number,
targetShadowNode: ShadowNode,
) => void;
// TODO: remove optionality in the next version
+unobserveAll?: (mutationObserverId: number) => void;
+unobserveAll: (mutationObserverId: number) => void;
+connect: (
notifyMutationObservers: () => void,
// We need this to retain the public instance before React removes the