Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.zhemerenko committed Mar 1, 2024
1 parent 473635a commit b241b54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.ionic.plugin.core.utils.resumeSafely
import com.ionic.plugin.core.utils.wrapAndResumeCatching
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine

Expand All @@ -21,7 +21,7 @@ fun <T> registerContinuationActivityResult(
activityResultObserver.add(object : IActivityResult {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
if (!test(requestCode, resultCode, data)) return false
it.resumeSafely { block(requestCode, resultCode, data) }
it.wrapAndResumeCatching { block(requestCode, resultCode, data) }
activityResultObserver.remove(this)
return true
}
Expand Down Expand Up @@ -57,6 +57,6 @@ fun <T> registerContinuationReceiver(
block: (intent: Intent) -> T,
) {
registerReceiver(activity, action) { intent ->
it.resumeSafely { block(intent) }
it.wrapAndResumeCatching { block(intent) }
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ extra.apply {
set("kotlinxAtomicfuVersion", "0.23.2")
set("androidxAppcompatVersion", "1.6.1")
set("mavenGroup", "com.github.SpryRocks.ionic-plugin-core")
set("mavenVersion", "0.1.15-alpha.7")
set("mavenVersion", "0.1.16-alpha.1")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.ionic.plugin.core.utils

import kotlinx.coroutines.isActive
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.*

class ContinuationCallback<T> {
private var continuation: Continuation<T>? = null
Expand All @@ -24,7 +22,7 @@ class ContinuationCallback<T> {
}
}

fun <T> Continuation<T>.resumeSafely(block: () -> T) {
fun <T> Continuation<T>.wrapAndResumeCatching(block: () -> T) {
try {
if (this.context.isActive) {
resume(block())
Expand All @@ -35,3 +33,17 @@ fun <T> Continuation<T>.resumeSafely(block: () -> T) {
}
}
}

suspend inline fun <T> suspendUncheckedCoroutine(crossinline block: (Continuation<T>) -> Unit): T {
return suspendCoroutine {
val wrapper = object : Continuation<T> {
override val context get() = it.context
override fun resumeWith(result: Result<T>) {
if (it.context.isActive) {
it.resumeWith(result)
}
}
}
block(wrapper)
}
}

0 comments on commit b241b54

Please sign in to comment.