Open pappkamerad opened 3 years ago
Hmmm, Interesting and thanks for the detailed writeup.
We are working on updating our Angular integration and add support for the new versions >=9. We have moved the initialization logic of our code in to a separate zone https://github.com/elastic/apm-agent-rum-js/blob/859d328b51ee41c00aa9173bf44bd1161a644b4d/packages/rum-angular/src/apm.service.ts#L43-L45
Would this fix the problem that you are describing here?
I tried to do that first, but unfortunately it did not work for me, no.
I think the issue is, that this throttle is called from within the patched XHR and therefore always runs in the Angular zone.
When using your library with Angular, I noticed that the setTimeout() in your throttle() function is causing Angular to not get 'stable' for the given interval (default is 60s).
Angular considers itself 'unstable' when ZoneJS is seeing pending micro or macro tasks in the Angular zone. ('isStable' doc: https://angular.io/api/core/ApplicationRef#is-stable-examples)
The problem, as i understand it, here is that the throttle() gets executed within the Angular zone if Angular is present.
We use this 'isStable' flag for certain features that need to know if the given route has finished loading and the page rendering/processing is done.
So, as the general solution to this is, that you run your long running tasks outside of the Angular zone, my idea was to also run your throttle setTimeout function in a separate, new zone if there is window.Zone available.
Something like:
What do you guys think?