Skip to content

Commit

Permalink
Merge pull request #96 from thefuntasty/feature/2-x-improve-flow
Browse files Browse the repository at this point in the history
Improve FlowInteractor
  • Loading branch information
skywall authored Jan 7, 2020
2 parents be7caec + cc606a7 commit 7ef8e26
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import kotlinx.coroutines.flow.Flow
abstract class BaseFlowInteractor<T> {

/**
* [Job] used to hold and cancel existing run of this interactor
* [Job] used to hold and cancel existing run of this interactor
*/
var job: Job? = null

/**
* Suspend function which should contain business logic
* Function which should contain business logic
*/
abstract suspend fun build(): Flow<T>
abstract fun build(): Flow<T>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

/**
Expand Down Expand Up @@ -73,22 +73,21 @@ interface CoroutineScopeOwner {
onComplete: () -> Unit = {}
) {
job?.cancel()
job = coroutineScope.launch(Dispatchers.Main) {
try {
build()
.flowOn(getWorkerDispatcher())
.onEach { onNext(it) }
.onCompletion { error ->
if (this@launch.isActive) {
error?.also { onError?.invoke(it) ?: throw error } ?: onComplete()
}
job = build()
.flowOn(getWorkerDispatcher())
.onEach { onNext(it) }
.onCompletion { error ->
when {
error is CancellationException -> {
// ignore this exception
}
.collect()
} catch (cancellation: CancellationException) {
// do nothing this is normal way of suspend function interruption
} catch (error: Throwable) {
onError?.invoke(error) ?: throw error
error != null -> {
onError?.invoke(error) ?: throw error
}
else -> onComplete()
}
}
}
.catch { /* handled in onCompletion */ }
.launchIn(coroutineScope)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.thefuntasty.mvvm.crinteractors

import com.thefuntasty.mvvm.crinteractors.testinteractors.base.BaseCoroutineScopeOwnerTest
import com.thefuntasty.mvvm.crinteractors.testinteractors.testinteractors.TestFailureInteractor
import com.thefuntasty.mvvm.crinteractors.testinteractors.testinteractors.TestFlowFailureInteractor
import com.thefuntasty.mvvm.crinteractors.testinteractors.testinteractors.TestFlowInteractor
import com.thefuntasty.mvvm.crinteractors.testinteractors.testinteractors.TestInteractor
import com.thefuntasty.mvvm.crinteractors.testinteractors.base.BaseCoroutineScopeOwnerTest
import org.junit.Assert
import org.junit.Test

Expand All @@ -14,13 +14,13 @@ class CoroutineScopeOwnerTest : BaseCoroutineScopeOwnerTest() {
fun previousExecutionCanceled() {
val testInteractor = TestInteractor().apply { init(1) }
var count = 0
testInteractor.execute( {
testInteractor.execute({
count++
}, {
Assert.fail("Exception thrown where shouldn't")
})
coroutineScope.advanceTimeBy(500)
testInteractor.execute( {
testInteractor.execute({
count++
}, {
Assert.fail("Exception thrown where shouldn't")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestFlowFailureInteractor : BaseFlowInteractor<Unit>() {
this.error = errorToThrow
}

override suspend fun build(): Flow<Unit> = flow {
override fun build(): Flow<Unit> = flow {
throw error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class TestFlowInteractor : BaseFlowInteractor<Int>() {
this.delayBetweenEmits = delayBetweenEmits
}

override suspend fun build(): Flow<Int> = listToEmit.asFlow().onEach { delay(delayBetweenEmits) }
override fun build(): Flow<Int> = listToEmit.asFlow().onEach { delay(delayBetweenEmits) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import javax.inject.Inject
class GetFormFlowInteractor @Inject constructor(
private val formStore: FormStore
) : BaseFlowInteractor<Pair<String, String>>() {
override suspend fun build(): Flow<Pair<String, String>> = formStore.getFormFlow()
override fun build(): Flow<Pair<String, String>> = formStore.getFormFlow()
}
1 change: 1 addition & 0 deletions mvvm-lint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build

0 comments on commit 7ef8e26

Please sign in to comment.