Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Applied PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolaj.leszczynski committed Nov 12, 2019
1 parent 7d09600 commit 0867304
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 107 deletions.
12 changes: 6 additions & 6 deletions docs/orbits.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ and enable reuse.

``` kotlin
.reduce {
state.copy(currentState.total + event.number)
state.copy(getCurrentState().total + event.number)
}
```

Expand All @@ -72,7 +72,7 @@ transformations beforehand:
``` kotlin
perform("addition")
.on<AddAction>()
.reduce { state.copy(currentState.total + event.number) }
.reduce { state.copy(getCurrentState().total + event.number) }
```

The reducers are passthrough transformers. This means that after applying
Expand All @@ -88,7 +88,7 @@ perform("add random number")

perform("reduce add random number")
.on<GetRandomNumberUseCaseStatus>()
.reduce { state.copy(currentState.total + event.number) }
.reduce { state.copy(getCurrentState().total + event.number) }
```

Loopbacks allow you to create feedback loops where events coming from one orbit
Expand Down Expand Up @@ -124,7 +124,7 @@ OrbitViewModel<State, SideEffect>(State(), {
perform("side effect straight on the incoming action")
.on<SomeAction>()
.sideEffect {
Timber.log(currentState)
Timber.log(getCurrentState())
Timber.log(event)
}

Expand All @@ -141,7 +141,7 @@ OrbitViewModel<State, SideEffect>(State(), {

perform("post side effect straight on the incoming action")
.on<NthAction>()
.sideEffect { post(SideEffect.Toast(currentState.toString())) }
.sideEffect { post(SideEffect.Toast(getCurrentState().toString())) }
.sideEffect { post(SideEffect.Toast(event.toString())) }
.sideEffect { post(SideEffect.Navigate(Screen.Home)) }
})
Expand All @@ -165,7 +165,7 @@ For example:
``` kotlin
perform("Toast the current state")
.on<SomeAction>()
.sideEffect { post(SideEffect.Toast(currentState.toString())) }
.sideEffect { post(SideEffect.Toast(getCurrentState().toString())) }
```

This property always captures the current state, and so calling this
Expand Down
20 changes: 20 additions & 0 deletions orbit/src/main/java/com/babylon/orbit/ActionFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2019 Babylon Partners Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.babylon.orbit

@OrbitDsl
class ActionFilter(val description: String)
22 changes: 22 additions & 0 deletions orbit/src/main/java/com/babylon/orbit/ConfigReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2019 Babylon Partners Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.babylon.orbit

@OrbitDsl
class ConfigReceiver(
var sideEffectCachingEnabled: Boolean = true
)
35 changes: 35 additions & 0 deletions orbit/src/main/java/com/babylon/orbit/EventReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019 Babylon Partners Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.babylon.orbit

/**
* @property event The incoming event.
*/
@OrbitDsl
class EventReceiver<STATE : Any, EVENT : Any>(
private val stateProvider: () -> STATE,
val event: EVENT
) {
/**
* Returns the current state captured whenever this method is called. Successive calls to this
* method may yield different results each time as the state could be modified by another flow at
* any time.
*
* Within a reducer however, you can expect this to be constant.
*/
fun getCurrentState() = stateProvider()
}
14 changes: 0 additions & 14 deletions orbit/src/main/java/com/babylon/orbit/Middleware.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@

package com.babylon.orbit

import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject

typealias TransformerFunction<STATE, SIDE_EFFECT> = OrbitContext<STATE, SIDE_EFFECT>.() -> (Observable<*>)

data class OrbitContext<STATE : Any, SIDE_EFFECT : Any>(
val currentStateProvider: () -> STATE,
val rawActions: Observable<*>,
val inputSubject: PublishSubject<Any>,
val reducerSubject: PublishSubject<(STATE) -> STATE>,
val sideEffectSubject: PublishSubject<SIDE_EFFECT>,
val ioScheduled: Boolean
)

interface Middleware<STATE : Any, SIDE_EFFECT : Any> {
val initialState: STATE
val orbits: List<TransformerFunction<STATE, SIDE_EFFECT>>
Expand Down
31 changes: 31 additions & 0 deletions orbit/src/main/java/com/babylon/orbit/OrbitContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Babylon Partners Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.babylon.orbit

import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject

typealias TransformerFunction<STATE, SIDE_EFFECT> = OrbitContext<STATE, SIDE_EFFECT>.() -> (Observable<*>)

data class OrbitContext<STATE : Any, SIDE_EFFECT : Any>(
val currentStateProvider: () -> STATE,
val rawActions: Observable<*>,
val inputSubject: PublishSubject<Any>,
val reducerSubject: PublishSubject<(STATE) -> STATE>,
val sideEffectSubject: PublishSubject<SIDE_EFFECT>,
val ioScheduled: Boolean
)
20 changes: 20 additions & 0 deletions orbit/src/main/java/com/babylon/orbit/OrbitDsl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2019 Babylon Partners Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.babylon.orbit

@DslMarker
annotation class OrbitDsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,6 @@ package com.babylon.orbit
import io.reactivex.Observable
import io.reactivex.rxkotlin.ofType
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.Subject

/*
What do we want to log:
- which flow was triggered
- by what action & state combination
- any outcome (new state emitted, something looped back etc.)
*/

@DslMarker
annotation class OrbitDsl

/**
* Convenience method for creating a [Middleware].
*
* @param initialState The initial state to set on your MVI system.
* @param init The DSL implementation describing your MVI flows.
*/
fun <STATE : Any, SIDE_EFFECT : Any> middleware(
initialState: STATE,
init: OrbitsBuilder<STATE, SIDE_EFFECT>.() -> Unit
): Middleware<STATE, SIDE_EFFECT> {

return OrbitsBuilder<STATE, SIDE_EFFECT>(initialState).apply {
init(this)
}.build()
}

@OrbitDsl
class ActionFilter(val description: String)

@OrbitDsl
class ConfigReceiver(
var sideEffectCachingEnabled: Boolean = true
)

@OrbitDsl
open class OrbitsBuilder<STATE : Any, SIDE_EFFECT : Any>(private val initialState: STATE) {
Expand All @@ -76,7 +41,9 @@ open class OrbitsBuilder<STATE : Any, SIDE_EFFECT : Any>(private val initialStat
* @param description The description for your flow. This needs to be unique within the same middleware.
*/
@Suppress("unused") // Used for the nice extension function highlight
fun OrbitsBuilder<STATE, SIDE_EFFECT>.perform(description: String) = ActionFilter(description)
fun OrbitsBuilder<STATE, SIDE_EFFECT>.perform(description: String) = ActionFilter(
description
)
.also {
require(!descriptions.contains(description)) {
"Names used in perform must be unique! $description already exists!"
Expand Down Expand Up @@ -238,57 +205,17 @@ open class OrbitsBuilder<STATE : Any, SIDE_EFFECT : Any>(private val initialStat
}

/**
* @property eventObservable The original observable to be transformed.
*/
@OrbitDsl
class TransformerReceiver<STATE : Any, EVENT : Any>(
private val stateProvider: () -> STATE,
val eventObservable: Observable<EVENT>
) {
/**
* Returns the current state captured whenever this method is called. Successive calls to this
* method may yield different results each time as the state could be modified by another flow at
* any time.
*/
fun getCurrentState() = stateProvider()
}

/**
* @property event The incoming event.
*/
@OrbitDsl
class EventReceiver<STATE : Any, EVENT : Any>(
private val stateProvider: () -> STATE,
val event: EVENT
) {
/**
* Returns the current state captured whenever this method is called. Successive calls to this
* method may yield different results each time as the state could be modified by another flow at
* any time.
*
* Within a reducer however, you can expect this to be constant.
*/
fun getCurrentState() = stateProvider()
}

/**
* @property event The incoming event.
* Convenience method for creating a [Middleware].
*
* @param initialState The initial state to set on your MVI system.
* @param init The DSL implementation describing your MVI flows.
*/
@OrbitDsl
class SideEffectEventReceiver<STATE : Any, EVENT : Any, SIDE_EFFECT : Any>(
private val stateProvider: () -> STATE,
private val sideEffectRelay: Subject<SIDE_EFFECT>,
val event: EVENT
) {
/**
* Returns the current state captured whenever this method is called. Successive calls to this
* method may yield different results each time as the state could be modified by another flow at
* any time.
*/
fun getCurrentState() = stateProvider()
fun <STATE : Any, SIDE_EFFECT : Any> middleware(
initialState: STATE,
init: OrbitsBuilder<STATE, SIDE_EFFECT>.() -> Unit
): Middleware<STATE, SIDE_EFFECT> {

/**
* Allows you to post a side effect to the side effect relay.
*/
fun post(sideEffect: SIDE_EFFECT) = sideEffectRelay.onNext(sideEffect)
return OrbitsBuilder<STATE, SIDE_EFFECT>(initialState).apply {
init(this)
}.build()
}
41 changes: 41 additions & 0 deletions orbit/src/main/java/com/babylon/orbit/SideEffectEventReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019 Babylon Partners Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.babylon.orbit

import io.reactivex.subjects.Subject

/**
* @property event The incoming event.
*/
@OrbitDsl
class SideEffectEventReceiver<STATE : Any, EVENT : Any, SIDE_EFFECT : Any>(
private val stateProvider: () -> STATE,
private val sideEffectRelay: Subject<SIDE_EFFECT>,
val event: EVENT
) {
/**
* Returns the current state captured whenever this method is called. Successive calls to this
* method may yield different results each time as the state could be modified by another flow at
* any time.
*/
fun getCurrentState() = stateProvider()

/**
* Allows you to post a side effect to the side effect relay.
*/
fun post(sideEffect: SIDE_EFFECT) = sideEffectRelay.onNext(sideEffect)
}
Loading

0 comments on commit 0867304

Please sign in to comment.