-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Adding more injector safety #3590
Adding more injector safety #3590
Conversation
@jamesdaniels Just a heads up... I've just installed RC4 and noticed the "Firebase API called outside injection context" warning in the console. The error message also includes a link to "Find out more" pointing to https://github.com/angular/angularfire/blob/main/docs/zones.md (see line 87 in zone.ts) but this file doesn't appear to exist at the moment. |
@davidgeary thanks yeah, getting that page together now. You can likely safely ignore the warnings, they're only produced in dev mode and are intended to help developers track down any change-detection / rehydration instabilities |
@jamesdaniels Ah, thanks. I was just starting to work through my code to see where the problem was, so you've saved me some time there! |
Apparently, once you await something inside an injection context, you lose the injection context. Here's our workaround: /**
* Runs an async function in the injection context. This can be awaited, unlike @see {runInInjectionContext}.
* For some ungodly reason, only the first awaited call inside the fn callback is actually inside the injection context.
* After something is awaited, the context is lost.
*
* NOTE: Use this sparingly and only when absolutely necessary.
* This is a band-aid solution for this issue:
* https://github.com/angular/angularfire/pull/3590
*
* @param injector The injector, usually inject(EnvironmentInjector)
* @param fn The async callback to be awaited
*/
export async function runAsyncInInjectionContext<T>(injector: Injector, fn: () => Promise<T>): Promise<T> {
return await runInInjectionContext(injector, () => {
return new Promise((resolve, reject) => {
fn().then(resolve).catch(reject);
});
});
} Usage: await runAsyncInInjectionContext(this.injector, async () => {
await loadBundle(this.firestore, response);
await getDocs(); // <-- this no longer is inside context
}); |
zoneWrapFn
beforeAuthStateChanged
should not block