Skip to content

Instantly share code, notes, and snippets.

View twyatt's full-sized avatar

Travis Wyatt twyatt

View GitHub Profile
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
fun main(args: Array<String>) {
val list = listOf(3, 2, 1)
val list2 = listOf(1f, 7f, 8f)
Observable.fromIterable(list)
@twyatt
twyatt / .gitconfig
Last active February 21, 2018 17:34
[user]
name = Travis Wyatt
email = <REDACTED>
[alias]
co = checkout
br = branch
st = status
wip = !git add --verbose --all && git commit --all --verbose --message='wip'
history = log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
@twyatt
twyatt / coroutines-actor-close-thread-test.kt
Last active March 22, 2018 00:21
Testing Kotlin Coroutines when using `newSingleThreadContext` (Thread is not being cleaned up)
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 }
@twyatt
twyatt / closing-channel1-console-log.md
Last active October 5, 2018 20:37
Demonstrates wrapping a callback based class to provide interaction via Kotlin Coroutine Channels

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 / example.kt
Created December 20, 2018 23:44
Example code for "The Tale of the Misleading Coroutine Stacktrace" article
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 / solution.kt
Created December 20, 2018 23:45
Example code for "The Tale of the Misleading Coroutine Stacktrace" article
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 / allowAfter.kt
Created January 10, 2019 06:30
LiveData extension function that prevents emissions from receiver until after trigger emits a value that satisfies predicate.
/**
* 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 / GattExtensions.kt
Last active April 8, 2019 19:49
Provides a shorthand for writing to a characteristic using a service and characteristic UUID. https://github.com/JuulLabs-OSS/able
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 / output.txt
Created August 22, 2019 05:45
Kotlin Coroutine Experiment: 2 Flows, 1 Channel
pollChars
← CharData(value=a)
← CharData(value=b)
pollNumbers
← NumberData(value=-1)
← NumberData(value=0)
← NumberData(value=1)
pollChars
← CharData(value=c)
pollNumbers
@twyatt
twyatt / osx-apps.md
Last active October 5, 2020 05:13
OS X Apps
brew install httpie
brew install bat