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

Replace hammerJS with native touch events #452

Closed
wants to merge 18 commits into from
Closed

Replace hammerJS with native touch events #452

wants to merge 18 commits into from

Conversation

vpetrusevici
Copy link
Contributor

@vpetrusevici vpetrusevici commented Aug 9, 2022

Currently I implemented swiping only for touch events (not mouse) and only for gallery-swiper component (not thumbs).
MurhafSousli Can you check please if my implementation is good enough and I can continue work in this way?
For #449

Copy link
Owner

@MurhafSousli MurhafSousli left a comment

Choose a reason for hiding this comment

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

My review is based on the code changes, but swiping didn't work!

domElement: this._el.nativeElement,
onSwipeMove: event => {
if (event.direction === this.config.slidingDirection) {
this._zone.run(() => this.updateSlider({ value: event.distance, active: true }));
Copy link
Owner

Choose a reason for hiding this comment

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

We should not use zone.run() here because we don't want to trigger so many change detections while dragging

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I'll try to avoid it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like hammerJS somehow triggers ChangeDetection on each pan event, but here I don't see any possibility to trigger async pipe outside of ngZone or without manually call detection. Here an example https://stackoverflow.com/a/35513390
Mb we can avoid ngStyles and use native js styles change, but don't think it's a good solution

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll continue with mouse events and thumbs swiping for now. Let me know if you have any ideas how to avoid change detection here.

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 i see we can improve performance using custom async pipe with subscription outside of ngZone as discussed in angular/angular#36139
Example: https://stackblitz.com/edit/angular-async-pipe-outside-angular?file=src%2Fapp%2Fvo-async.pipe.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I replaced all async pipes with this custom pipe, and yes, it's really effective. Now we can create an observable outside of ngZone and don't care about changeDetection.

}

ngOnInit() {
if (this.config.gestures && typeof Hammer !== 'undefined') {
Copy link
Owner

Choose a reason for hiding this comment

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

We should only enable swiping if user enables the gestures in config

Copy link
Contributor Author

@vpetrusevici vpetrusevici Aug 10, 2022

Choose a reason for hiding this comment

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

fixed

}
private onSwipe(e: SwipeEvent) {
if (e.direction === this.config.slidingDirection) {
const velocity = e.distance / (e.moveEvent.sourceEvent.timeStamp - e.startEvent.sourceEvent.timeStamp);
Copy link
Owner

Choose a reason for hiding this comment

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

Would be better if we get the velocity with the SwipeEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. It was here just to test :)

Copy link
Contributor Author

@vpetrusevici vpetrusevici Aug 10, 2022

Choose a reason for hiding this comment

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

fixed

@vpetrusevici
Copy link
Contributor Author

@MurhafSousli strange about swiping. I just retested it in chrome. Looks like is working https://amdaris-my.sharepoint.com/:v:/p/vladimir_petrusevici/EShrbtcG6pFDng1Z2TeSJQ8BzEmBGQIiCwkzAaM7tEAXzg?e=UBLKuq

@vpetrusevici
Copy link
Contributor Author

Also I see a problem with autoplay. Application will never have a stable state when autoPlay = true in the current implementation.
You can check it in this way
image

I think this observable with delay should be also moved outside ngZone.

@vpetrusevici
Copy link
Contributor Author

Also I see a problem with autoplay. Application will never have a stable state when autoPlay = true in the current implementation. You can check it in this way image

I think this observable with delay should be also moved outside ngZone.

Fixed with custom async pipe. Now the app is always stable.

@vpetrusevici
Copy link
Contributor Author

Implemented mouse events

@vpetrusevici
Copy link
Contributor Author

Aaaand looks like all functionalities are implemented now.

@vpetrusevici
Copy link
Contributor Author

@MurhafSousli should i change the docs or is it better to leave it to you?

@vpetrusevici vpetrusevici marked this pull request as ready for review August 10, 2022 19:36
@vpetrusevici
Copy link
Contributor Author

I should stop pushing... sorry)

@vpetrusevici
Copy link
Contributor Author

@MurhafSousli Hi, I fixed the conflicts. Can you check this PR please?

@vpetrusevici
Copy link
Contributor Author

Hi @MurhafSousli,
did you have a chance to check it out? Сan't wait to finish this PR :)

@MurhafSousli
Copy link
Owner

Hi @MurhafSousli, did you have a chance to check it out? Сan't wait to finish this PR :)

When you add many unnecessary changes, you make it difficult for me to review! please keep the PR focused next time and don't refactor parts that are not related to the replacement of hammer.js

@vpetrusevici
Copy link
Contributor Author

Hi @MurhafSousli, did you have a chance to check it out? Сan't wait to finish this PR :)

When you add many unnecessary changes, you make it difficult for me to review! please keep the PR focused next time and don't refactor parts that are not related to the replacement of hammer.js

Sorry, i can try to recreate the PR with only necessary changes, but don't think it will be much smaller

@MurhafSousli
Copy link
Owner

MurhafSousli commented Oct 5, 2022

I think HammerJS is not bad, comparing the benefits we would get from implementing our own touch-events solution does not bring any value in terms of feature or functionality, we are just adding more code which we will have to maintenance, in favor of getting rid of one dependency.

I will close this for now, but will keep an eye on your solution if something changed.

Thanks for the PR anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants