@@ -3399,6 +3399,7 @@ FragmentInstance.prototype.unobserveUsing = function (
33993399 unobserveChild ,
34003400 observer ,
34013401 ) ;
3402+ unobservePendingChildren ( this , observer ) ;
34023403 }
34033404} ;
34043405function unobserveChild (
@@ -3415,6 +3416,92 @@ function unobserveChild(
34153416 observer . unobserve ( instance ) ;
34163417 return false ;
34173418}
3419+
3420+ type PendingIntersectionUnobserve = {
3421+ fragmentInstance : FragmentInstanceType ,
3422+ observer : IntersectionObserver ,
3423+ instance : Instance ,
3424+ } ;
3425+
3426+ let pendingIntersectionUnobserves : Array < PendingIntersectionUnobserve > = [ ] ;
3427+ let intersectionUnobserveScheduled : boolean = false ;
3428+
3429+ function isIntersectionObserver (
3430+ observer : IntersectionObserver | ResizeObserver ,
3431+ ) : boolean {
3432+ // IntersectionObserver has rootMargin; ResizeObserver does not. Avoid
3433+ // instanceof so jsdom mocks and cross-realm observers still match.
3434+ return typeof ( observer as any ) . rootMargin === 'string' ;
3435+ }
3436+
3437+ // A later commit can reinsert the same node before the post-paint unobserve
3438+ // runs (Activity hidden → visible). Cancel so the flush does not drop it.
3439+ function cancelPendingIntersectionUnobserve (
3440+ fragmentInstance : FragmentInstanceType ,
3441+ observer : IntersectionObserver | ResizeObserver ,
3442+ instance : Instance ,
3443+ ) : void {
3444+ let writeIdx = 0 ;
3445+ for ( let i = 0 ; i < pendingIntersectionUnobserves . length ; i ++ ) {
3446+ const pending = pendingIntersectionUnobserves [ i ] ;
3447+ if (
3448+ pending . fragmentInstance !== fragmentInstance ||
3449+ pending . observer !== observer ||
3450+ pending . instance !== instance
3451+ ) {
3452+ pendingIntersectionUnobserves [ writeIdx ++ ] = pending ;
3453+ }
3454+ }
3455+ pendingIntersectionUnobserves . length = writeIdx ;
3456+ }
3457+
3458+ // unobserveUsing() only walks the children that are still mounted, so release
3459+ // the deleted ones that are waiting for their exit record.
3460+ function unobservePendingChildren (
3461+ fragmentInstance : FragmentInstanceType ,
3462+ observer : IntersectionObserver | ResizeObserver ,
3463+ ) : void {
3464+ let writeIdx = 0 ;
3465+ for ( let i = 0 ; i < pendingIntersectionUnobserves . length ; i ++ ) {
3466+ const pending = pendingIntersectionUnobserves [ i ] ;
3467+ if (
3468+ pending . fragmentInstance === fragmentInstance &&
3469+ pending . observer === observer
3470+ ) {
3471+ observer . unobserve ( pending . instance ) ;
3472+ } else {
3473+ pendingIntersectionUnobserves [ writeIdx ++ ] = pending ;
3474+ }
3475+ }
3476+ pendingIntersectionUnobserves . length = writeIdx ;
3477+ }
3478+
3479+ function schedulePendingIntersectionUnobserve (
3480+ fragmentInstance : FragmentInstanceType ,
3481+ observer : IntersectionObserver ,
3482+ instance : Instance ,
3483+ ) : void {
3484+ pendingIntersectionUnobserves . push ( {
3485+ fragmentInstance,
3486+ observer,
3487+ instance,
3488+ } ) ;
3489+ if ( ! intersectionUnobserveScheduled ) {
3490+ intersectionUnobserveScheduled = true ;
3491+ // Unobserving in this commit would cancel the record IntersectionObserver
3492+ // delivers for the now disconnected target. Wait until after paint so the
3493+ // exit fires first, then drop the observer's strong ref to the node.
3494+ requestPostPaintCallback ( ( ) => {
3495+ intersectionUnobserveScheduled = false ;
3496+ const pending = pendingIntersectionUnobserves ;
3497+ pendingIntersectionUnobserves = [ ] ;
3498+ for ( let i = 0 ; i < pending . length ; i ++ ) {
3499+ const item = pending [ i ] ;
3500+ item . observer . unobserve ( item . instance ) ;
3501+ }
3502+ } ) ;
3503+ }
3504+ }
34183505// $FlowFixMe[prop-missing]
34193506FragmentInstance . prototype . getClientRects = function (
34203507 this : FragmentInstanceType ,
@@ -3797,8 +3884,10 @@ export function commitNewChildToFragmentInstance(
37973884 return ;
37983885 }
37993886 const instance : InstanceWithFragmentHandles = childInstance as any ;
3800- if ( fragmentInstance . _observers !== null ) {
3801- fragmentInstance . _observers . forEach ( observer => {
3887+ const observers = fragmentInstance . _observers ;
3888+ if ( observers !== null ) {
3889+ observers . forEach ( observer => {
3890+ cancelPendingIntersectionUnobserve ( fragmentInstance , observer , instance ) ;
38023891 observer . observe ( instance ) ;
38033892 } ) ;
38043893 }
@@ -3826,6 +3915,22 @@ export function deleteChildFromFragmentInstance(
38263915 return;
38273916 }
38283917 const instance : InstanceWithFragmentHandles = childInstance as any ;
3918+ const observers = fragmentInstance . _observers ;
3919+ if ( observers !== null ) {
3920+ observers . forEach ( observer => {
3921+ if ( isIntersectionObserver ( observer ) ) {
3922+ // Stay observed until the next IntersectionObserver delivery so a
3923+ // disconnected target still gets an isIntersecting: false record.
3924+ schedulePendingIntersectionUnobserve (
3925+ fragmentInstance ,
3926+ observer as any as IntersectionObserver ,
3927+ instance ,
3928+ ) ;
3929+ } else {
3930+ observer . unobserve ( instance ) ;
3931+ }
3932+ } ) ;
3933+ }
38293934 if ( enableFragmentRefsInstanceHandles ) {
38303935 if ( instance . reactFragments != null ) {
38313936 instance . reactFragments . delete ( fragmentInstance ) ;
0 commit comments