Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(core): clean up event-handling in ViewCommon #10534

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 4 commits into from
May 2, 2024

Conversation

shirakaba
Copy link
Contributor

@shirakaba shirakaba commented May 1, 2024

Relates to #10531. These two PRs are independent and can be merged in either order.

Cleans up the code related to event-handling in ViewCommon.

It makes it a lot clearer that we're actually splitting event names both here and again in Observable! So in a follow-up PR after this and the related PR, we can investigate removing event name splitting altogether.

PR Checklist

What is the current behavior?

There's some unclear, redundant code.

What is the new behavior?

I've made the code clear and readable.

Comment on lines +308 to +307
// If it's a gesture (and this Observable declares e.g. `static tapEvent`)
if (gesture && !this._isEvent(normalizedName)) {
Copy link
Contributor Author

@shirakaba shirakaba May 1, 2024

Choose a reason for hiding this comment

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

A very curious case is when it's a gesture (e.g. "tap") and yet the View hasn't declared static tapEvent. We'll fall through to the next case, which just treats it as a regular event named "tap" rather than setting up a tap gesture observer. This has long/always been the case, so I decided not to change it.

I don't have the appetite to change it during this PR that's just focused on cleanup, but any thoughts about this anyway? Why is it necessary in the first place to declare static tapEvent on a ViewBase in order for it to be treated as a gesture event? Is that still an appropriate idea, or would it make sense to remove those across all classes by removing this condition?

Copy link
Contributor

Choose a reason for hiding this comment

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

Those static properties are for vanilla to be able to distinguish properties from events because XML format does not help in that matter.

We could get rid of those static properties across all classes possibly by doing a breaking change on vanilla and expect event declarations to have something like on prefix in XML.

<!-- 'on' helps distinguish events from properties -->
<Label text="Hello world" on:tap="dosomething"/>

@shirakaba shirakaba changed the title fix(core): clean up event-handling in ViewBase fix(core): clean up event-handling in ViewCommon May 1, 2024

// Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined
const gesture: GestureTypes | undefined = gestureFromString(normalizedName);
Copy link
Contributor Author

@shirakaba shirakaba May 1, 2024

Choose a reason for hiding this comment

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

Desperate for TypeScript strict mode 😔

@shirakaba shirakaba changed the title fix(core): clean up event-handling in ViewCommon chore(core): clean up event-handling in ViewCommon May 1, 2024
@shirakaba
Copy link
Contributor Author

For a follow-up PR: would be worth checking that addObservers and removeObservers follow all the same rules as addEventListener and removeEventListener (i.e. checking whether thisArg matches).

Copy link
Collaborator

@farfromrefug farfromrefug left a comment

Choose a reason for hiding this comment

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

I think we should already deal with that redundant code for the regexp and split of ,.
I would even remove support of multiple events directly

@shirakaba shirakaba force-pushed the gesture-events-cleanup branch from 5b9d24f to 8ba69ad Compare May 1, 2024 23:34
ViewCommon splits event names redundantly (the superclass, Observable, splits event names too).
@shirakaba shirakaba force-pushed the gesture-events-cleanup branch from 8ba69ad to 6311592 Compare May 1, 2024 23:41
@shirakaba
Copy link
Contributor Author

@farfromrefug I've dealt with that redundant code now.

Removing support for multiple events altogether is something I'd prefer to do in a separate followup PR, as it would surely be slowed down by discussion of migration/deprecation path.

@shirakaba shirakaba changed the title chore(core): clean up event-handling in ViewCommon fix(core): clean up event-handling in ViewCommon May 1, 2024
@NathanWalker NathanWalker merged commit 4a7e40d into main May 2, 2024
1 of 4 checks passed
@NathanWalker NathanWalker deleted the gesture-events-cleanup branch May 2, 2024 07:03
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.

4 participants