From 5a286536b78b645c79f53f5a4a6da2b3beae4e1d Mon Sep 17 00:00:00 2001 From: Gijs van Veen Date: Fri, 19 Apr 2024 09:51:31 +0200 Subject: [PATCH] Fixed failing test --- .../dev/gitlive/firebase/firestore/firestore.kt | 2 +- .../dev/gitlive/firebase/firestore/firestore.kt | 3 ++- .../firebase/firestore/FirestoreSourceTest.kt | 8 +++++++- .../dev/gitlive/firebase/firestore/firestore.kt | 2 +- .../dev/gitlive/firebase/firestore/firestore.kt | 12 ++++++++++-- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt b/firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt index 35aca8603..eb73173e5 100644 --- a/firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt +++ b/firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt @@ -57,7 +57,7 @@ val LocalCacheSettings.android: com.google.firebase.firestore.LocalCacheSettings private val callbackExecutorMap = ConcurrentHashMap() actual typealias NativeFirebaseFirestore = com.google.firebase.firestore.FirebaseFirestore -actual internal class NativeFirebaseFirestoreWrapper actual constructor(actual val native: NativeFirebaseFirestore) { +internal actual class NativeFirebaseFirestoreWrapper actual constructor(actual val native: NativeFirebaseFirestore) { actual var settings: FirebaseFirestoreSettings get() = with(native.firestoreSettings) { diff --git a/firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt b/firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt index 840d649b4..6afe7dd7a 100644 --- a/firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt +++ b/firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt @@ -40,7 +40,8 @@ class FirebaseFirestore internal constructor(private val wrapper: NativeFirebase constructor(native: NativeFirebaseFirestore) : this(NativeFirebaseFirestoreWrapper(native)) - val native = wrapper.native + // Important to leave this as a get property since on JS it is initialized lazily + val native get() = wrapper.native var settings: FirebaseFirestoreSettings get() = wrapper.settings set(value) { diff --git a/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/FirestoreSourceTest.kt b/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/FirestoreSourceTest.kt index 439d459e7..8d849900b 100644 --- a/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/FirestoreSourceTest.kt +++ b/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/FirestoreSourceTest.kt @@ -39,7 +39,13 @@ class FirestoreSourceTest { firestore = Firebase.firestore(app).apply { useEmulator(emulatorHost, 8080) - setSettings(persistenceEnabled = persistenceEnabled) + settings = firestoreSettings(settings) { + cacheSettings = if (persistenceEnabled) { + persistentCacheSettings { } + } else { + memoryCacheSettings { } + } + } } } diff --git a/firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt b/firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt index e094e6f51..266f77e35 100644 --- a/firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt +++ b/firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt @@ -39,7 +39,7 @@ val LocalCacheSettings.ios: FIRLocalCacheSettingsProtocol get() = when (this) { actual typealias NativeFirebaseFirestore = FIRFirestore @Suppress("UNCHECKED_CAST") -actual internal class NativeFirebaseFirestoreWrapper internal actual constructor(actual val native: NativeFirebaseFirestore) { +internal actual class NativeFirebaseFirestoreWrapper internal actual constructor(actual val native: NativeFirebaseFirestore) { actual var settings: FirebaseFirestoreSettings = firestoreSettings { }.also { native.settings = it.ios diff --git a/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt b/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt index 19982b7e3..4600481aa 100644 --- a/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt +++ b/firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt @@ -78,7 +78,7 @@ actual fun Firebase.firestore(app: FirebaseApp) = actual data class NativeFirebaseFirestore(val js: JsFirestore) -actual internal class NativeFirebaseFirestoreWrapper internal constructor( +internal actual class NativeFirebaseFirestoreWrapper internal constructor( private val createNative: NativeFirebaseFirestoreWrapper.() -> NativeFirebaseFirestore ){ @@ -98,13 +98,21 @@ actual internal class NativeFirebaseFirestoreWrapper internal constructor( private data class EmulatorSettings(val host: String, val port: Int) actual var settings: FirebaseFirestoreSettings = FirebaseFirestoreSettings.Builder().build() + set(value) { + if (lazyNative.isInitialized()) { + 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 + } + } private var emulatorSettings: EmulatorSettings? = null // initializeFirestore must be called before any call, including before `getFirestore()` // To allow settings to be updated, we defer creating the wrapper until the first call to `native` - actual val native: NativeFirebaseFirestore by lazy { + private val lazyNative = lazy { createNative() } + actual val native: NativeFirebaseFirestore by lazyNative private val js get() = native.js actual fun collection(collectionPath: String) = rethrow { NativeCollectionReference(jsCollection(js, collectionPath)) }