Describe the bug
When you kick-off the setup (with checkAuth) then this package will create intervals and timeouts. When using Service workers it's important to get the application into stable mode or else the new version isn't downloaded. However this is not possible because this package is making it unstable and it will never turn stable.
To Reproduce
Steps to reproduce the behavior:
ts
this.applicationRef.isStable.pipe(
tap(isStable => console.log(`ApplicationRef::stable`, isStable))
).subscribe();
Expected behavior
intervals and timeouts should be run outside the NgZone so that it wont interfere with the "stability" of Angular.
This package reaches stable for me when these two culprits are run outside angular:
https://github.com/damienbod/angular-auth-oidc-client/blob/710af82194bc9372d8a5ffedae00d94fc6f5417c/projects/angular-auth-oidc-client/src/lib/callback/intervall.service.ts#L18
https://github.com/damienbod/angular-auth-oidc-client/blob/03aa2ad53a5c4622300e7e283e2f4bd8535e1019/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts#L132
Potential fixes
intervall.service.ts:
startPeriodicTokenCheck(repeatAfterSeconds) {
const millisecondsDelayBetweenTokenCheck = repeatAfterSeconds * 1000;
const update$ = new BehaviorSubject({});
this.zone.runOutsideAngular(() => setInterval(() => this.zone.run(() => update$.next({})), millisecondsDelayBetweenTokenCheck))
return update$;
}
check-session.service.ts:
this.zone.runOutsideAngular(() => {
this.scheduledHeartBeatRunning = setTimeout(() => this.zone.run(pollServerSessionRecur), this.heartBeatInterval);
});
Hey, thanks for the issue. Can you do a PR, then we can test it. Thanks.
I have submitted the PR; I have adjusted the 'potential fix' a little bit to make it work nice with the already existing unit tests.
Also this should be considered a performance enhancement since ChangeDetection will now also not be called after each interval and timeout.
Hey @BillyBlaze , thanks for this. In the mentioned issue we now have to emit from isAuthenticated$ outside the ngzone. People have to manually hook into this if they want their UI to be updated. Do you think it is possible to hook back into the ngZone _without_ breaking the stability we fixed in this issue but that the people do not have to hook into ngZone manually? Where in the lib would be the best place without breaking the fix here? Thanks for your thoughts.
Hi @FabianGosebrink,
I do not work anymore at the company where I implemented the code, so it's hard for me to verify/test it. However there is one emit that is not run inside the zone:
https://github.com/damienbod/angular-auth-oidc-client/pull/965/files#diff-73284c44fab8baf773a2eb916d8abbf42bb245218348a0ef4a0a4efa7071e888R23
Hopefully this issue is fixed if we just change:
intervalId = setInterval(() => subscriber.next(), millisecondsDelayBetweenTokenCheck);
into:
intervalId = setInterval(() => this.zone.run(() => subscriber.next()), millisecondsDelayBetweenTokenCheck);
Hey @BillyBlaze , thanks for the quick reply.
Yes, this would be my first approach as well. Thanks, I will try that! Thank you!
Quick update. In my tests this fixes it. Thank you!
Most helpful comment
Quick update. In my tests this fixes it. Thank you!