Skip to content

Commit 9e3be34

Browse files
committed
remove awaitExpectedResult
1 parent e224920 commit 9e3be34

File tree

7 files changed

+26
-54
lines changed

7 files changed

+26
-54
lines changed

firebase-auth/src/commonTest/kotlin/dev/gitlive/firebase/auth/auth.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class FirebaseAuthTest {
5454
fun testFetchSignInMethods() = runTest {
5555
val email = "test+${Random.nextInt(100000)}@test.com"
5656
var signInMethodResult = Firebase.auth.fetchSignInMethodsForEmail(email)
57-
assertEquals(emptyList(), signInMethodResult.signInMethods)
57+
assertEquals(emptyList(), signInMethodResult)
5858
Firebase.auth.createUserWithEmailAndPassword(email, "test123")
5959
signInMethodResult = Firebase.auth.fetchSignInMethodsForEmail(email)
60-
assertEquals(listOf("password"), signInMethodResult.signInMethods)
60+
assertEquals(listOf("password"), signInMethodResult)
6161

6262
Firebase.auth.signInWithEmailAndPassword(email, "test123").user!!.delete()
6363
}
@@ -85,7 +85,7 @@ class FirebaseAuthTest {
8585

8686
@Test
8787
fun testSignInWithCredential() = runTest {
88-
val credential = EmailAuthProvider.credentialWithEmail("[email protected]", "test123")
88+
val credential = EmailAuthProvider.credential("[email protected]", "test123")
8989
val result = Firebase.auth.signInWithCredential(credential)
9090
assertEquals("mn8kgIFnxLO7il8GpTa5g0ObP6I2", result.user!!.uid)
9191

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

+11-39
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ actual class FirebaseAuth internal constructor(val ios: FIRAuth) {
4545
actual suspend fun confirmPasswordReset(code: String, newPassword: String) = ios.await { confirmPasswordResetWithCode(code, newPassword, it) }.run { Unit }
4646

4747
actual suspend fun createUserWithEmailAndPassword(email: String, password: String) =
48-
AuthResult(ios.awaitExpectedResult { createUserWithEmail(email = email, password = password, completion = it) })
48+
AuthResult(ios.awaitResult { createUserWithEmail(email = email, password = password, completion = it) })
4949

50-
actual suspend fun fetchSignInMethodsForEmail(email: String) = ios.awaitResult { fetchSignInMethodsForEmail(email, it) }.orEmpty()
50+
actual suspend fun fetchSignInMethodsForEmail(email: String) = ios.awaitResult { fetchSignInMethodsForEmail(email, it) } as List<String>
5151

5252
actual suspend fun sendPasswordResetEmail(email: String, actionCodeSettings: ActionCodeSettings?) {
5353
ios.await { actionCodeSettings?.let { actionSettings -> sendPasswordResetWithEmail(email, actionSettings.toIos(), it) } ?: sendPasswordResetWithEmail(email = email, completion = it) }
@@ -56,24 +56,24 @@ actual class FirebaseAuth internal constructor(val ios: FIRAuth) {
5656
actual suspend fun sendSignInLinkToEmail(email: String, actionCodeSettings: ActionCodeSettings) = ios.await { sendSignInLinkToEmail(email, actionCodeSettings.toIos(), it) }.run { Unit }
5757

5858
actual suspend fun signInWithEmailAndPassword(email: String, password: String) =
59-
AuthResult(ios.awaitExpectedResult { signInWithEmail(email = email, password = password, completion = it) })
59+
AuthResult(ios.awaitResult { signInWithEmail(email = email, password = password, completion = it) })
6060

6161
actual suspend fun signInWithCustomToken(token: String) =
62-
AuthResult(ios.awaitExpectedResult { signInWithCustomToken(token, it) })
62+
AuthResult(ios.awaitResult { signInWithCustomToken(token, it) })
6363

6464
actual suspend fun signInAnonymously() =
65-
AuthResult(ios.awaitExpectedResult { signInAnonymouslyWithCompletion(it) })
65+
AuthResult(ios.awaitResult { signInAnonymouslyWithCompletion(it) })
6666

6767
actual suspend fun signInWithCredential(authCredential: AuthCredential) =
68-
AuthResult(ios.awaitExpectedResult { signInWithCredential(authCredential.ios, it) })
68+
AuthResult(ios.awaitResult { signInWithCredential(authCredential.ios, it) })
6969

7070
actual suspend fun signOut() = ios.throwError { signOut(it) }.run { Unit }
7171

7272
actual suspend fun updateCurrentUser(user: FirebaseUser) = ios.await { updateCurrentUser(user.ios, it) }.run { Unit }
73-
actual suspend fun verifyPasswordResetCode(code: String): String = ios.awaitExpectedResult { verifyPasswordResetCode(code, it) }
73+
actual suspend fun verifyPasswordResetCode(code: String): String = ios.awaitResult { verifyPasswordResetCode(code, it) }
7474

7575
actual suspend fun <T : ActionCodeResult> checkActionCode(code: String): T {
76-
val result = ios.awaitExpectedResult { checkActionCode(code, it) }
76+
val result = ios.awaitResult { checkActionCode(code, it) }
7777
@Suppress("UNCHECKED_CAST")
7878
return when(result.operation) {
7979
FIRActionCodeOperationUnknown -> Error
@@ -93,7 +93,7 @@ actual class AuthResult internal constructor(val ios: FIRAuthDataResult) {
9393
get() = FirebaseUser(ios.user)
9494
}
9595

96-
internal fun ActionCodeSettings.toIos() = FIRActionCodeSettings().let {
96+
internal fun ActionCodeSettings.toIos() = FIRActionCodeSettings().also {
9797
it.URL = NSURL.URLWithString(url)
9898
androidPackageName?.run { it.setAndroidPackageName(androidPackageName, installIfNotAvailable, minimumVersion) }
9999
it.dynamicLinkDomain = dynamicLinkDomain
@@ -127,41 +127,13 @@ internal fun <T, R> T.throwError(block: T.(errorPointer: CPointer<ObjCObjectVar<
127127
}
128128
}
129129

130-
internal suspend fun <T, R> T.awaitResult(function: T.(callback: (R?, NSError?) -> Unit) -> Unit): R? {
131-
val job = CompletableDeferred<R?>()
132-
function { result, error ->
133-
if(error != null) {
134-
job.completeExceptionally(error.toException())
135-
} else {
136-
job.complete(result)
137-
}
138-
}
139-
return job.await()
140-
}
141-
142-
internal suspend fun <T, R> T.awaitResult(default: R, function: T.(callback: (R?, NSError?) -> Unit) -> Unit): R {
130+
internal suspend fun <T, R> T.awaitResult(function: T.(callback: (R, NSError?) -> Unit) -> Unit): R {
143131
val job = CompletableDeferred<R>()
144132
function { result, error ->
145-
if(result != null) {
133+
if(error == null) {
146134
job.complete(result)
147-
} else if(error != null) {
148-
job.completeExceptionally(error.toException())
149135
} else {
150-
job.complete(default)
151-
}
152-
}
153-
return job.await()
154-
}
155-
156-
internal suspend fun <T, R> T.awaitExpectedResult(function: T.(callback: (R?, NSError?) -> Unit) -> Unit): R {
157-
val job = CompletableDeferred<R>()
158-
function { result, error ->
159-
if(result != null) {
160-
job.complete(result)
161-
} else if(error != null) {
162136
job.completeExceptionally(error.toException())
163-
} else {
164-
job.completeExceptionally(UnexpectedNullResultException())
165137
}
166138
}
167139
return job.await()

firebase-auth/src/iosMain/kotlin/dev/gitlive/firebase/auth/credentials.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ actual class OAuthProvider(val ios: FIROAuthProvider, private val auth: Firebase
6969
null}.toMap()
7070
}
7171

72-
actual suspend fun signIn(signInProvider: SignInProvider): AuthResult = AuthResult(ios.awaitExpectedResult { auth.ios.signInWithProvider(ios, signInProvider.delegate, it) })
72+
actual suspend fun signIn(signInProvider: SignInProvider): AuthResult = AuthResult(ios.awaitResult { auth.ios.signInWithProvider(ios, signInProvider.delegate, it) })
7373
}
7474

7575
actual class SignInProvider(val delegate: FIRAuthUIDelegateProtocol)
@@ -80,7 +80,7 @@ actual class PhoneAuthProvider(val ios: FIRPhoneAuthProvider) {
8080

8181
actual fun credential(verificationId: String, smsCode: String): PhoneAuthCredential = PhoneAuthCredential(ios.credentialWithVerificationID(verificationId, smsCode))
8282
actual suspend fun verifyPhoneNumber(phoneNumber: String, verificationProvider: PhoneVerificationProvider): AuthCredential {
83-
val verificationId: String = ios.awaitExpectedResult { ios.verifyPhoneNumber(phoneNumber, verificationProvider.delegate, it) }
83+
val verificationId: String = ios.awaitResult { ios.verifyPhoneNumber(phoneNumber, verificationProvider.delegate, it) }
8484
val verificationCode = verificationProvider.getVerificationCode()
8585
return credential(verificationId, verificationCode)
8686
}

firebase-auth/src/iosMain/kotlin/dev/gitlive/firebase/auth/multifactor.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ actual class MultiFactor(val ios: FIRMultiFactor) {
1010
actual val enrolledFactors: List<MultiFactorInfo>
1111
get() = ios.enrolledFactors.mapNotNull { info -> (info as? FIRMultiFactorInfo)?.let{ MultiFactorInfo(it) } }
1212
actual suspend fun enroll(multiFactorAssertion: MultiFactorAssertion, displayName: String?) = ios.await { enrollWithAssertion(multiFactorAssertion.ios, displayName, it) }.run { Unit }
13-
actual suspend fun getSession(): MultiFactorSession = MultiFactorSession(ios.awaitExpectedResult { getSessionWithCompletion(completion = it) })
13+
actual suspend fun getSession(): MultiFactorSession = MultiFactorSession(ios.awaitResult { getSessionWithCompletion(completion = it) })
1414
actual suspend fun unenroll(multiFactorInfo: MultiFactorInfo) = ios.await { unenrollWithInfo(multiFactorInfo.ios, it) }.run { Unit }
1515
actual suspend fun unenroll(factorUid: String) = ios.await { unenrollWithFactorUID(factorUid, it) }.run { Unit }
1616
}
@@ -38,5 +38,5 @@ actual class MultiFactorResolver(val ios: FIRMultiFactorResolver) {
3838
actual val hints: List<MultiFactorInfo> = ios.hints.mapNotNull { hint -> (hint as? FIRMultiFactorInfo)?.let { MultiFactorInfo(it) } }
3939
actual val session: MultiFactorSession = MultiFactorSession(ios.session)
4040

41-
actual suspend fun resolveSignIn(assertion: MultiFactorAssertion): AuthResult = AuthResult(ios.awaitExpectedResult { resolveSignInWithAssertion(assertion.ios, it) })
41+
actual suspend fun resolveSignIn(assertion: MultiFactorAssertion): AuthResult = AuthResult(ios.awaitResult { resolveSignInWithAssertion(assertion.ios, it) })
4242
}

firebase-auth/src/iosMain/kotlin/dev/gitlive/firebase/auth/user.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ actual class FirebaseUser internal constructor(val ios: FIRUser) {
3939
ios.awaitResult { getIDTokenForcingRefresh(forceRefresh, it) }
4040

4141
actual suspend fun linkWithCredential(credential: AuthCredential): AuthResult =
42-
AuthResult(ios.awaitExpectedResult { linkWithCredential(credential.ios, it) })
42+
AuthResult(ios.awaitResult { linkWithCredential(credential.ios, it) })
4343

4444
actual suspend fun reauthenticate(credential: AuthCredential) =
45-
ios.awaitExpectedResult { reauthenticateWithCredential(credential.ios, it) }.run { Unit }
45+
ios.await { reauthenticateWithCredential(credential.ios, it) }
4646

4747
actual suspend fun reauthenticateAndRetrieveData(credential: AuthCredential): AuthResult =
48-
AuthResult(ios.awaitExpectedResult { reauthenticateAndRetrieveDataWithCredential(credential.ios, it) })
48+
AuthResult(ios.awaitResult { reauthenticateAndRetrieveDataWithCredential(credential.ios, it) })
4949

5050
actual suspend fun sendEmailVerification(actionCodeSettings: ActionCodeSettings?) = ios.await {
5151
actionCodeSettings?.let { settings -> sendEmailVerificationWithActionCodeSettings(settings.toIos(), it) }
5252
?: sendEmailVerificationWithCompletion(it)
53-
}.run { Unit }
53+
}
5454

5555
actual suspend fun unlink(provider: String): FirebaseUser? {
5656
val user: FIRUser? = ios.awaitResult { unlinkFromProvider(provider, it) }

firebase-database/src/iosMain/kotlin/dev/gitlive/firebase/database/database.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ actual class DatabaseException(message: String) : RuntimeException(message)
175175
private suspend fun <T, R> T.awaitResult(whileOnline: Boolean, function: T.(callback: (NSError?, R?) -> Unit) -> Unit): R {
176176
val job = CompletableDeferred<R>()
177177
function { error, result ->
178-
if(result != null) {
178+
if(error == null) {
179179
job.complete(result)
180-
} else if(error != null) {
180+
} else {
181181
job.completeExceptionally(DatabaseException(error.toString()))
182182
}
183183
}

firebase-functions/src/iosMain/kotlin/dev/gitlive/firebase/functions/functions.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ private suspend fun <T> T.await(function: T.(callback: (NSError?) -> Unit) -> Un
7070
suspend fun <T, R> T.awaitResult(function: T.(callback: (R?, NSError?) -> Unit) -> Unit): R {
7171
val job = CompletableDeferred<R>()
7272
function { result, error ->
73-
if(result != null) {
73+
if(error == null) {
7474
job.complete(result)
75-
} else if(error != null) {
75+
} else {
7676
job.completeExceptionally(FirebaseFunctionsException(error.toString()))
7777
}
7878
}

0 commit comments

Comments
 (0)