From be243a99d701371f10195375aa06fa0d1c2ea3c5 Mon Sep 17 00:00:00 2001 From: Gijs van Veen Date: Tue, 9 Jul 2024 17:46:17 +0200 Subject: [PATCH] Fix for #551 --- .../gitlive/firebase/firestore/firestore.kt | 9 +++++++ .../NativeFirebaseFirestoreWrapper.kt | 24 +++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt b/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt index 584d01140..1e932a1fe 100644 --- a/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt +++ b/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt @@ -5,6 +5,7 @@ package dev.gitlive.firebase.firestore import dev.gitlive.firebase.Firebase +import dev.gitlive.firebase.FirebaseApp import dev.gitlive.firebase.FirebaseOptions import dev.gitlive.firebase.apps import dev.gitlive.firebase.internal.decode @@ -85,6 +86,7 @@ class FirebaseFirestoreTest { ) } + lateinit var firebaseApp: FirebaseApp lateinit var firestore: FirebaseFirestore @BeforeTest @@ -100,6 +102,7 @@ class FirebaseFirestoreTest { gcmSenderId = "846484016111", ), ) + firebaseApp = app firestore = Firebase.firestore(app).apply { useEmulator(emulatorHost, 8080) @@ -1031,6 +1034,12 @@ class FirebaseFirestoreTest { fieldQuery.assertDocuments(FirestoreTest.serializer(), testOne) } + @Test + fun testMultiple() = runTest { + Firebase.firestore(firebaseApp).disableNetwork() + Firebase.firestore(firebaseApp).enableNetwork() + } + private suspend fun setupFirestoreData( documentOne: FirestoreTest = testOne, documentTwo: FirestoreTest = testTwo, diff --git a/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/internal/NativeFirebaseFirestoreWrapper.kt b/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/internal/NativeFirebaseFirestoreWrapper.kt index 919286d00..5ca836e1d 100644 --- a/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/internal/NativeFirebaseFirestoreWrapper.kt +++ b/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/internal/NativeFirebaseFirestoreWrapper.kt @@ -10,6 +10,7 @@ import dev.gitlive.firebase.firestore.NativeWriteBatch import dev.gitlive.firebase.firestore.externals.clearIndexedDbPersistence import dev.gitlive.firebase.firestore.externals.connectFirestoreEmulator import dev.gitlive.firebase.firestore.externals.doc +import dev.gitlive.firebase.firestore.externals.getFirestore import dev.gitlive.firebase.firestore.externals.initializeFirestore import dev.gitlive.firebase.firestore.externals.setLogLevel import dev.gitlive.firebase.firestore.externals.writeBatch @@ -20,28 +21,41 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.await import kotlinx.coroutines.promise +// There is currently no way to check whether Firestore was already initialized for a given app without actually initializing it +// Therefore we keep track of this internally +private val appsWithFirestore = mutableListOf() + internal actual class NativeFirebaseFirestoreWrapper internal constructor( private val createNative: NativeFirebaseFirestoreWrapper.() -> NativeFirebaseFirestore, + private val canUpdateSettings: () -> Boolean, ) { - internal actual constructor(native: NativeFirebaseFirestore) : this({ native }) + internal actual constructor(native: NativeFirebaseFirestore) : this({ native }, { false }) internal constructor(app: FirebaseApp) : this( { NativeFirebaseFirestore( - initializeFirestore(app, settings.js).also { - emulatorSettings?.run { - connectFirestoreEmulator(it, host, port) + if (appsWithFirestore.contains(app)) { + getFirestore(app) + } else { + initializeFirestore(app, settings.js).also { + emulatorSettings?.run { + connectFirestoreEmulator(it, host, port) + } + appsWithFirestore.add(app) } }, ) }, + { + !appsWithFirestore.contains(app) + }, ) private data class EmulatorSettings(val host: String, val port: Int) actual var settings: FirebaseFirestoreSettings = FirebaseFirestoreSettings.Builder().build() set(value) { - if (lazyNative.isInitialized()) { + if (lazyNative.isInitialized() || !canUpdateSettings()) { throw IllegalStateException("FirebaseFirestore has already been started and its settings can no longer be changed. You can only call setFirestoreSettings() before calling any other methods on a FirebaseFirestore object.") } else { field = value