Skip to content

Instantly share code, notes, and snippets.

@twyatt
twyatt / ChannelSendExceptions.kt
Created Apr 15, 2021
Exceptions thrown when sending to closed/cancelled Channel
View ChannelSendExceptions.kt
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
suspend fun main() = coroutineScope<Unit> {
val receive = Channel<Int>()
receive.cancel() // ReceiveChannel
// java.util.concurrent.CancellationException: RendezvousChannel was cancelled
runCatching { receive.send(1) }.onFailure { println(it) }
val send = Channel<Int>()
@twyatt
twyatt / async-await.kt
Last active Dec 30, 2020
Experiment to confirm that multiple Coroutines will all suspend when calling `Deferred.await` (and all get the resulting failure)
View async-await.kt
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
suspend fun <T> Deferred<T>.awaitCatching(launchNumber: Int, startTime: Long) = try {
await()
} catch (t: Throwable) {
@twyatt
twyatt / comparison.md
Last active Dec 1, 2020
try-finally vs. invokeOnCompletion
View comparison.md

The invokeOnCompletion will always be invoked, but it has a number of different characteristics vs. its try counterpart.

try-catch-finally invokeOnCompletion
  • suspend functionality
  • Predictable threading/context
  • Familiar semantics
  • Adheres to Coroutine cancellation
  • Blocking execution on arbitrary thread
  • No guarantees around execution timing
  • Implementation must be fast, non-blocking, and thread-safe
  • Exceptions are wrapped with CompletionHandlerException1
GlobalScope.launch(Dispatchers.IO) {
    try {
        // todo
View osx-apps.md

HTTPie

brew install httpie

bat

brew install bat
@twyatt
twyatt / cheatsheet.md
Last active Oct 19, 2020
CBOR cheatsheet
View cheatsheet.md

CBOR cheatsheet

Major type Description Binary Shorthand
0 an unsigned integer 000_xxxxx unsigned(#)
1 a negative integer 001_xxxxx negative(#-1)
2 a byte string 010_xxxxx bytes(n)
3 a text string 011_xxxxx text(n)
4 an array of data items 100_xxxxx array(n)
5 a map of pairs of data items 101_xxxxx map(n)
@twyatt
twyatt / output.txt
Created Aug 22, 2019
Kotlin Coroutine Experiment: 2 Flows, 1 Channel
View output.txt
pollChars
← CharData(value=a)
← CharData(value=b)
pollNumbers
← NumberData(value=-1)
← NumberData(value=0)
← NumberData(value=1)
pollChars
← CharData(value=c)
pollNumbers
@twyatt
twyatt / GattExtensions.kt
Last active Apr 8, 2019
Provides a shorthand for writing to a characteristic using a service and characteristic UUID. https://github.com/JuulLabs-OSS/able
View GattExtensions.kt
package com.example.myapplication
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattService
import com.juul.able.experimental.Gatt
import com.juul.able.experimental.WriteType
import com.juul.able.experimental.throwable.writeCharacteristicOrThrow
import java.util.UUID
class GattServiceNotFound(uuid: UUID) : Exception("GATT service $uuid not found.")
@twyatt
twyatt / allowAfter.kt
Created Jan 10, 2019
LiveData extension function that prevents emissions from receiver until after trigger emits a value that satisfies predicate.
View allowAfter.kt
/**
* Prevent emissions from receiver until after [trigger] emits a value that satisfies [predicate].
*/
private fun <T, R> LiveData<T>.allowAfter(
trigger: LiveData<R>,
predicate: (R?) -> Boolean
) = object : MediatorLiveData<T?>() {
private var isReady = false
private var hasValue = false
@twyatt
twyatt / solution.kt
Created Dec 20, 2018
Example code for "The Tale of the Misleading Coroutine Stacktrace" article
View solution.kt
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class SourceClosed(cause: Throwable) : IllegalStateException(cause)
fun main(args: Array<String>) = runBlocking<Unit> {
@twyatt
twyatt / example.kt
Created Dec 20, 2018
Example code for "The Tale of the Misleading Coroutine Stacktrace" article
View example.kt
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
fun main(args: Array<String>) = runBlocking<Unit> {
val channel = Channel<Int>(CONFLATED)
launch {