Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional explanation commentary. #5

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/kotlin/cancel/KtCancel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class KtCancel (
val job = launch { longRunningFunction() }

launch {
delay(5000) // wait 5 seconds
job.cancel()
delay(5000) // wait 5 seconds,
job.cancel() // then cancel the running coroutine
}
}

suspend fun longRunningFunction() {
while (isActive) {
while (isActive) { // check that the coroutine has not been canceled
println("Long running job still active in KtCancel")
delay(timeDelay)
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/cancel/RxCancel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ private const val timeDelay = 1000L
class RxCancel (
scheduler: Scheduler = Schedulers.computation()
) {
val stream = Flowable.interval(timeDelay, TimeUnit.MILLISECONDS)
val stream = Flowable.interval(timeDelay, TimeUnit.MILLISECONDS) // Once every second...
.observeOn(scheduler)
.map {
"Long running job still active in RxCancel"
"Long running job still active in RxCancel" // emit this string
}

init {
val disposable = stream.subscribe({
println(it)
println(it) // log every emission to the console
})

Completable.fromAction {
disposable.dispose()
disposable.dispose() // throw away our subscription to the stream, canceling it
}
.observeOn(scheduler)
.delaySubscription(5000, TimeUnit.MILLISECONDS)
.subscribe()
.observeOn(scheduler)
.delaySubscription(5000, TimeUnit.MILLISECONDS) // Wait 5 seconds, then subscribe (for demonstration purposes)
.subscribe()
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/completable/KtCompletable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KtCompletable(
): CoroutineScope {

init {
launch {
launch { // Since the encapsulating class extends CoroutineScope, we can simply use the launch coroutine builder to fire the work as a child of the current scope
doSomeWork()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/filter/KtFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class KtFilter (

init {
launch {
producer.filter { it % 2 == 0 }
.consumeEach {
producer.filter { it % 2 == 0 } // the .filter operator here will only send even numbers downstream
.consumeEach { // consumeEach subscribes to the producer Channel
println("Value from KtFilter: $it")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/merge/KtMerge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class KtMerge(

init {
launch {
flowOf(flowOne, flowTwo).flattenMerge().collect {
flowOf(flowOne, flowTwo).flattenMerge().collect { // without the flattenMerge() we'd have a flow of flows rather than a flow of Ints
println("Value from KtMerge $it")
}
}
Expand Down