Skip to content

Commit

Permalink
Add sample of rxMaybe
Browse files Browse the repository at this point in the history
  • Loading branch information
Elijah Verdoorn committed Sep 18, 2019
1 parent f441eb1 commit 2bf26f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/interop/maybe/MaybeInterop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package interop.maybe

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.rx2.rxMaybe
import kotlin.coroutines.CoroutineContext
import kotlin.random.Random

class MaybeInterop(
override val coroutineContext: CoroutineContext = Dispatchers.Default
): CoroutineScope {
fun doWorkMaybe() = CoroutineScope(coroutineContext).rxMaybe {
return@rxMaybe if (Random.Default.nextBoolean()) "A value from the function" else null
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/interop/maybe/TestMaybeInterop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package interop.maybe

fun main() {
println("Creating an instance of MaybeInterop")
val maybeInterop = MaybeInterop()
maybeInterop.doWorkMaybe().subscribe(::println, ::println)

Thread.sleep(2000) // Sleep the main thread to await the completion of the background thread
}

0 comments on commit 2bf26f6

Please sign in to comment.