Skip to content

Instantly share code, notes, and snippets.

@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 {
@twyatt
twyatt / closing-channel1-console-log.md
Last active Oct 5, 2018
Demonstrates wrapping a callback based class to provide interaction via Kotlin Coroutine Channels
View closing-channel1-console-log.md

Demonstrates closing channel 1. Channel 2 continues to be consumable (wrapper is not closed).

(cancelMode = CancelMode.CHANNEL)

main → threads=[Monitor Ctrl-Break, Signal Dispatcher, Finalizer, main @coroutine#1, Reference Handler]
Wrapper[1] → createChannel → <start>
Wrapper[1] → client.observe
Client → observe
Client → observe → thread → handler.onResponse(0)
Wrapper[1] → onResponse → <start>
@twyatt
twyatt / coroutines-actor-close-thread-test.kt
Last active Mar 22, 2018
Testing Kotlin Coroutines when using `newSingleThreadContext` (Thread is not being cleaned up)
View coroutines-actor-close-thread-test.kt
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.channels.consumeEach
import kotlinx.coroutines.experimental.newSingleThreadContext
import kotlinx.coroutines.experimental.runBlocking
val threads: Set<Thread>
get() = Thread.getAllStackTraces().keys
val threadNames: List<String>
get() = threads.map { it.name }
You can’t perform that action at this time.