Skip to content

Instantly share code, notes, and snippets.

Avatar

Robin roschlau

View GitHub Profile
@roschlau
roschlau / Repeater.kt
Last active Mar 16, 2017
A simple repeater that will repeat an action infinitely with a set interval until it is stopped. Uses Coroutines.
View Repeater.kt
class Repeater(val interval: Long,
val context: CoroutineContext = UI,
val block: () -> Unit
) {
private var updatingJob: Job? = null
private val updatingJobLock = Any()
fun start() = synchronized(updatingJobLock) {
if (updatingJob != null && updatingJob!!.isActive) {
return
@roschlau
roschlau / MutableLazy.kt
Last active Nov 26, 2017
Lazy Delegate implementation for a mutable, but lazily initialized variable
View MutableLazy.kt
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Property delegate to allow lazy initialization of a mutable variable.
*/
class MutableLazy<T>(val init: () -> T) : ReadWriteProperty<Any?, T> {
private var value: Optional<T> = Optional.None()
@roschlau
roschlau / event.kt
Last active Jul 7, 2017 — forked from orangy/event.kt
C#-style events in Kotlin
View event.kt
class Event<T> {
private val handlers = arrayListOf<(T) -> Unit>()
operator fun plusAssign(handler: (T) -> Unit) { handlers.add(handler) }
operator fun invoke(value: T) { for (handler in handlers) handler(value) }
}
val e = Event<String>() // define event
fun main(args : Array<String>) {
e += { println(it) } // subscribe