Skip to content

Instantly share code, notes, and snippets.

View omkar-tenkale's full-sized avatar
☸️
Planning

Omkar Tenkale omkar-tenkale

☸️
Planning
View GitHub Profile
fun launch(block: suspend () -> Unit) {
val callback = object : Continuation<Unit> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<Unit>) {}
}
block.createCoroutineUnintercepted(callback).resumeWith(Result.success(Unit))
}
...
import kotlin.concurrent.thread
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
fun main() {
// Define a suspend lambda
import java.lang.Exception
import kotlin.concurrent.thread
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
fun main() {
// Compiler generates a new class that contains the suspendingLambda
class Continuation1(val previousContinuation: Continuation<Double>) : Continuation<Unit> {
suspend fun calculateSquareRoot(number: Double): Double {
val block: (Continuation<Double>)->Any? = {
// Wait for result in current thread
kotlin.math.sqrt(number)
}
return suspendCoroutineUninterceptedOrReturn(block)
}
public interface Continuation<in T> {
// A map like data structure to hold data carried by the continuation
public val context: CoroutineContext
// Called to complete the unfinished execution
public fun resumeWith(result: Result<T>)
}
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted
fun launch(block: suspend () -> Unit) {
val callback = object : Continuation<Unit> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<Unit>) {}
}
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
fun main() {
// Compiler generates a new class that contains the suspendingLambda
class Continuation1(val previousContinuation: Continuation<String>) : Continuation<Unit> {
override val context = previousContinuation.context
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted
fun main() {
// Define a suspend lambda that returns "Hello World!"
val suspendingLambda: suspend () -> String = suspend {
"Hello World!"
}
@omkar-tenkale
omkar-tenkale / ControlledExplosion.kt
Last active December 28, 2024 05:10
Compose Explosion Animation Snippets
@Composable
fun ControlledExplosion() {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
var progress by remember { mutableStateOf(0f) }
Explosion(progress)

This is a draft, a note to myself to understand dagger

Understanding Dagger2

It all starts with a messy code, or its early realization if your're well prepared As projects starts to become large, they will have lots of classes referencing lots of dependencies

Retrofit instance, logger instance, DB instance, SharedPreferenceManager instance Generally we declare them singleton and move on, accessing them directly This is fine in the beginning but it hampers testability in long term as those singletons are hard to mock