Skip to content
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

Web: Race condition with initializeFirebase #146

Open
ncoder opened this issue Dec 12, 2022 · 1 comment
Open

Web: Race condition with initializeFirebase #146

ncoder opened this issue Dec 12, 2022 · 1 comment

Comments

@ncoder
Copy link

ncoder commented Dec 12, 2022

Describe the bug
FirebaseAnaltyics behaves badly if don't wait for initializeFirebase to return, and call trackEvent.

To Reproduce

export async function initFirebase() {
    return FirebaseAnalytics.initializeFirebase(firebaseConfig).then(() => {
        FirebaseAnalytics.setCollectionEnabled({
            enabled: true,
        });
    })
}

export const firebaseReady = initFirebase()

## if you call FirebaseAnalytics.trackEvent() here, without waiting for firebaseReady, you'll get a warning that firebase was initialized twice, and the setCollectionEnabled call will fail.

Expected behavior
There are already some readyhandlers coded in FirebaseAnalyticsWeb. i expect them to take care of this situation automatically.
Eg:
If you call trackEvent before initializeFirebase, i expect them to error.
if you call trackEvent after initializeFirebase, but before it's complete... it should wait on the promise internally and exectute.

At a minimum, it should give a more appropriate error message.

version info
@capacitor-community/firebase-analytics: 4.0.0,
@capacitor/core: 4.5.0,

@georgiarnaudov
Copy link

Faced an identical issue. Here's how I tackled it. Hope it helps.

(async () => {
  dayjs.locale(activeLocale);

  if (Capacitor.getPlatform() === "web") {
    console.log("Firebase Analytics Initializing...");

    await FirebaseAnalytics.initializeFirebase({
      apiKey: "AIzaSyCUUQE4ntyjztCMSjWSpmoO0TtXHSCWop8",
      authDomain: "shredy-test.firebaseapp.com",
      databaseURL: "https://shredy-test.firebaseio.com",
      projectId: "shredy-test",
      storageBucket: "shredy-test.appspot.com",
      messagingSenderId: "799608757722",
      appId: "1:799608757722:web:ec6ed28de552a4bfe574e3",
      measurementId: "G-04CYHDSB4K",
    });

    await FirebaseAnalytics.setCollectionEnabled({
      enabled: true,
    });

    console.log("Firebase Analytics Initialized");
  }

  ReactDOM.render(<App />, document.getElementById("root"));

  // Call the element loader after the app has been rendered the first time
  defineCustomElements(window);
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants