Skip to content

A simple isolated state implementation for ReactorKit

License

Notifications You must be signed in to change notification settings

wotjd/IsolatedStateReactor

Repository files navigation

IsolatedStateReactor

A simple isolated state implementation for ReactorKit

Previously,

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)

isolatedState

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
    }
  }
}

About

A simple isolated state implementation for ReactorKit

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages