Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ReactorKit at Wantedly

ReactorKit at Wantedly

28.Jun.2018 ReactorKit Meetup Japan
https://wantedly.connpass.com/event/90261/

Jiro Nagashima

June 28, 2018
Tweet

More Decks by Jiro Nagashima

Other Decks in Technology

Transcript

  1. ©2018 Wantedly, Inc. An app which requires a lot of

    state management search, profile, chat, etc… Page Title Page Subtitle
  2. ©2018 Wantedly, Inc. Less typing Easy to understand Easy to

    code review ReactorKit is simple, therefore
  3. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. // ProjectViewController func bind(reactor: ProjectReactor)

    { // Action rx.methodInvoked(#selector(viewDidLoad)) .map { _ in Reactor.Action.load } .bind(to: reactor.action) .disposed(by: disposeBag) // State reactor.state.map { $0.isBookmarking } .bind(to: bookmarkButton.rx.isSelected) .disposed(by: disposeBag) }
  4. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. // ProjectReactor enum Action {

    case load } enum Mutation { case setProject(Project) } struct State { let projectId: Int var isBookmarking: Bool }
  5. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. // ProjectReactor func mutate(action: Action)

    -> Observable<Mutation> { switch action { case .load: return projectRepository.project(id: currentState.projectId) .asObservable() .map(Mutation.setProject) } } func reduce(state: State, mutation: Mutation) -> State { var state = state switch mutation { case let .setProject(project): state.isBookmarking = project.bookmarked } return state }
  6. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. // ProjectRepository func bookmark(projectId: Int)

    -> Single<Void> { return provider.rx.request(.bookmark(projectId: projectId)) .filterSuccessfulStatusCodes() .map { _ in } .do(onSubscribe: { _ in ProjectEvent.stream.onNext( .updateBookmarking(projectId: projectId, isBookmarking: true) ) }) .do(onError: { _ in ProjectEvent.stream.onNext( .updateBookmarking(projectId: projectId, isBookmarking: false) ) }) }
  7. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. // ProjectReactor enum Action {

    case toggleBookmarking } func mutate(action: Action) -> Observable<Mutation> { switch action { case .toggleBookmarking: if currentState.isBookmarking { _ = projectRepository.unbookmark(projectId: currentState.projectId).subscribe() } else { _ = projectRepository.bookmark(projectId: currentState.projectId).subscribe() } return .empty() } }
  8. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. // ProjectCellReactor func transform(mutation: Observable<Mutation>)

    -> Observable<Mutation> { let projectId = currentState.projectId let fromProjectEvent = ProjectEvent.stream .flatMap { event -> Observable<Mutation> in switch event { case let .updateBookmarking(id, isBookmarking): return id == projectId ? .just(.setBookmarking(isBookmarking)) : .empty() } } return Observable.merge(mutation, fromProjectEvent) } func reduce(state: State, mutation: Mutation) -> State { var state = state switch mutation { case let .setBookmarking(isBookmarking): state.isBookmarking = isBookmarking } return state }
  9. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2018 Wantedly, Inc. Company Home Posts Info Projects

    Employees Event Stream Reactor View Action State Reactor View Action State