Skip to content

Commit ac445f1

Browse files
committed
Add coroutine continuation wrapper for easier use from Java
1 parent 2637d47 commit ac445f1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package {{ sdk.namespace | caseDot }}.coroutines
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlin.coroutines.Continuation
5+
import kotlin.coroutines.CoroutineContext
6+
7+
interface Callback<T> {
8+
fun onComplete(result: T?, error: Throwable?)
9+
}
10+
11+
class CoroutineCallback<T> @JvmOverloads constructor(
12+
private val callback: Callback<T>,
13+
override val context: CoroutineContext = Dispatchers.Default
14+
) : Continuation<T> {
15+
override fun resumeWith(result: Result<T>) {
16+
callback.onComplete(result.getOrNull(), result.exceptionOrNull())
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package {{ sdk.namespace | caseDot }}.coroutines
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlin.coroutines.Continuation
5+
import kotlin.coroutines.CoroutineContext
6+
7+
interface Callback<T> {
8+
fun onComplete(result: T?, error: Throwable?)
9+
}
10+
11+
class CoroutineCallback<T> @JvmOverloads constructor(
12+
private val callback: Callback<T>,
13+
override val context: CoroutineContext = Dispatchers.Default
14+
) : Continuation<T> {
15+
override fun resumeWith(result: Result<T>) {
16+
callback.onComplete(result.getOrNull(), result.exceptionOrNull())
17+
}
18+
}

0 commit comments

Comments
 (0)