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

Lazily start the controller when accessing the effects property #27

Closed
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Update kotlinx.coroutines to `1.4.3`.
- Remove kotlin as `api` dependency.
- Remove `ControllerLog.default`.
- Lazily start controller when accessing `Controller.effects` field (#26)

## `[0.13.1]` - 2020-09-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ internal class ControllerImplementation<Action, Mutation, State, Effect>(
get() = if (stubEnabled) {
stubbedEffectFlow.receiveAsFlow().cancellable()
} else {
if (controllerStart is ControllerStart.Lazy) start()
effectsChannel.receiveAsFlow().cancellable()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,31 @@ internal class ImplementationTest {
effects expect emissions(testEmissions)
}

@Test
fun `controller is started lazily when only effects field is accessed`() {
val sut = ControllerImplementation<Int, Int, Int, String>(
scope = testCoroutineScope,
dispatcher = testCoroutineScope.defaultScopeDispatcher(),
controllerStart = ControllerStart.Lazy,
initialState = 0,
mutator = { action -> flowOf(action) },
reducer = { mutation, _ -> mutation },
actionsTransformer = { actions ->
merge(actions, flow {
emitEffect("actionsTransformer started")
})
},
mutationsTransformer = { mutations -> mutations },
statesTransformer = { states -> states },
tag = "ImplementationTest.EffectController",
controllerLog = ControllerLog.None
)

val effects = sut.effects.testIn(testCoroutineScope)

effects expect emissions(listOf("actionsTransformer started"))
}

@Test
fun `effects are only received once collector`() {
val sut = testCoroutineScope.createEffectTestController()
Expand Down