Skip to content

Terminal events in Actions section

Sergej Shafarenka edited this page Jun 19, 2020 · 5 revisions

Perform-handlers declared in actions {} section must never terminate in order to perform actions multiple times.

In the example below, the Action.Load observable will complete when onErrorReturn is executed.

actions {
    perform<Action.Load> {
        flatMapSingle<Data> { loadData() }
            .map<Change> { Change.Load.Success(it) }
            .onErrorReturn { Change.Load.Failure(it) }
    }
}

Knot will throw an IllegalStateException when action observable terminates, to highlight the implementation error. Applying onErrorReturn onto the single inside the flatMapSingle operator solves the issue.

Here is the corrected implementation.

actions {
    perform<Action.Load> {
        flatMapSingle { 
            loadData()
                .map<Change> { Change.Load.Success(it) }
                .onErrorReturn { Change.Load.Failure(it) }
        }
    }
}
Clone this wiki locally