-
Notifications
You must be signed in to change notification settings - Fork 90
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
Introduced ability to override AndroidMainThreadFeatureScheduler via plugin #194
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.badoo.mvicore.android | ||
|
||
import com.badoo.mvicore.feature.FeatureScheduler | ||
|
||
/** | ||
* Allows customisation of the MVICore Android integration. | ||
*/ | ||
object MviCoreAndroidPlugins { | ||
@Volatile | ||
var mainThreadFeatureScheduler: FeatureScheduler = AndroidMainThreadFeatureScheduler.Default | ||
|
||
/** | ||
* Overrides the [AndroidMainThreadFeatureScheduler]. | ||
*/ | ||
fun setMainThreadFeatureScheduler(schedulerProvider: () -> FeatureScheduler) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the point of using provider if you invoke it instantly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RxJava uses these as functions. I assume this is because some users may want to do some lazy initialisation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because in RxJava these providers are actually lazy and invoked only when we invoke |
||
mainThreadFeatureScheduler = schedulerProvider() | ||
} | ||
|
||
/** | ||
* Resets the plugins back to the original state. | ||
*/ | ||
fun reset() { | ||
mainThreadFeatureScheduler = AndroidMainThreadFeatureScheduler.Default | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.badoo.mvicore.android.lifecycle | ||
|
||
import com.badoo.mvicore.android.AndroidMainThreadFeatureScheduler | ||
import com.badoo.mvicore.android.MviCoreAndroidPlugins | ||
import com.badoo.mvicore.feature.FeatureSchedulers | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.schedulers.Schedulers | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Assertions.assertSame | ||
import org.junit.jupiter.api.Test | ||
|
||
class AndroidMainThreadFeatureSchedulerTest { | ||
@AfterEach | ||
fun after() { | ||
MviCoreAndroidPlugins.reset() | ||
} | ||
|
||
@Test | ||
fun `GIVEN android main scheduler not overridden WHEN scheduler accessed THEN scheduler is android main thread scheduler`() { | ||
assertSame(AndroidSchedulers.mainThread(), AndroidMainThreadFeatureScheduler.scheduler) | ||
} | ||
|
||
@Test | ||
fun `GIVEN android main scheduler overridden with trampoline feature scheduler WHEN scheduler accessed THEN scheduler is trampoline scheduler`() { | ||
MviCoreAndroidPlugins.setMainThreadFeatureScheduler { FeatureSchedulers.TrampolineFeatureScheduler } | ||
|
||
assertSame(Schedulers.trampoline(), AndroidMainThreadFeatureScheduler.scheduler) | ||
} | ||
|
||
@Test | ||
fun `GIVEN android main scheduler overridden with trampoline feature scheduler AND reset WHEN scheduler accessed THEN scheduler is android main thread scheduler`() { | ||
MviCoreAndroidPlugins.setMainThreadFeatureScheduler { FeatureSchedulers.TrampolineFeatureScheduler } | ||
MviCoreAndroidPlugins.reset() | ||
|
||
assertSame(AndroidSchedulers.mainThread(), AndroidMainThreadFeatureScheduler.scheduler) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hide? We have
reset()
for reset. Otherwise not clear what to use:AndroidMainThreadFeatureScheduler
orAndroidMainThreadFeatureScheduler.Default
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems reasonable