Skip to content

transformations

Florian Schuster edited this page Apr 19, 2020 · 7 revisions

transform functions are called once for corresponding Flow's in a Controller.

actionsTransformer

transforms the action Flow that is created directly after the Controller.dispatch function.

a possible use case for this transformation could be to implement an initial action:

val controller = scope.createController<Action, Mutation, State>(
    actionsTransformer = { actions ->
        actions.onStart { emit(Action.InitialLoad) }
    }
    ...,
)

mutationsTransformer

a possible use case is merging a global state with the Controller state:

val userSession: Flow<Session>

val controller = scope.createController<Action, Mutation, State>(
    mutationsTransformer = { mutations ->
        merge(mutations, userSession.map { Mutation.SetSession(it) })
    }
    ...,
)

every time userSession emits a new session, its gets reduced into the Controller.state via Mutation.SetSession

statesTransformer

 🤷‍♂️

val controller = scope.createController<Action, Mutation, State>(
    statesTransformer = { states -> states.onEach { println("new state: $it") } }
    ...,
)