Skip to content

Commit daac5be

Browse files
committed
add ios implementation for functions
1 parent 5d516e0 commit daac5be

File tree

4 files changed

+79
-72
lines changed

4 files changed

+79
-72
lines changed

firebase-auth/src/iosMain/kotlin/dev/teamhub/firebase/auth/auth.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import dev.teamhub.firebase.FirebaseApp
66
import dev.teamhub.firebase.FirebaseException
77
import kotlinx.cinterop.*
88
import kotlinx.coroutines.channels.awaitClose
9+
import kotlinx.coroutines.flow.*
910
import platform.Foundation.*
11+
import kotlinx.coroutines.CompletableDeferred
12+
1013

1114
actual val Firebase.auth
1215
get() = FirebaseAuth(FIRAuth.auth())
@@ -93,7 +96,6 @@ private suspend fun <T> T.await(function: T.(callback: (NSError?) -> Unit) -> Un
9396
job.await()
9497
}
9598

96-
9799
private fun NSError.toException() = when(domain) {
98100
FIRAuthErrorDomain -> when(code) {
99101
FIRAuthErrorCodeInvalidActionCode,

firebase-firestore/src/iosMain/kotlin/dev/teamhub/firebase/firestore/firestore.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.serialization.ImplicitReflectionSerializer
88
import kotlinx.serialization.SerializationStrategy
99
import kotlinx.serialization.DeserializationStrategy
1010

11-
actual open class FirebaseFirestoreException : FirebaseException()
11+
actual open class FirebaseFirestoreException(message: String) : FirebaseException(message)
1212

1313
actual val FirebaseFirestoreException.code: FirestoreExceptionCode get() = TODO("not implemented")
1414

firebase-functions/src/iosMain/kotlin/dev/teamhub/firebase/functions/FirebaseFunctions.kt

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dev.teamhub.firebase.functions
2+
3+
import cocoapods.FirebaseFunctions.*
4+
import dev.teamhub.firebase.Firebase
5+
import dev.teamhub.firebase.FirebaseApp
6+
import dev.teamhub.firebase.FirebaseException
7+
import kotlinx.serialization.DeserializationStrategy
8+
import kotlinx.serialization.SerializationStrategy
9+
import kotlinx.serialization.ImplicitReflectionSerializer
10+
import dev.teamhub.firebase.decode
11+
import dev.teamhub.firebase.encode
12+
import platform.Foundation.*
13+
import kotlinx.coroutines.CompletableDeferred
14+
15+
actual val Firebase.functions
16+
get() = FirebaseFunctions(FIRFunctions.functions())
17+
18+
actual fun Firebase.functions(region: String) =
19+
FirebaseFunctions(FIRFunctions.functionsForRegion(region))
20+
21+
actual fun Firebase.functions(app: FirebaseApp) =
22+
FirebaseFunctions(FIRFunctions.functionsForApp(app.ios))
23+
24+
actual fun Firebase.functions(app: FirebaseApp, region: String) =
25+
FirebaseFunctions(FIRFunctions.functionsForApp(app.ios, region))
26+
27+
actual class FirebaseFunctions internal constructor(val ios: FIRFunctions) {
28+
actual fun httpsCallable(name: String, timeout: Long?) =
29+
HttpsCallableReference(ios.HTTPSCallableWithName(name).apply { timeout?.let { setTimeoutInterval(it/1000.0) } })
30+
}
31+
32+
actual class HttpsCallableReference internal constructor(val ios: FIRHTTPSCallable) {
33+
actual suspend fun call() = HttpsCallableResult(ios.awaitResult { callWithCompletion(it) })
34+
35+
actual suspend inline fun <reified T> call(data: T) =
36+
HttpsCallableResult(ios.awaitResult { callWithObject(encode(data), it) })
37+
38+
actual suspend inline fun <reified T> call(strategy: SerializationStrategy<T>, data: T) =
39+
HttpsCallableResult(ios.awaitResult { callWithObject(encode(strategy, data), it) })
40+
}
41+
42+
actual class HttpsCallableResult constructor(val ios: FIRHTTPSCallableResult) {
43+
44+
actual inline fun <reified T> data() =
45+
decode<T>(value = ios.data)
46+
47+
actual inline fun <reified T> data(strategy: DeserializationStrategy<T>) =
48+
decode(strategy, ios.data)
49+
}
50+
51+
actual class FirebaseFunctionsException(message: String): FirebaseException(message)
52+
53+
private suspend fun <T> T.await(function: T.(callback: (NSError?) -> Unit) -> Unit) {
54+
val job = CompletableDeferred<Unit>()
55+
function { error ->
56+
if(error == null) {
57+
job.complete(Unit)
58+
} else {
59+
job.completeExceptionally(FirebaseFunctionsException(error.toString()))
60+
}
61+
}
62+
job.await()
63+
}
64+
65+
suspend fun <T, R> T.awaitResult(function: T.(callback: (R?, NSError?) -> Unit) -> Unit): R {
66+
val job = CompletableDeferred<R>()
67+
function { result, error ->
68+
if(result != null) {
69+
job.complete(result)
70+
} else if(error != null) {
71+
job.completeExceptionally(FirebaseFunctionsException(error.toString()))
72+
}
73+
}
74+
return job.await()
75+
}

0 commit comments

Comments
 (0)