Skip to content

Terminal events in Actions section

Sergej Shafarenka edited this page Mar 7, 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<String> { loadData() }
            .map<Change> { Change.Load.Success(it) }
            .onErrorReturn { Change.Load.Failure(it) }
    }
}

Knot will throw an IllegalStateException when the observable terminates in order to signal the implementation error. Applying onErrorReturn to the single inside flatMapSingle solves the issue. Here is the correct implementation.

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