Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions firebase-database/api/android/firebase-database.api
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class dev/gitlive/firebase/database/DatabaseReference : dev/gitlive
public final fun onDisconnect ()Ldev/gitlive/firebase/database/OnDisconnect;
public final fun push ()Ldev/gitlive/firebase/database/DatabaseReference;
public final fun removeValue (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun runTransaction (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun runTransaction (Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun runTransaction$default (Ldev/gitlive/firebase/database/DatabaseReference;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun setValue (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -65,6 +66,15 @@ public final class dev/gitlive/firebase/database/FirebaseDatabase {
public final class dev/gitlive/firebase/database/FirebaseDatabase$Companion {
}

public final class dev/gitlive/firebase/database/MutableData {
public final fun child (Ljava/lang/String;)Ldev/gitlive/firebase/database/MutableData;
public final fun getChildren ()Ljava/lang/Iterable;
public final fun getHasChildren ()Z
public final fun getKey ()Ljava/lang/String;
public final fun getValue ()Ljava/lang/Object;
public final fun setValue (Ljava/lang/Object;)V
}

public final class dev/gitlive/firebase/database/OnDisconnect {
public final fun cancel (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun removeValue (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -131,13 +141,34 @@ public final class dev/gitlive/firebase/database/ServerValueSerializer : kotlinx
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
}

public final class dev/gitlive/firebase/database/Transaction {
public final fun abort ()Ldev/gitlive/firebase/database/Transaction$Result;
public final fun success (Ldev/gitlive/firebase/database/MutableData;)Ldev/gitlive/firebase/database/Transaction$Result;
}

public abstract class dev/gitlive/firebase/database/Transaction$Result {
}

public final class dev/gitlive/firebase/database/Transaction$Result$Abort : dev/gitlive/firebase/database/Transaction$Result {
public static final field INSTANCE Ldev/gitlive/firebase/database/Transaction$Result$Abort;
}

public final class dev/gitlive/firebase/database/Transaction$Result$Success : dev/gitlive/firebase/database/Transaction$Result {
public final fun component1 ()Ldev/gitlive/firebase/database/MutableData;
public fun equals (Ljava/lang/Object;)Z
public final fun getData ()Ldev/gitlive/firebase/database/MutableData;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class dev/gitlive/firebase/database/databaseAndroid {
public static final fun database (Ldev/gitlive/firebase/Firebase;Ldev/gitlive/firebase/FirebaseApp;)Ldev/gitlive/firebase/database/FirebaseDatabase;
public static final fun database (Ldev/gitlive/firebase/Firebase;Ldev/gitlive/firebase/FirebaseApp;Ljava/lang/String;)Ldev/gitlive/firebase/database/FirebaseDatabase;
public static final fun database (Ldev/gitlive/firebase/Firebase;Ljava/lang/String;)Ldev/gitlive/firebase/database/FirebaseDatabase;
public static final fun getAndroid (Ldev/gitlive/firebase/database/DataSnapshot;)Lcom/google/firebase/database/DataSnapshot;
public static final fun getAndroid (Ldev/gitlive/firebase/database/DatabaseReference;)Lcom/google/firebase/database/DatabaseReference;
public static final fun getAndroid (Ldev/gitlive/firebase/database/FirebaseDatabase;)Lcom/google/firebase/database/FirebaseDatabase;
public static final fun getAndroid (Ldev/gitlive/firebase/database/MutableData;)Lcom/google/firebase/database/MutableData;
public static final fun getAndroid (Ldev/gitlive/firebase/database/OnDisconnect;)Lcom/google/firebase/database/OnDisconnect;
public static final fun getAndroid (Ldev/gitlive/firebase/database/Query;)Lcom/google/firebase/database/Query;
public static final fun getDatabase (Ldev/gitlive/firebase/Firebase;)Ldev/gitlive/firebase/database/FirebaseDatabase;
Expand Down
31 changes: 31 additions & 0 deletions firebase-database/api/jvm/firebase-database.api
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class dev/gitlive/firebase/database/DatabaseReference : dev/gitlive
public final fun onDisconnect ()Ldev/gitlive/firebase/database/OnDisconnect;
public final fun push ()Ldev/gitlive/firebase/database/DatabaseReference;
public final fun removeValue (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun runTransaction (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun runTransaction (Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun runTransaction$default (Ldev/gitlive/firebase/database/DatabaseReference;Lkotlinx/serialization/KSerializer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun setValue (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -65,6 +66,15 @@ public final class dev/gitlive/firebase/database/FirebaseDatabase {
public final class dev/gitlive/firebase/database/FirebaseDatabase$Companion {
}

public final class dev/gitlive/firebase/database/MutableData {
public final fun child (Ljava/lang/String;)Ldev/gitlive/firebase/database/MutableData;
public final fun getChildren ()Ljava/lang/Iterable;
public final fun getHasChildren ()Z
public final fun getKey ()Ljava/lang/String;
public final fun getValue ()Ljava/lang/Object;
public final fun setValue (Ljava/lang/Object;)V
}

public final class dev/gitlive/firebase/database/OnDisconnect {
public final fun cancel (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun removeValue (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -131,13 +141,34 @@ public final class dev/gitlive/firebase/database/ServerValueSerializer : kotlinx
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
}

public final class dev/gitlive/firebase/database/Transaction {
public final fun abort ()Ldev/gitlive/firebase/database/Transaction$Result;
public final fun success (Ldev/gitlive/firebase/database/MutableData;)Ldev/gitlive/firebase/database/Transaction$Result;
}

public abstract class dev/gitlive/firebase/database/Transaction$Result {
}

public final class dev/gitlive/firebase/database/Transaction$Result$Abort : dev/gitlive/firebase/database/Transaction$Result {
public static final field INSTANCE Ldev/gitlive/firebase/database/Transaction$Result$Abort;
}

public final class dev/gitlive/firebase/database/Transaction$Result$Success : dev/gitlive/firebase/database/Transaction$Result {
public final fun component1 ()Ldev/gitlive/firebase/database/MutableData;
public fun equals (Ljava/lang/Object;)Z
public final fun getData ()Ldev/gitlive/firebase/database/MutableData;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class dev/gitlive/firebase/database/databaseAndroid {
public static final fun database (Ldev/gitlive/firebase/Firebase;Ldev/gitlive/firebase/FirebaseApp;)Ldev/gitlive/firebase/database/FirebaseDatabase;
public static final fun database (Ldev/gitlive/firebase/Firebase;Ldev/gitlive/firebase/FirebaseApp;Ljava/lang/String;)Ldev/gitlive/firebase/database/FirebaseDatabase;
public static final fun database (Ldev/gitlive/firebase/Firebase;Ljava/lang/String;)Ldev/gitlive/firebase/database/FirebaseDatabase;
public static final fun getAndroid (Ldev/gitlive/firebase/database/DataSnapshot;)Lcom/google/firebase/database/DataSnapshot;
public static final fun getAndroid (Ldev/gitlive/firebase/database/DatabaseReference;)Lcom/google/firebase/database/DatabaseReference;
public static final fun getAndroid (Ldev/gitlive/firebase/database/FirebaseDatabase;)Lcom/google/firebase/database/FirebaseDatabase;
public static final fun getAndroid (Ldev/gitlive/firebase/database/MutableData;)Lcom/google/firebase/database/MutableData;
public static final fun getAndroid (Ldev/gitlive/firebase/database/OnDisconnect;)Lcom/google/firebase/database/OnDisconnect;
public static final fun getAndroid (Ldev/gitlive/firebase/database/Query;)Lcom/google/firebase/database/Query;
public static final fun getDatabase (Ldev/gitlive/firebase/Firebase;)Ldev/gitlive/firebase/database/FirebaseDatabase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.google.android.gms.tasks.Task
import com.google.firebase.database.ChildEventListener
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.Logger
import com.google.firebase.database.MutableData
import com.google.firebase.database.Transaction
import com.google.firebase.database.ValueEventListener
import dev.gitlive.firebase.DecodeSettings
import dev.gitlive.firebase.EncodeDecodeSettingsBuilder
Expand Down Expand Up @@ -233,9 +231,9 @@ internal actual class NativeDatabaseReference internal constructor(
@OptIn(ExperimentalSerializationApi::class)
actual suspend fun <T> runTransaction(strategy: KSerializer<T>, buildSettings: EncodeDecodeSettingsBuilder.() -> Unit, transactionUpdate: (currentData: T) -> T): DataSnapshot {
val deferred = CompletableDeferred<DataSnapshot>()
android.runTransaction(object : Transaction.Handler {
android.runTransaction(object : com.google.firebase.database.Transaction.Handler {

override fun doTransaction(currentData: MutableData): Transaction.Result {
override fun doTransaction(currentData: com.google.firebase.database.MutableData): com.google.firebase.database.Transaction.Result {
val valueToReencode = currentData.value
// Value may be null initially, so only reencode if this is allowed
if (strategy.descriptor.isNullable || valueToReencode != null) {
Expand All @@ -246,7 +244,7 @@ internal actual class NativeDatabaseReference internal constructor(
transactionUpdate,
)
}
return Transaction.success(currentData)
return com.google.firebase.database.Transaction.success(currentData)
}

override fun onComplete(
Expand All @@ -260,13 +258,50 @@ internal actual class NativeDatabaseReference internal constructor(
deferred.complete(DataSnapshot(snapshot!!, persistenceEnabled))
}
}
})
}, false)
return deferred.await()
}

actual suspend fun runTransaction(update: Transaction.(MutableData) -> Transaction.Result): DataSnapshot? {
val deferred = CompletableDeferred<DataSnapshot?>()
android.runTransaction(
/* handler = */
object : com.google.firebase.database.Transaction.Handler {
override fun doTransaction(currentData: com.google.firebase.database.MutableData): com.google.firebase.database.Transaction.Result {
val mutableData = MutableData(currentData)
return when (val result = Transaction().update(mutableData)) {
is Transaction.Result.Success ->
com.google.firebase.database.Transaction.success(result.data.android)

Transaction.Result.Abort ->
com.google.firebase.database.Transaction.abort()
}
}

override fun onComplete(
error: DatabaseError?,
committed: Boolean,
snapshot: com.google.firebase.database.DataSnapshot?,
) {
if (error != null) {
deferred.completeExceptionally(error.toException())
} else if (committed) {
deferred.complete(DataSnapshot(snapshot!!, persistenceEnabled))
} else {
deferred.complete(null)
}
}
},
/* fireLocalEvents = */
false,
)
return deferred.await()
}
}

public val DatabaseReference.android: com.google.firebase.database.DatabaseReference get() = nativeReference.android
public val DataSnapshot.android: com.google.firebase.database.DataSnapshot get() = android
public val MutableData.android: com.google.firebase.database.MutableData get() = android

public actual class DataSnapshot internal constructor(
internal val android: com.google.firebase.database.DataSnapshot,
Expand All @@ -290,6 +325,21 @@ public actual class DataSnapshot internal constructor(
public actual val children: Iterable<DataSnapshot> get() = android.children.map { DataSnapshot(it, persistenceEnabled) }
}

public actual class MutableData internal constructor(
internal val android: com.google.firebase.database.MutableData
) {
public actual val key: String? get() = android.key

public actual var value: Any?
get() = android.value
set(value) { android.value = value }

public actual fun child(path: String): MutableData = MutableData(android.child(path))
public actual val hasChildren: Boolean get() = android.hasChildren()
public actual val children: Iterable<MutableData>
get() = android.children.map { MutableData(it) }
}

internal actual class NativeOnDisconnect internal constructor(
val android: com.google.firebase.database.OnDisconnect,
val persistenceEnabled: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import cocoapods.FirebaseDatabase.FIRDataSnapshot
import cocoapods.FirebaseDatabase.FIRDatabase
import cocoapods.FirebaseDatabase.FIRDatabaseQuery
import cocoapods.FirebaseDatabase.FIRDatabaseReference
import cocoapods.FirebaseDatabase.FIRMutableData
import cocoapods.FirebaseDatabase.FIRTransactionResult
import dev.gitlive.firebase.DecodeSettings
import dev.gitlive.firebase.EncodeDecodeSettingsBuilder
Expand Down Expand Up @@ -42,6 +43,7 @@ import kotlinx.coroutines.selects.select
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import platform.Foundation.NSError
import platform.Foundation.NSNull
import platform.Foundation.allObjects

public val FirebaseDatabase.ios: FIRDatabase get() = FIRDatabase.database()
Expand Down Expand Up @@ -210,10 +212,38 @@ internal actual class NativeDatabaseReference internal constructor(
)
return deferred.await()
}

actual suspend fun runTransaction(update: Transaction.(MutableData) -> Transaction.Result): DataSnapshot? {
val deferred = CompletableDeferred<DataSnapshot?>()
ios.runTransactionBlock(
block = { fIRMutableData ->
val mutableData = MutableData(fIRMutableData!!)
when(val result = Transaction().update(mutableData)) {
is Transaction.Result.Success ->
FIRTransactionResult.successWithValue(result.data.ios)

Transaction.Result.Abort ->
FIRTransactionResult.abort()
}
},
andCompletionBlock = { error, committed, snapshot ->
if (error != null) {
deferred.completeExceptionally(DatabaseException(error.toString(), null))
} else if (committed) {
deferred.complete(DataSnapshot(snapshot!!, persistenceEnabled))
} else {
deferred.complete(null)
}
},
withLocalEvents = false
)
return deferred.await()
}
}

public val DatabaseReference.ios: FIRDatabaseReference get() = nativeReference.ios
public val DataSnapshot.ios: FIRDataSnapshot get() = ios
public val MutableData.ios: FIRMutableData get() = ios

public actual class DataSnapshot internal constructor(
internal val ios: FIRDataSnapshot,
Expand All @@ -237,6 +267,22 @@ public actual class DataSnapshot internal constructor(
public actual val children: Iterable<DataSnapshot> get() = ios.children.allObjects.map { DataSnapshot(it as FIRDataSnapshot, persistenceEnabled) }
}

public actual class MutableData internal constructor(
internal val ios: FIRMutableData
) {
public actual val key: String? get() = ios.key

public actual var value: Any?
get() = ios.value?.takeIf { it !is NSNull }
set(value) { ios.value = value }

public actual fun child(path: String): MutableData = MutableData(ios.childDataByAppendingPath(path))

public actual val hasChildren: Boolean get() = ios.hasChildren()
public actual val children: Iterable<MutableData>
get() = ios.children.allObjects.map { MutableData(it as FIRMutableData) }
}

internal actual class NativeOnDisconnect internal constructor(
val ios: FIRDatabaseReference,
val persistenceEnabled: Boolean,
Expand Down
Loading