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

Create single threaded Feature with scheduler #158

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Removed onNext behaviour from observeOnFeatureScheduler
  • Loading branch information
LachlanMcKee committed Aug 3, 2022
commit 53aa5f37862fcf7f19f25e8036afbd7d21693165
25 changes: 8 additions & 17 deletions mvicore/src/main/java/com/badoo/mvicore/feature/BaseFeature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ open class BaseFeature<Wish : Any, in Action : Any, in Effect : Any, State : Any
disposables += postProcessorWrapper
disposables += newsPublisherWrapper
disposables += actionSubject
.observeOnFeatureScheduler(featureScheduler) { action ->
invokeActor(state, action)
}
.subscribe()
.observeOnFeatureScheduler(featureScheduler)
.subscribe { action -> invokeActor(state, action) }

if (bootstrapper != null) {
setupBootstrapper(bootstrapper)
Expand All @@ -94,11 +92,9 @@ open class BaseFeature<Wish : Any, in Action : Any, in Effect : Any, State : Any
disposables +=
Observable
.defer { bootstrapper() }
.observeOnFeatureScheduler(featureScheduler) { action ->
output.accept(action)
}
.observeOnFeatureScheduler(featureScheduler)
.subscribeOnNullable(featureScheduler?.scheduler)
.subscribe()
.subscribe { action -> output.accept(action) }
}
}

Expand Down Expand Up @@ -163,15 +159,13 @@ open class BaseFeature<Wish : Any, in Action : Any, in Effect : Any, State : Any
disposable =
actor
.invoke(state, action)
.observeOnFeatureScheduler(featureScheduler) { effect ->
invokeReducer(stateSubject.value!!, action, effect)
}
.observeOnFeatureScheduler(featureScheduler)
.doAfterTerminate {
// Remove disposables manually because CompositeDisposable does not do it automatically producing memory leaks
// Check for null as it might be disposed instantly
disposable?.let(disposables::remove)
}
.subscribe()
.subscribe { effect -> invokeReducer(stateSubject.value!!, action, effect) }

// Disposable might be already disposed in case of no scheduler + Observable.just
if (!disposable.isDisposed) disposables += disposable
Expand Down Expand Up @@ -277,18 +271,15 @@ open class BaseFeature<Wish : Any, in Action : Any, in Effect : Any, State : Any

private companion object {
private fun <T : Any> Observable<T>.observeOnFeatureScheduler(
scheduler: FeatureScheduler?,
func: (T) -> Unit
scheduler: FeatureScheduler?
): Observable<T> =
flatMap { value ->
val upstream = Observable.just(value)
if (scheduler == null || scheduler.isOnFeatureThread) {
upstream
.doOnNext { func(it) }
} else {
upstream
.observeOn(scheduler.scheduler)
.doOnNext { func(it) }
.subscribeOn(scheduler.scheduler)
}
}
}
Expand Down