This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias Session = String | |
typealias SessionId = String | |
// Global Graph | |
interface GlobalGraph { | |
val sessionCache: SessionCache | |
} | |
// 1 - Global state | |
// globalGraph.sessionCache.getCurrentSession() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.test.runBlockingTest | |
import kotlinx.coroutines.withContext | |
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.Test | |
// function to test | |
suspend fun Int.add(valueToAdd: Int): Int = withContext(Dispatchers.Default) { | |
// pretend this is an expensive operation, so we switch off the main thread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.assertj.core.api.Assertions | |
import org.junit.Before | |
import org.junit.Test | |
// function to test | |
fun Int.add(valueToAdd: Int): Int = this + valueToAdd | |
class AddTest { | |
private lateinit var test: TestDelegate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:JvmName("RxChannelExtensions") | |
import kotlinx.coroutines.experimental.Dispatchers | |
import kotlinx.coroutines.experimental.GlobalScope | |
import kotlinx.coroutines.experimental.channels.ReceiveChannel | |
import kotlinx.coroutines.experimental.channels.consumeEach | |
import kotlinx.coroutines.experimental.channels.consumes | |
import kotlinx.coroutines.experimental.channels.consumesAll | |
import kotlinx.coroutines.experimental.channels.produce | |
import kotlinx.coroutines.experimental.launch | |
import kotlinx.coroutines.experimental.sync.Mutex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.experimental.android.Main | |
import kotlinx.coroutines.experimental.CoroutineScope | |
class ExampleActivity : Activity(), CoroutineScope by CoroutineScope(Dispatchers.Main) { | |
override fun onCreate(savedInstanceState: Bundle?) | |
super.onCreate(savedInstanceState) | |
val ui = Ui(this) // bind views, etc | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
Thread { | |
var counter = 0 | |
counter++ | |
counter++ | |
println("$counter") // will always print "2" | |
} | |
// 2 | |
// for every Request, a Response is returned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.app.Application | |
import android.content.Context | |
import android.os.Bundle | |
// A Graph is just an interface that exposes your dependencies (like a Dagger 2 @Component). | |
interface Graph { | |
val context: Context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import io.reactivex.Observable | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.rxkotlin.addTo | |
import io.reactivex.rxkotlin.subscribeBy | |
/** | |
* Stores state related to login. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.arch.lifecycle.LiveData | |
import android.arch.lifecycle.MutableLiveData | |
import android.support.annotation.MainThread | |
import kotlin.reflect.KClass | |
/** Only emits [T] when [source] is true ]*/ | |
fun <T : Any> LiveData<T>.filterTrue(source: LiveData<Boolean>): LiveData<T> { | |
return this.combineLatest(source) { emission, isStateInitialized -> | |
Pair(emission, isStateInitialized) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun <A : Any?, B : Any?, R> ReceiveChannel<A>.combineLatest( | |
otherSource: ReceiveChannel<B>, | |
context: CoroutineContext = Unconfined, | |
combineFunction: suspend (A, B) -> R | |
): ReceiveChannel<R> = produce(context) { | |
val sourceA: ReceiveChannel<A> = this@combineLatest | |
val sourceB: ReceiveChannel<B> = otherSource | |
val latestA = AtomicReference<A>() |