Skip to content

Instantly share code, notes, and snippets.

View myungpyo's full-sized avatar

myungpyo,shim myungpyo

View GitHub Profile
package foo.bar
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
private val testingScope = CoroutineScope(CoroutineName("TestingScope"))
launch(CoroutineName("Coroutine:B-2") + SupervisorJob(coroutineContext[Job])) {
// ...
}
object ExceptionTester3 {
private val singleThreadedDispatcher = Executors.newSingleThreadExecutor { runnable ->
Thread(runnable).apply {
// Set uncaught exception handler for the thread
// setUncaughtExceptionHandler { _, exception -> println("UncaughtExceptionHandler : $exception") }
}
}.asCoroutineDispatcher()
private val testingScope = CoroutineScope(singleThreadedDispatcher)
@JvmStatic
internal class ChildHandleNode(
@JvmField val childJob: ChildJob
) : JobCancellingNode(), ChildHandle {
override val parent: Job get() = job
override fun invoke(cause: Throwable?) = childJob.parentCancelled(job)
override fun childCancelled(cause: Throwable): Boolean = job.childCancelled(cause)
}
// Standalone Coroutine
override fun handleJobException(exception: Throwable): Boolean {
handleCoroutineException(context, exception)
return true
}
public fun handleCoroutineException(context: CoroutineContext, exception: Throwable) {
try {
context[CoroutineExceptionHandler]?.let {
it.handleException(context, exception)
private fun cancelParent(cause: Throwable): Boolean {
if (isScopedCoroutine) return true
val isCancellation = cause is CancellationException
val parent = parentHandle
if (parent === null || parent === NonDisposableHandle) {
return isCancellation
}
private fun finalizeFinishingState(state: Finishing, proposedUpdate: Any?): Any? {
// ...
if (finalException != null) {
val handled = cancelParent(finalException) || handleJobException(finalException)
if (handled) (finalState as CompletedExceptionally).makeHandled()
}
// ...
}
fun fetchPrimaryInfo() = viewModelScope.launch {
val appInfoDeferred = async { fetchAppInfoUseCase().execute() }
val userInfoDeferred = async { fetchUserInfoUseCase().execute() }
runCatching {
PrimaryInfo(
appInfo = appInfoDeferred.await(),
userInfo = userInfoDeferred.await(),
)
}.onSuccess { primaryInfo ->
package io.github.myungpyo.simplekspsample.sample
import android.os.Bundle
class MainActivityStateBinding {
fun save(stateHolder: MainActivity, stateStore: Bundle) {
with(stateHolder) {
stateStore.putString("StickyState_stringProp", stateHolder.stringProp)
stateStore.putInt("StickyState_intProp", stateHolder.intProp)
class MainActivity : AppCompatActivity() {
private val stateBinding = MainActivityStateBinding()
@StickyState
var stringProp: String = "aaa"
@StickyState
var intProp: Int = 1