-
Notifications
You must be signed in to change notification settings - Fork 436
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
Silent renew triggers Angular change detection #1266
Comments
@lwoehlck-clgx Thanks for the nice summary! We are experiencing the same issue with change detection running every 3s. As you have already described, I think the current pattern does not help to reduce the number of change detection runs. Looking at the commit 23b4b78 that introduced the change, it seemed to be more about ensuring that I think removing const pollServerSessionRecur = () => {
// Check stuff here ...
if (somethingInterestingHappened) {
zone.run(doSomething);
}
setTimeout(pollServerSessionRecur, 3000);
};
zone.runOutsideAngular(pollServerSessionRecur); There is one more occurrence of that pattern in angular-auth-oidc-client/projects/angular-auth-oidc-client/src/lib/callback/interval.service.ts Lines 22 to 24 in 3de72d5
@damienbod What are your thoughts on this? Do you think it would be possible to remove I am happy to provide additional context if this is related to the specific setup we have, it looks however more like a general issue to me. |
I started digging into this a little more and I think we'd really have to address both of these instances to reduce the number of change detection runs: angular-auth-oidc-client/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts Lines 226 to 231 in 33372ed
angular-auth-oidc-client/projects/angular-auth-oidc-client/src/lib/callback/interval.service.ts Lines 22 to 24 in 3de72d5
The current state of my exploration can be found here maechler@6bc32f8. Possible solutions could be: 1. Remove call to This would break existing applications and is thus probably not feasible. 2. Use For get checkSessionChanged$(): Observable<boolean> {
return new Observable((subscriber) => {
return this.checkSessionChangedInternal$.asObservable().subscribe({
next: value => this.zone.run(() => subscriber.next(value)),
error: error => this.zone.run(() => subscriber.next(error)),
complete: () => this.zone.run(() => subscriber.complete()),
});
});
}
// ..
this.scheduledHeartBeatRunning = this.document?.defaultView?.setTimeout(
() => pollServerSessionRecur(),
this.heartBeatInterval
) ?? null;
// ..
this.zone.runOutsideAngular(() => pollServerSessionRecur()); For In addition this approach has the drawback that as soon as there is a subscriber, we are back to running change detection every 3s although it's hardly ever necessary. That's why I think we should probably delegate the responsibility of change detection to the consumer for those APIs. 3. Allow to opt-out of After exploring 2) I think this might be the most promising way forward. We could add a setting to conditionally disable the automatic use of 4. Wait for Angular to provide zoneless change detection Having all that said, there is already experimental support for zoneless change detection and maybe we could just wait for it to become stable: https://angular.dev/guide/experimental/zoneless |
@damienbod @FabianGosebrink What do you think about this? I still think it would be useful and also feasible to implement my 3. suggestion, adding a config to opt-out of these I'm not sure yet if such a config should be part of |
Hey Markus, thanks for your message. Sure, this PR has an absolute chance to be merged. I am thinking about this config option: Why would somebody opt out? I mean, 99% do NOT want the CD to run I suppose. What do you think? |
Agree, I just thought it would be nice not to make it a breaking change. I think simply removing But we could also just make it a breaking change or optionally first introduce it with a feature flag and then change the default value with the next major release. |
I remember that we once had issues with this. WE really have to test this well. |
Silent renew triggers Angular change detection
To Reproduce
Steps to reproduce the behavior:
ngDoCheck() { console.log('ngDoCheck called'); }
Expected behavior
ngDoCheck should not be called due to silent renews
Desktop:
Additional context
In https://github.com/damienbod/angular-auth-oidc-client/blob/3de72d57b53f479f946d30b9b77a5bfa975aec79/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts, line 147, we are calling
zone.runOutsideAngular()
and then callingzone.run()
inside that.zone.run()
is used to run code in the Angular zone (and therefore gain automatic change detection), and is undoing whatzone.runOutsideAngular()
is accomplishing (which is to run code without triggering change detection). This could be rectified by removingzone.run()
. See https://angular.io/guide/zone#ngzone-run-and-runoutsideofangular.The text was updated successfully, but these errors were encountered: