Skip to content

Commit

Permalink
Fixes #26
Browse files Browse the repository at this point in the history
thanks @FlowMo7
  • Loading branch information
floschu committed Jun 22, 2021
1 parent 13b3e3e commit da6568e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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 @@ -327,6 +327,31 @@ internal class ImplementationTest {
sut.cancel()
}

@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"))
}

private fun CoroutineScope.createAlwaysSameStateController() =
ControllerImplementation<Unit, Unit, Int, Nothing>(
scope = this,
Expand Down

0 comments on commit da6568e

Please sign in to comment.