Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
1 change: 1 addition & 0 deletions mvicore-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {

implementation deps('io.reactivex.rxjava2:rxjava')
implementation deps('io.reactivex.rxjava2:rxkotlin')
implementation deps('io.reactivex.rxjava2:rxandroid')

testImplementation deps('junit:junit')
testImplementation deps('org.jetbrains.kotlin:kotlin-test-junit')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.badoo.mvicore.android

import android.os.Looper
import com.badoo.mvicore.feature.FeatureScheduler
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers

/**
* A feature scheduler that ensures that MVICore feature only manipulates state on the Android
* main thread.
*
* It also uses the 'isOnFeatureThread' field to avoid observing on the main thread if it is already
* the current thread.
*/
object AndroidMainThreadFeatureScheduler: FeatureScheduler {
override val scheduler: Scheduler
get() = AndroidSchedulers.mainThread()

override val isOnFeatureThread: Boolean
get() = Looper.myLooper() == Looper.getMainLooper()
}
1 change: 1 addition & 0 deletions mvicore-demo/mvicore-demo-feature2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ dependencies {
implementation deps('io.reactivex.rxjava2:rxandroid')

implementation project(":mvicore")
implementation project(":mvicore-android")
implementation project(':mvicore-demo:mvicore-demo-catapi')
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.badoo.feature2.Feature2.News
import com.badoo.feature2.Feature2.State
import com.badoo.feature2.Feature2.Wish
import com.badoo.feature2.Feature2.Wish.LoadNewImage
import com.badoo.mvicore.android.AndroidMainThreadFeatureScheduler
import com.badoo.mvicore.element.Actor
import com.badoo.mvicore.element.Bootstrapper
import com.badoo.mvicore.element.NewsPublisher
Expand All @@ -19,7 +20,6 @@ import com.badoo.mvicore.element.TimeCapsule
import com.badoo.mvicore.feature.ActorReducerFeature
import io.reactivex.Observable
import io.reactivex.Observable.just
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.parcel.Parcelize

class Feature2(
Expand All @@ -29,7 +29,8 @@ class Feature2(
bootstrapper = BootStrapperImpl(),
actor = ActorImpl(),
reducer = ReducerImpl(),
newsPublisher = NewsPublisherImpl()
newsPublisher = NewsPublisherImpl(),
featureScheduler = AndroidMainThreadFeatureScheduler
) {
init {
timeCapsule?.register(Feature2::class.java) { state.copy(
Expand Down Expand Up @@ -74,7 +75,6 @@ class Feature2(
fun loadRandomImage(): Observable<Response> {
return service.getRandomImage()
.randomlyThrowAnException()
.observeOn(AndroidSchedulers.mainThread())
}
}

Expand Down
7 changes: 0 additions & 7 deletions mvicore/src/main/java/com/badoo/mvicore/extension/Rx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.Scheduler
import io.reactivex.functions.Consumer
import io.reactivex.subjects.Subject

fun <T> Observer<T>.asConsumer() = Consumer<T> { onNext(it) }

internal fun <T> Observable<T>.observeOnNullable(scheduler: Scheduler?): Observable<T> =
if (scheduler != null) observeOn(scheduler) else this

internal fun <T> Observable<T>.subscribeOnNullable(scheduler: Scheduler?): Observable<T> =
if (scheduler != null) subscribeOn(scheduler) else this

internal fun <T> Subject<T>.serializeIfNotNull(param: Any?): Subject<T> =
if (param != null) toSerialized() else this
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@ import com.badoo.mvicore.element.Bootstrapper
import com.badoo.mvicore.element.NewsPublisher
import com.badoo.mvicore.element.Reducer

/**
* An implementation of a single threaded feature.
*
* Please be aware of the following threading behaviours based on whether a 'featureScheduler' is provided.
*
* No 'featureScheduler' provided:
* The feature must execute on the thread that created the class. If the bootstrapper/actor observables
* change to a different thread it is your responsibility to switch back to the feature's original
* thread via observeOn, otherwise an exception will be thrown.
*
* 'featureScheduler' provided (this must be single threaded):
* The feature does not have to execute on the thread that created the class. It automatically
* switches to the feature scheduler thread when necessary.
*/
open class ActorReducerFeature<Wish : Any, in Effect : Any, State : Any, News : Any>(
initialState: State,
bootstrapper: Bootstrapper<Wish>? = null,
actor: Actor<State, Wish, Effect>,
reducer: Reducer<State, Effect>,
newsPublisher: NewsPublisher<Wish, Effect, State, News>? = null,
schedulers: FeatureSchedulers? = null
featureScheduler: FeatureScheduler? = null
) : BaseFeature<Wish, Wish, Effect, State, News>(
initialState = initialState,
bootstrapper = bootstrapper,
wishToAction = { wish -> wish },
actor = actor,
reducer = reducer,
newsPublisher = newsPublisher,
schedulers = schedulers
featureScheduler = featureScheduler
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.badoo.mvicore.feature

import io.reactivex.Scheduler

/**
* A set of [Scheduler]s that change the threading behaviour of [BaseAsyncFeature]
*/
class AsyncFeatureSchedulers(
/** Should be single-threaded. */
val featureScheduler: Scheduler,
val observationScheduler: Scheduler
)
Loading