Skip to content
Permalink
Browse files

0.8.8 - add notifyUpdate and defaultDispatcher property. Arrow/SafeRe…

…sult split
  • Loading branch information
arnaudgiuliani committed Oct 30, 2019
1 parent ecedefc commit 5152c197131c7f9d8b3129a37017671c4aea49ae
Showing with 316 additions and 139 deletions.
  1. +1 −1 README.md
  2. +1 −1 gradle/versions.gradle
  3. +17 −8 uniflow-android/src/main/java/io/uniflow/android/flow/AndroidDataFlow.kt
  4. +18 −4 uniflow-androidx/src/main/java/io/uniflow/androidx/flow/AndroidDataFlow.kt
  5. +1 −1 uniflow-arrow/build.gradle
  6. +0 −49 uniflow-arrow/src/main/kotlin/io/uniflow/core/result/SafeTypeBuilders.kt
  7. +1 −1 uniflow-arrow/src/main/kotlin/io/uniflow/{core → }/result/TryExt.kt
  8. +3 −6 uniflow-arrow/src/test/kotlin/io/uniflow/arrow/ErrorResultTest.kt
  9. +4 −4 uniflow-arrow/src/test/kotlin/io/uniflow/arrow/StateFlowTest.kt
  10. +4 −4 uniflow-arrow/src/test/kotlin/io/uniflow/arrow/SuccessResultTest.kt
  11. +1 −1 ...src/main/kotlin/io/uniflow/result → uniflow-core/src/main/kotlin/io/uniflow/core/error}/Errors.kt
  12. +18 −18 uniflow-core/src/main/kotlin/io/uniflow/core/flow/DataFlow.kt
  13. +1 −4 uniflow-core/src/main/kotlin/io/uniflow/core/threading/Threading.kt
  14. +0 −18 uniflow-saferesult/src/main/kotlin/io/uniflow/result/Result.kt
  15. +23 −5 uniflow-saferesult/src/main/kotlin/io/uniflow/result/SafeResult.kt
  16. +0 −6 uniflow-saferesult/src/main/kotlin/io/uniflow/result/SafeResultExt.kt
  17. +96 −0 uniflow-saferesult/src/test/kotlin/io/uniflow/result/ErrorResultTest.kt
  18. +23 −0 uniflow-saferesult/src/test/kotlin/io/uniflow/result/StateFlowTest.kt
  19. +70 −0 uniflow-saferesult/src/test/kotlin/io/uniflow/result/SuccessResultTest.kt
  20. +12 −4 uniflow-test/src/test/kotlin/io/uniflow/test/ActorFlowTest.kt
  21. +14 −4 uniflow-test/src/test/kotlin/io/uniflow/test/data/ListDataFlow.kt
  22. +2 −0 uniflow-test/src/test/kotlin/io/uniflow/test/data/TodoListState.kt
  23. +6 −0 uniflow-test/src/test/kotlin/io/uniflow/test/data/TodoStackActorFlow.kt
@@ -3,7 +3,7 @@

## Setup

#### Current version is `0.8.7`
#### Current version is `0.8.8`

Choose one of the following dependency:

