A simple isolated state implementation for ReactorKit
distinctUntilChanged
operator follows state observable and this produces quiet complicated code.
for example,
// in bind(reactor:)
reactor.state.map(\.someBoolState)
.distinctUntilChanged()
...
// in reactor.mutate(action:)
// to avoid skipping event by distinctUntilChanged
return Observable.of(true, false).map(Mutation.updateBoolState)
to solve this, I made isolatedState
// usage
reactor.isolatedState(\.boolProperty)
// work properly without distinctUntilChanged()
.subscribe(...)
// implementation
class ViewReactor: IsolatedStateReactor {
enum Action { ... }
enum Mutation { ... }
// State should provide updates stores keyPath array to know which property is updated by mutation
struct State: UpdateStorable {
var boolProperty = false
var updates: [PartialKeyPath<Self>] = [\State.self]
}
func mutate(action: Action) -> Observable<Mutation> { ... }
func reduce(isolatedState: inout IsolatedState, mutation: Mutation) {
switch mutation {
case let .updateBoolProperty(bool):
state.boolProperty = bool
}
}
}