Skip to content

Fire onInit callback if instance is already available #4971

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

Merged
merged 14 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cold-llamas-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/component": patch
"@firebase/firestore": patch
---

Fixes a regression that prevented Firestore from detecting Auth during its initial initialization, which could cause some writes to not be send.
26 changes: 17 additions & 9 deletions packages/component/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Provider<T extends Name> {
* @param identifier A provider can provide mulitple instances of a service
* if this.component.multipleInstances is true.
*/
get(identifier: string = DEFAULT_ENTRY_NAME): Promise<NameServiceMapping[T]> {
get(identifier?: string): Promise<NameServiceMapping[T]> {
// if multipleInstances is not supported, use the default name
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);

Expand Down Expand Up @@ -99,11 +99,11 @@ export class Provider<T extends Name> {
identifier?: string;
optional?: boolean;
}): NameServiceMapping[T] | null {
const identifier = options?.identifier ?? DEFAULT_ENTRY_NAME;
const optional = options?.optional ?? false;

// if multipleInstances is not supported, use the default name
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
const normalizedIdentifier = this.normalizeInstanceIdentifier(
options?.identifier
);
const optional = options?.optional ?? false;

if (
this.isInitialized(normalizedIdentifier) ||
Expand Down Expand Up @@ -219,9 +219,9 @@ export class Provider<T extends Name> {
}

initialize(opts: InitializeOptions = {}): NameServiceMapping[T] {
const { instanceIdentifier = DEFAULT_ENTRY_NAME, options = {} } = opts;
const { options = {} } = opts;
const normalizedIdentifier = this.normalizeInstanceIdentifier(
instanceIdentifier
opts.instanceIdentifier
);
if (this.isInitialized(normalizedIdentifier)) {
throw Error(
Expand Down Expand Up @@ -263,9 +263,15 @@ export class Provider<T extends Name> {
*
* @returns a function to unregister the callback
*/
onInit(callback: OnInitCallBack<T>): () => void {
onInit(callback: OnInitCallBack<T>, identifier?: string): () => void {
this.onInitCallbacks.add(callback);

const instance = this.getImmediate({ identifier, optional: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment that OnInit() is only intended to be used for EXPLICIT components, so getImmediate() will never initialize a new instance?

if (instance) {
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
callback(instance, normalizedIdentifier);
}

return () => {
this.onInitCallbacks.delete(callback);
};
Expand Down Expand Up @@ -324,7 +330,9 @@ export class Provider<T extends Name> {
return instance || null;
}

private normalizeInstanceIdentifier(identifier: string): string {
private normalizeInstanceIdentifier(
identifier: string = DEFAULT_ENTRY_NAME
): string {
if (this.component) {
return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME;
} else {
Expand Down
3 changes: 3 additions & 0 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
this.invokeChangeListener = true;
this.asyncQueue = asyncQueue;
this.changeListener = changeListener;
if (this.auth) {
this.awaitTokenAndRaiseInitialEvent();
}
}

removeChangeListener(): void {
Expand Down