Skip to content

fix(core): Pseudo-class handlers failing to unsubscribe listeners #10680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/ui/button/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class Button extends ButtonBase {
this.on(GestureTypes[GestureTypes.touch], onButtonStateChange);
} else {
this.off(GestureTypes[GestureTypes.touch], onButtonStateChange);
this._removeVisualState('highlighted');
}
}

Expand Down
32 changes: 21 additions & 11 deletions packages/core/ui/button/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export class Button extends ButtonBase {

public disposeNativeView(): void {
this._tapHandler = null;

if (this._stateChangedHandler) {
this._stateChangedHandler.stop();
this._stateChangedHandler = null;
}

super.disposeNativeView();
}

Expand All @@ -37,28 +43,32 @@ export class Button extends ButtonBase {
return this.nativeViewProtected;
}

public onUnloaded() {
super.onUnloaded();
if (this._stateChangedHandler) {
this._stateChangedHandler.stop();
}
}

@PseudoClassHandler('normal', 'highlighted', 'pressed', 'active')
_updateButtonStateChangeHandler(subscribe: boolean) {
if (subscribe) {
if (!this._stateChangedHandler) {
const viewRef = new WeakRef<Button>(this);

this._stateChangedHandler = new ControlStateChangeListener(this.nativeViewProtected, observableVisualStates, (state: string, add: boolean) => {
if (add) {
this._addVisualState(state);
} else {
this._removeVisualState(state);
const view = viewRef?.deref?.();

if (view) {
if (add) {
view._addVisualState(state);
} else {
view._removeVisualState(state);
}
}
});
}
this._stateChangedHandler.start();
} else {
this._stateChangedHandler.stop();

// Remove any possible pseudo-class leftovers
for (const state of observableVisualStates) {
this._removeVisualState(state);
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions packages/core/ui/core/control-state-change/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

@NativeClass
class ObserverClass extends NSObject {
public callback: WeakRef<ControlStateChangeListenerCallback>;
public callback: ControlStateChangeListenerCallback;

public static initWithCallback(callback: WeakRef<ControlStateChangeListenerCallback>): ObserverClass {
public static initWithCallback(callback: ControlStateChangeListenerCallback): ObserverClass {
const observer = <ObserverClass>ObserverClass.alloc().init();
observer.callback = callback;

return observer;
}

public observeValueForKeyPathOfObjectChangeContext(path: string, object: UIControl) {
const callback = this.callback?.deref();

const callback = this.callback;
if (callback) {
callback(path, object[path]);
}
Expand All @@ -30,7 +29,7 @@ export class ControlStateChangeListener implements ControlStateChangeListenerDef
constructor(control: UIControl, states: string[], callback: ControlStateChangeListenerCallback) {
this._control = control;
this._states = states;
this._observer = ObserverClass.initWithCallback(new WeakRef(callback));
this._observer = ObserverClass.initWithCallback(callback);
}

public start() {
Expand Down
24 changes: 13 additions & 11 deletions packages/core/ui/core/view/view-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,21 @@ export function viewMatchesModuleContext(view: ViewDefinition, context: ModuleCo

export function PseudoClassHandler(...pseudoClasses: string[]): MethodDecorator {
const stateEventNames = pseudoClasses.map((s) => ':' + s);
const listeners = Symbol('listeners');

return <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => {
function update(change: number) {
const prev = this[listeners] || 0;
const next = prev + change;
if (prev <= 0 && next > 0) {
this[propertyKey](true);
} else if (prev > 0 && next <= 0) {
this[propertyKey](false);

return <T>(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => {
// This will help keep track of pseudo-class subscription changes
const subscribeKey = Symbol(propertyKey + '_flag');

function onSubscribe(subscribe: boolean) {
if (subscribe != !!this[subscribeKey]) {
this[subscribeKey] = subscribe;
this[propertyKey](subscribe);
}
}
stateEventNames.forEach((s) => (target[s] = update));

for (const eventName of stateEventNames) {
target[eventName] = onSubscribe;
}
};
}

Expand Down
18 changes: 12 additions & 6 deletions packages/core/ui/editable-text-base/editable-text-base-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,25 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB
public autocorrect: boolean;
public hint: string;
public maxLength: number;
public placeholderColor: Color;
public valueFormatter: (value: string) => string;

public abstract dismissSoftInput();
public abstract _setInputType(inputType: number): void;
public abstract setSelection(start: number, stop?: number);
placeholderColor: Color;

@PseudoClassHandler('focus', 'blur')
_updateTextBaseFocusStateHandler(subscribe) {
const method = subscribe ? 'on' : 'off';

this[method]('focus', focusChangeHandler);
this[method]('blur', focusChangeHandler);
_updateTextBaseFocusStateHandler(subscribe: boolean) {
if (subscribe) {
this.on('focus', focusChangeHandler);
this.on('blur', focusChangeHandler);
} else {
this.off('focus', focusChangeHandler);
this.off('blur', focusChangeHandler);

this._removeVisualState('focus');
this._removeVisualState('blur');
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/ui/styling/style-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ export class CssState {
const eventName = ':' + pseudoClass;
view.addEventListener(':' + pseudoClass, this._onDynamicStateChangeHandler);
if (view[eventName]) {
view[eventName](+1);
view[eventName](true);
}
});
}
Expand All @@ -812,7 +812,7 @@ export class CssState {
const eventName = ':' + pseudoClass;
view.removeEventListener(eventName, this._onDynamicStateChangeHandler);
if (view[eventName]) {
view[eventName](-1);
view[eventName](false);
}
});
}
Expand Down
Loading