-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample of backpressure-enabled stream conversion
- Loading branch information
Elijah Verdoorn
committed
Sep 18, 2019
1 parent
c5d6dfc
commit 82a0c3a
Showing
3 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package interop.flowable | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.rx2.rxFlowable | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class FlowableInterop( | ||
override val coroutineContext: CoroutineContext = Dispatchers.Default | ||
): CoroutineScope { | ||
fun getFlowable() = CoroutineScope(coroutineContext).rxFlowable { | ||
(1..10).map { | ||
send(it) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package interop.flowable | ||
|
||
fun main() { | ||
println("Creating FlowableInterop") | ||
val flowableInterop = FlowableInterop() | ||
flowableInterop.getFlowable() | ||
.subscribe({ | ||
println(it) | ||
Thread.sleep(1000) // notice that the producing coroutine's backpressure is handled | ||
},::println) | ||
|
||
Thread.sleep(11000) // allow background work to occur before main thread exits | ||
} |
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