@@ -1,6 +1,6 @@
ext {
// Koin
uniflow_version = '0.8.7'
uniflow_version = '0.8.8'

// Kotlin
kotlin_version = '1.3.50'
@@ -21,15 +21,11 @@ import android.arch.lifecycle.ViewModel
import io.uniflow.core.dispatcher.UniFlowDispatcher
import io.uniflow.core.flow.*
import io.uniflow.core.logger.UniFlowLogger
import io.uniflow.core.threading.onIO
import io.uniflow.core.threading.onMain
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.isActive

abstract class AndroidDataFlow(defaultCapacity: Int = 10) : ViewModel(), DataFlow {
abstract class AndroidDataFlow(defaultCapacity: Int = 10, override val defaultDispatcher: CoroutineDispatcher = UniFlowDispatcher.dispatcher.io()) : ViewModel(), DataFlow {

private val viewModelJob = SupervisorJob()
override val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Main + viewModelJob)
@@ -50,21 +46,34 @@ abstract class AndroidDataFlow(defaultCapacity: Int = 10) : ViewModel(), DataFlo
return null
}

override suspend fun notifyUpdate(newState: UIState, notificationEvent: UIEvent): UIState? {
onMain(immediate = true) {
UniFlowLogger.logState(newState)
_internalState = newState
UniFlowLogger.logEvent(notificationEvent)
_events.value = Event(notificationEvent)
}
return null
}

override suspend fun applyState(state: UIState) {
onMain(immediate = true) {
UniFlowLogger.logState(state)
_internalState = state
_states.value = state
}
}

private var _internalState: UIState? = null

override val currentState: UIState?
get() = _states.value
get() = _internalState

override val actorFlow = coroutineScope.actor<StateAction>(UniFlowDispatcher.dispatcher.default(), capacity = defaultCapacity) {
for (action in channel) {
if (coroutineScope.isActive) {
UniFlowLogger.log("AndroidActorFlow action $action")
onIO {
withContext(defaultDispatcher) {
proceedAction(action)
}
} else {
@@ -22,13 +22,14 @@ import androidx.lifecycle.viewModelScope
import io.uniflow.core.dispatcher.UniFlowDispatcher
import io.uniflow.core.flow.*
import io.uniflow.core.logger.UniFlowLogger
import io.uniflow.core.threading.onIO
import io.uniflow.core.threading.onMain
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext

abstract class AndroidDataFlow(defaultCapacity: Int = 10) : ViewModel(), DataFlow {
abstract class AndroidDataFlow(defaultCapacity: Int = 10, override val defaultDispatcher: CoroutineDispatcher = UniFlowDispatcher.dispatcher.io()) : ViewModel(), DataFlow {

override val coroutineScope: CoroutineScope = viewModelScope

@@ -51,18 +52,31 @@ abstract class AndroidDataFlow(defaultCapacity: Int = 10) : ViewModel(), DataFlo
override suspend fun applyState(state: UIState) {
onMain(immediate = true) {
UniFlowLogger.logState(state)
_internalState = state
_states.value = state
}
}

override suspend fun notifyUpdate(newState: UIState, notificationEvent: UIEvent): UIState? {
onMain(immediate = true) {
UniFlowLogger.logState(newState)
_internalState = newState
UniFlowLogger.logEvent(notificationEvent)
_events.value = Event(notificationEvent)
}
return null
}

private var _internalState: UIState? = null

override val currentState: UIState?
get() = _states.value
get() = _internalState

override val actorFlow = coroutineScope.actor<StateAction>(UniFlowDispatcher.dispatcher.default(), capacity = defaultCapacity) {
for (action in channel) {
if (coroutineScope.isActive) {
UniFlowLogger.log("AndroidActorFlow run $action")
onIO {
withContext(defaultDispatcher) {
proceedAction(action)
}
} else {
@@ -4,7 +4,7 @@ archivesBaseName = 'uniflow-arrow'
description = 'UniFlow a Kotlin unidrectional data flow framework'

dependencies {
api project(":uniflow-saferesult")
api project(":uniflow-core")
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"

This file was deleted.

@@ -1,4 +1,4 @@
package io.uniflow.core.result
package io.uniflow.result

import arrow.core.Success
import arrow.core.Try
@@ -1,11 +1,8 @@
package io.uniflow.arrow

import arrow.core.Failure
import arrow.core.failure
import arrow.core.orElse
import arrow.core.success
import arrow.core.*
import io.uniflow.core.flow.UIState
import io.uniflow.core.result.*
import io.uniflow.result.*
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -35,7 +32,7 @@ class ErrorResultTest {
fun `orElse result`() = runBlocking {
val sndValue = " #2"
val result =
safeCall<String> { throw error }
Try<String> { throw error }
.orElse { sndValue.success() }
assertTrue(result.get() == sndValue)
}
@@ -1,9 +1,9 @@
package io.uniflow.arrow

import arrow.core.Try
import io.uniflow.core.flow.UIState
import io.uniflow.core.result.onFailure
import io.uniflow.core.result.safeValue
import io.uniflow.core.result.toState
import io.uniflow.result.onFailure
import io.uniflow.result.toState
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -13,7 +13,7 @@ class StateFlowTest {
@Test
fun `test flow`() = runBlocking {

val value = safeValue { throw RuntimeException("boom") }
val value = Try { throw RuntimeException("boom") }
.onFailure { System.err.println("error -> $it") }
.toState(
{ UIState.Success },
@@ -2,10 +2,10 @@ package io.uniflow.arrow

import arrow.core.success
import io.uniflow.core.flow.UIState
import io.uniflow.core.result.get
import io.uniflow.core.result.onFailure
import io.uniflow.core.result.onSuccess
import io.uniflow.core.result.toStateOrNull
import io.uniflow.result.get
import io.uniflow.result.onFailure
import io.uniflow.result.onSuccess
import io.uniflow.result.toStateOrNull
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -1,4 +1,4 @@
package io.uniflow.result
package io.uniflow.core.error

import io.uniflow.core.flow.UIEvent
import io.uniflow.core.flow.UIState
@@ -16,10 +16,11 @@
package io.uniflow.core.flow

import io.uniflow.core.logger.UniFlowLogger
import io.uniflow.core.threading.launchOnIO
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

/**
* Unidirectional Data Flow
@@ -33,22 +34,33 @@ interface DataFlow {
*/
val coroutineScope: CoroutineScope

/**
* Default Coroutine dispatcher
*/
val defaultDispatcher: CoroutineDispatcher

/**
* Actor to buffer incoming actions
*/
val actorFlow: SendChannel<StateAction>

/**
* Return current State if any
* @return state
*/
val currentState: UIState?

/**
* Send an event
* @param event
* @param nothing
*/
suspend fun sendEvent(event: UIEvent): UIState?

/**
* Return current State if any
* @return state
*
*/
val currentState: UIState?
suspend fun notifyUpdate(newState: UIState, notificationEvent: UIEvent): UIState?

/**
* Apply new state to current state
@@ -111,7 +123,7 @@ interface DataFlow {
fun onStateFlow(stateFlowAction: StateFlowAction) {
coroutineScope.apply {
if (isActive) {
launchOnIO {
launch(defaultDispatcher) {
proceedStateFlow(stateFlowAction)
}
}
@@ -130,18 +142,6 @@ interface DataFlow {
}
}

// fun onAction(action: StateAction) {
// coroutineScope.apply {
// if (isActive) {
// UniFlowLogger.log("DataFlow.onAction run $action")
// launchOnIO {
// proceedAction(action)
// }
// } else {
// UniFlowLogger.log("DataFlow.onAction action cancelled")
// }
// }
// }
/**
* Execute the action & catch any flowError
* @param action
@@ -180,7 +180,7 @@ interface DataFlow {
coroutineScope.apply {
if (isActive) {
UniFlowLogger.log("DataFlow onActionError '${error.message}' for action $action ")
launchOnIO {
launch(defaultDispatcher) {
if (action.errorFunction != null) {
val failState = action.errorFunction.let {
it.invoke(action, error)
@@ -1,10 +1,7 @@
package io.uniflow.core.threading

import io.uniflow.core.dispatcher.UniFlowDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*


/**

This file was deleted.

0 comments on commit 5152c19

Please sign in to comment.
You can’t perform that action at this time.