-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Error when several instances with different root configurations #1055
Comments
@Jonnyprof could you tell more about your environment? Because I don't see a way how And can you prepare some example on stackblitz by forking this project https://stackblitz.com/edit/ng-in-viewport-example?embed=1&file=src/main.ts |
Sorry, I didn't notice your reply. I'm logging config.hash You can download the stackblitz code and run it on a webserver and hash will be always false. I hope this help. |
I will switch to btoa as now node.js lts have that function in its global scope. |
@all-contributors please add @Jonnyprof for bug |
I've put up a pull request to add @Jonnyprof! 🎉 |
Should be fixed in v15.0.0 |
Great, thanks! Tell me if you need some testing help. |
@Jonnyprof You can install it and test if it works for you. As it was complete rewrite of library, it's now have 100% coverage. But unit tests are not real world scenarios. So it would be beneficial to see if it works in real app. |
It happens to me in version 6.1.5, but I guess it happens too in the lastest version, because isPlatformBrowser function (the main problem) has the same code. I'll try to explain it as best as possible.
When you register several instances, they can reuse IntersectionObserver with findEntry function.
ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport.service.ts
Line 49 in 97a772d
The error occurs because
config.hash
is alwaysfalse
because isPlatformBrowser returns always false.Let's go step by step:
1- Hash is calculated in InViewportConfig contructor calling a static function.
ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport-config.ts
Line 117 in 97a772d
2- Static hash function calls toBase64 static function.
ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport-config.ts
Lines 104 to 106 in 97a772d
3- toBase64 depends on isPlatformBrowser.
ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport-config.ts
Lines 43 to 47 in 97a772d
4- isPlatformBrowser always return false, because
this === window
is always false.ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport-config.ts
Lines 35 to 38 in 97a772d
5- As isPlatformBrowser is false we call toBase64Node. This function returns always false because of
typeof global !== 'undefined'
ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport-config.ts
Lines 49 to 52 in 97a772d
6- So this._hash is always false
ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport-config.ts
Line 117 in 97a772d
If we have 2 instances, the first one has a
rootMargin = '0px 0px 0px 0px';
and the second onerootMargin = '100px 0px 0px 0px';
. When register the instances in the service, both uses same IntersectionObserver because both have same hash (false) and same root.ng-in-viewport/projects/ng-in-viewport/src/lib/in-viewport.service.ts
Line 49 in 97a772d
and the second one not works with the correct margin, so don't work as expected.
The text was updated successfully, but these errors were encountered: