-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Conversation
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 = |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
3e24b85
to
d95d0de
Compare
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).