Skip to content
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

fix(core): stop accepting GestureTypes enum as an eventName #10537

Merged
merged 4 commits into from
May 6, 2024

Conversation

shirakaba
Copy link
Contributor

@shirakaba shirakaba commented May 3, 2024

This PR is related to #10538, but can be merged independently. Whichever is merged first will cause minor conflicts with the other, but it's a simple rebase that I'd be happy to take care of.

PR Checklist

What is the current behavior?

ViewCommon is a subclass of Observable. When overriding addEventListener and removeEventListener, it makes the following changes to the signature:

- addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any, once?: boolean): void;
+ addEventListener(eventNames: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any, once?: boolean): void;

- removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any): void;
+ removeEventListener(eventNames: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any);

Although this is allowed in TypeScript, even in strict mode, here are my motivations for removing it:

  • there's no real need for it. We can just coerce to string upstream of it. Its one advantage is that it supports expressing multiple events at once, but (aside from there being an appetite to remove support for adding/moving multiple events at once) it seems we're not even using that functionality in practice.
  • we're not extensively using the feature anywhere within Core in the first place (only four lines across all of Core).
  • it complicates the typings and makes them diverge further from EventTarget.

What is the new behavior?

Accept only strings for event names.

This is not a breaking change within Core (no tests were relying on it, and I've migrated the only four lines relying on it), and any plugins making use of it should see what to do by heeding the updated typings.

@cla-bot cla-bot bot added the cla: yes label May 3, 2024
@shirakaba shirakaba changed the title fix(core): stop accepting GestureTypes as an eventName fix(core): stop accepting GestureTypes enum as an eventName May 3, 2024
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the names suggest, these tests are not actually automated tests; they're purely auto-documented code-snippets for use on the old docs site. I thought I'd just remove the now-unsupported ones to save some mental burden when using the search functionality.

Comment on lines +334 to 336
export function fromString(type: string): GestureTypes | undefined {
return GestureTypes[type.trim()];
}
Copy link
Contributor Author

@shirakaba shirakaba May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the .toLowerCase() as:

  1. Event names have mostly been input via the toString() convenience function up until now.
  2. Users should have been getting the string keys from the GestureTypes enum to begin with.

To be honest, we can likely remove the .trim() too, but I'd prefer to do that in the PR where we remove support for multiple event names.

Comment on lines -129 to +131
this.on(GestureTypes.touch, this._highlightedHandler);
this.on(GestureTypes[GestureTypes.touch], this._highlightedHandler);
} else {
this.off(GestureTypes.touch, this._highlightedHandler);
this.off(GestureTypes[GestureTypes.touch], this._highlightedHandler);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@farfromrefug As requested, now using the string constants instead (forgot how enums worked).

Copy link
Contributor

@CatchABus CatchABus May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is we have guided people to use the types as explained in old docs for years: https://old.docs.nativescript.org/ui/gestures

How are we going to make up for that?

Copy link
Contributor Author

@shirakaba shirakaba May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 🤔

Optimistically, I’d imagine they’d see a compiler error, see that it now accepts only a string, and be able to take a good guess at what the string is (as it comes from the same enum).

Arguably, it’s now left unmentioned in the new docs, so the typings serve as the up-to-date documentation.

I’d rather not handle it at runtime and just pull the plaster, if possible. It’s unlikely to be the only breaking change regarding event-handling that’s to come, so once the dust has settled, we could provide a migration guide based on what makes it into the release. Most likely in the blog post announcing the new release.

@shirakaba
Copy link
Contributor Author

shirakaba commented May 4, 2024

@farfromrefug @edusperoni @CatchABus Sorry to poke. Anything holding this back from approval?

@farfromrefug
Copy link
Collaborator

Good with me

@NathanWalker NathanWalker merged commit 3b77fff into main May 6, 2024
4 checks passed
@NathanWalker NathanWalker deleted the gesture-observers-cleanup branch May 6, 2024 18:04
apburgess pushed a commit to apburgess/NativeScript that referenced this pull request May 15, 2024
apburgess pushed a commit to apburgess/NativeScript that referenced this pull request May 15, 2024
apburgess pushed a commit to apburgess/NativeScript that referenced this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants