From 9fae9c428185ed2f99b3710e3fe16cf060bc072b Mon Sep 17 00:00:00 2001 From: Dimitris-Rafail Katsampas Date: Wed, 22 Nov 2023 20:06:56 +0200 Subject: [PATCH] fix(ios): ScrollView with listeners removed scroll delegate (#10432) --- .../core/ui/scroll-view/scroll-view-common.ts | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/core/ui/scroll-view/scroll-view-common.ts b/packages/core/ui/scroll-view/scroll-view-common.ts index 206b366ad..d281a974e 100644 --- a/packages/core/ui/scroll-view/scroll-view-common.ts +++ b/packages/core/ui/scroll-view/scroll-view-common.ts @@ -9,18 +9,23 @@ import { CoreTypes } from '../../core-types'; @CSSType('ScrollView') export abstract class ScrollViewBase extends ContentView implements ScrollViewDefinition { - private _scrollChangeCount = 0; public static scrollEvent = 'scroll'; public orientation: CoreTypes.OrientationType; public scrollBarIndicatorVisible: boolean; public isScrollEnabled: boolean; + private _addedScrollEvent = false; + public addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any): void { + const hasExistingScrollListeners: boolean = this.hasListeners(ScrollViewBase.scrollEvent); + super.addEventListener(arg, callback, thisArg); - if (arg === ScrollViewBase.scrollEvent) { - this._scrollChangeCount++; + // This indicates that a scroll listener was added for first time + if (!hasExistingScrollListeners && this.hasListeners(ScrollViewBase.scrollEvent)) { + this._addedScrollEvent = true; + if (this.nativeViewProtected) { this.attachNative(); } @@ -28,28 +33,29 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe } public removeEventListener(arg: string, callback?: (data: EventData) => void, thisArg?: any): void { + const hasExistingScrollListeners: boolean = this.hasListeners(ScrollViewBase.scrollEvent); + super.removeEventListener(arg, callback, thisArg); - if (arg === ScrollViewBase.scrollEvent) { - if (this._scrollChangeCount > 0) { - this._scrollChangeCount--; + // This indicates that the final scroll listener was removed + if (hasExistingScrollListeners && !this.hasListeners(ScrollViewBase.scrollEvent)) { + this._addedScrollEvent = false; - if (this.nativeViewProtected && this._scrollChangeCount === 0) { - this.detachNative(); - } + if (this.nativeViewProtected) { + this.detachNative(); } } } initNativeView() { super.initNativeView(); - if (this._scrollChangeCount > 0) { + if (this._addedScrollEvent) { this.attachNative(); } } public disposeNativeView() { - if (this._scrollChangeCount > 0) { + if (this._addedScrollEvent) { this.detachNative(); } super.disposeNativeView();