fix(ios): ScrollView with listeners removed scroll delegate (#10432)

This commit is contained in:
Dimitris-Rafail Katsampas
2023-11-22 20:06:56 +02:00
committed by GitHub
parent 2cf166da59
commit 9fae9c4281

View File

@ -9,18 +9,23 @@ import { CoreTypes } from '../../core-types';
@CSSType('ScrollView') @CSSType('ScrollView')
export abstract class ScrollViewBase extends ContentView implements ScrollViewDefinition { export abstract class ScrollViewBase extends ContentView implements ScrollViewDefinition {
private _scrollChangeCount = 0;
public static scrollEvent = 'scroll'; public static scrollEvent = 'scroll';
public orientation: CoreTypes.OrientationType; public orientation: CoreTypes.OrientationType;
public scrollBarIndicatorVisible: boolean; public scrollBarIndicatorVisible: boolean;
public isScrollEnabled: boolean; public isScrollEnabled: boolean;
private _addedScrollEvent = false;
public addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any): void { public addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any): void {
const hasExistingScrollListeners: boolean = this.hasListeners(ScrollViewBase.scrollEvent);
super.addEventListener(arg, callback, thisArg); super.addEventListener(arg, callback, thisArg);
if (arg === ScrollViewBase.scrollEvent) { // This indicates that a scroll listener was added for first time
this._scrollChangeCount++; if (!hasExistingScrollListeners && this.hasListeners(ScrollViewBase.scrollEvent)) {
this._addedScrollEvent = true;
if (this.nativeViewProtected) { if (this.nativeViewProtected) {
this.attachNative(); 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 { public removeEventListener(arg: string, callback?: (data: EventData) => void, thisArg?: any): void {
const hasExistingScrollListeners: boolean = this.hasListeners(ScrollViewBase.scrollEvent);
super.removeEventListener(arg, callback, thisArg); super.removeEventListener(arg, callback, thisArg);
if (arg === ScrollViewBase.scrollEvent) { // This indicates that the final scroll listener was removed
if (this._scrollChangeCount > 0) { if (hasExistingScrollListeners && !this.hasListeners(ScrollViewBase.scrollEvent)) {
this._scrollChangeCount--; this._addedScrollEvent = false;
if (this.nativeViewProtected && this._scrollChangeCount === 0) { if (this.nativeViewProtected) {
this.detachNative(); this.detachNative();
}
} }
} }
} }
initNativeView() { initNativeView() {
super.initNativeView(); super.initNativeView();
if (this._scrollChangeCount > 0) { if (this._addedScrollEvent) {
this.attachNative(); this.attachNative();
} }
} }
public disposeNativeView() { public disposeNativeView() {
if (this._scrollChangeCount > 0) { if (this._addedScrollEvent) {
this.detachNative(); this.detachNative();
} }
super.disposeNativeView(); super.disposeNativeView();