28.Jun.2018 ReactorKit Meetup Japan https://wantedly.connpass.com/event/90261/
©2018 Wantedly, Inc.ReactorKit at WantedlyReactorKit Meetup Japan28.Jun.2018 - Jiro Nagashima
View Slide
©2018 Wantedly, Inc.ReactorKit is simple and powerfulPage Title Page Subtitle
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.Jiro Nagashima@hedjirogSoftware EngineerWantedly Visit App
©2018 Wantedly, Inc. https://wantedlyinc.com/ja/products
©2018 Wantedly, Inc.An app which requiresa lot of state managementsearch, profile, chat, etc…Page Title Page Subtitle
©2018 Wantedly, Inc.ReactorKit took care of it all ❤Page Title Page Subtitle
©2018 Wantedly, Inc.2017Page Title Page Subtitle2018Used in one screenUsed everywhere
©2018 Wantedly, Inc.ReactorKit is simplePage Title Page Subtitle
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc. https://github.com/ReactorKit/ReactorKit#basic-concept
©2018 Wantedly, Inc.Less typingEasy to understandEasy to code reviewReactorKit is simple, therefore
©2018 Wantedly, Inc.ReactorKit is powerfulPage Title Page Subtitle
©2018 Wantedly, Inc.Example 1Showing bookmarked statusPage Title Page Subtitle
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.ReactorViewActionState
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.// ProjectViewControllerfunc bind(reactor: ProjectReactor) {// Actionrx.methodInvoked(#selector(viewDidLoad)).map { _ in Reactor.Action.load }.bind(to: reactor.action).disposed(by: disposeBag)// Statereactor.state.map { $0.isBookmarking }.bind(to: bookmarkButton.rx.isSelected).disposed(by: disposeBag)}
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.// ProjectReactorenum Action {case load}enum Mutation {case setProject(Project)}struct State {let projectId: Intvar isBookmarking: Bool}
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.// ProjectReactorfunc mutate(action: Action) -> Observable {switch action {case .load:return projectRepository.project(id: currentState.projectId).asObservable().map(Mutation.setProject)}}func reduce(state: State, mutation: Mutation) -> State {var state = stateswitch mutation {case let .setProject(project):state.isBookmarking = project.bookmarked}return state}
©2018 Wantedly, Inc.Example 2Bookmark event propagationPage Title Page Subtitle
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.ReactorViewActionStateReactorViewActionStateEvent Stream
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.// ProjectRepositoryfunc bookmark(projectId: Int) -> Single {return provider.rx.request(.bookmark(projectId: projectId)).filterSuccessfulStatusCodes().map { _ in }.do(onSubscribe: { _ inProjectEvent.stream.onNext(.updateBookmarking(projectId: projectId, isBookmarking: true))}).do(onError: { _ inProjectEvent.stream.onNext(.updateBookmarking(projectId: projectId, isBookmarking: false))})}
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.// ProjectReactorenum Action {case toggleBookmarking}func mutate(action: Action) -> Observable {switch action {case .toggleBookmarking:if currentState.isBookmarking {_ = projectRepository.unbookmark(projectId: currentState.projectId).subscribe()} else {_ = projectRepository.bookmark(projectId: currentState.projectId).subscribe()}return .empty()}}
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.// ProjectCellReactorfunc transform(mutation: Observable) -> Observable {let projectId = currentState.projectIdlet fromProjectEvent = ProjectEvent.stream.flatMap { event -> Observable inswitch 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 = stateswitch mutation {case let .setBookmarking(isBookmarking):state.isBookmarking = isBookmarking}return state}
©2018 Wantedly, Inc.Example 3Company pagePage Title Page Subtitle
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.CompanyHome Posts Info Projects Employees
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ©2018 Wantedly, Inc.CompanyHome Posts Info Projects EmployeesEvent StreamReactorViewActionStateReactorViewActionState
©2018 Wantedly, Inc.Complicated state management is possibleCovers most use casesReactorKit is powerful, therefore
©2018 Wantedly, Inc.I can wholeheartedly recommend it!Page Title Page Subtitle