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

refactor: always use native intersection observer #159

Merged
merged 1 commit into from
Nov 13, 2024

Conversation

arturovt
Copy link
Collaborator

In this commit, we retrieve the native IntersectionObserver implementation hidden by __zone_symbol__IntersectionObserver. This would be the unpatched version of the observer present in zone.js environments.
Otherwise, if the user is using zoneless change detection (and zone.js is not included), we fall back to the native implementation. Accessing the native implementation allows us to remove runOutsideAngular calls and reduce indentation, making the code a bit more readable.

I also reduced Prettier print width to 90, because 120 seems to be
large (the code overlaps the editor).

Copy link

stackblitz bot commented Nov 13, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

// we fall back to the native implementation. Accessing the native implementation
// allows us to remove `runOutsideAngular` calls and reduce indentation,
// making the code a bit more readable.
export const IntersectionObserver: typeof globalThis.IntersectionObserver =
Copy link
Member

Choose a reason for hiding this comment

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

but what about applications that uses zone and need to run it outside the zone?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would get the first operand immediately (globalThis.__zone_symbol__IntersectionObserver).

Copy link
Member

Choose a reason for hiding this comment

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

I meant that you removed the run outside zone call from the code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah it's not required. It's always running the native implementation (which is not intercepted by Angular).

  • when zone.js is included it gets __zone_symbol__IntersectionObserver
  • when zoneless is enabled it gets || .IntersectionObserver

Copy link
Member

Choose a reason for hiding this comment

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

But with zone js enabled this will run inside the zone

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It will. But the emission from the observable (inView()) would be outside of the zone. Native implementation is always running outside of the zone.

Copy link
Member

Choose a reason for hiding this comment

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

But the emission from the observable (inView()) would be outside of the zone

even if we use zone js?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep:

image

image

In this commit, we retrieve the native `IntersectionObserver` implementation hidden by
`__zone_symbol__IntersectionObserver`. This would be the unpatched version of the observer
present in zone.js environments.
Otherwise, if the user is using zoneless change detection (and zone.js is not included),
we fall back to the native implementation. Accessing the native implementation allows us
to remove `runOutsideAngular` calls and reduce indentation, making the code a bit more readable.

I also reduced Prettier print width to 90, because 120 seems to be
large (the code overlaps the editor).
@NetanelBasal NetanelBasal merged commit 4d13451 into ngneat:master Nov 13, 2024
2 checks passed
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