Skip to content

Commit

Permalink
Fix for GitLiveApp#551
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Jul 9, 2024
1 parent 457926f commit be243a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,6 +86,7 @@ class FirebaseFirestoreTest {
)
}

lateinit var firebaseApp: FirebaseApp
lateinit var firestore: FirebaseFirestore

@BeforeTest
Expand All @@ -100,6 +102,7 @@ class FirebaseFirestoreTest {
gcmSenderId = "846484016111",
),
)
firebaseApp = app

firestore = Firebase.firestore(app).apply {
useEmulator(emulatorHost, 8080)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<FirebaseApp>()

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
Expand Down

0 comments on commit be243a9

Please sign in to comment.