-
Notifications
You must be signed in to change notification settings - Fork 18
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
Run async stuff outside Angular #196
Open
arturovt
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
arturovt:run-outside-angular
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
projects/applicationinsights-angularplugin-js/src/lib/run-outside-angular.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This would be exposed on the `globalThis` whenever `zone.js` is | ||
// included in the `polyfills` configuration property. Starting from Angular 17, | ||
// users can opt-in to use zoneless change detection. | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
declare const Zone: any; | ||
|
||
/** | ||
* The function that does the same job as `NgZone.runOutsideAngular`. | ||
* It doesn't require an injection context to be specified. | ||
* | ||
* ⚠️ Note: Most of the Application Insights functionality called from | ||
* inside the Angular execution context must be wrapped in this function. | ||
* Angular's rendering relies on asynchronous tasks being scheduled within | ||
* its execution context. | ||
* Since the plugin schedules tasks that do not interact with Angular's rendering, | ||
* it may prevent Angular from functioning reliably. Consequently, it may disrupt | ||
* processes such as server-side rendering or client-side hydration. | ||
*/ | ||
export const runOutsideAngular = <T>(callback: () => T): T => | ||
// Running the `callback` within the root execution context enables Angular | ||
// processes (such as SSR and hydration) to continue functioning normally without | ||
// timeouts and delays that could affect the user experience. | ||
typeof Zone !== "undefined" ? Zone.root.run(callback) : callback() | ||
; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This one is likely not needed as this is called internally from all of the
trackXXX
calls -- so (if I'm following your comments correctly) this should already be getting run in the "outside" context...Although, I do concede that if something does end up calling a track call within the Angular context (like any of the automatic events (JS errors, URL change etc)) then this would be the point at which it would switch to the different async context.
My only concern with this (which is less of an issue with any SPA framework) is that once the page starts to unload all operation "should" become synchronous to avoid the loss of events. And I don't think we current expose the internal flags indicating that we have received any unload signal (unload, beforeunload, pagehide and visibilitychange events)