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

マッチングアプリにつきまとう状態管理のつらさ/torte_state

corin8823
October 18, 2017

 マッチングアプリにつきまとう状態管理のつらさ/torte_state

corin8823

October 18, 2017
Tweet

More Decks by corin8823

Other Decks in Technology

Transcript

  1. final class TopAction { enum Item { case load case

    error } private let dispatcher: Dispatcher init(dispatcher: Dispatcher = .shared) { self.dispatcher = dispatcher } func loadItem() { let req = API.ItemRequest() Session.send(req) { result in switch result { case .success(let items): self.dispatcher.dispatch(obj: items, key: Item.load) case .failure(let error): self.dispatcher.dispatch(obj: error, key: Item.error) } } } }
  2. final class TopStore { private(set) var items = Variable<[Item]>([]) private(set)

    var error = PublishSubject<Error>() init(dispatcher: Dispatcher = .shared) { dispatcher.register(observer: self, key: TopAction.Item.load) { [weak self] (items: [Item]) in self?.items.value = items } dispatcher.register(observer: self, key: TopAction.Item.error) { [weak self] (error: Error) in self?.error.on(.next(error)) } } }
  3. final class TopViewController: UIViewController { @IBOutlet weak var tableView: UITableView!

    private let store = TopStore() private let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() self.store.error .bind { _ in // error handling } .disposed(by: self.disposeBag) self.store.items .asObservable() .bind { [weak self] _ in self?.tableView.reloadData() } .disposed(by: self.disposeBag) TopAction().loadItem() } }
  4. final class TopDetailAction { enum ItemDetail { case update case

    error } func update(itemId: Int64) { let req = API.UpdateItemRequest(itemId: id) Session.send(req) { result in switch result { case .success(let item): self.dispatcher.dispatch(obj: item, key: ItemDetail.update) 
 case .failure(let error): self.dispatcher.dispatch(obj: error, key: update.error) } } } }
  5. final class TopDetailAction { enum ItemDetail { case update case

    error } enum Item { case update } func update(itemId: Int64) { let req = API.UpdateItemRequest(itemId: id) Session.send(req) { result in switch result { case .success(let item): self.dispatcher.dispatch(obj: item, key: ItemDetail.update) self.dispatcher.dispatch(obj: item, key: Item.update) 
 case .failure(let error): self.dispatcher.dispatch(obj: error, key: update.error) } } } }
  6. final class TopStore { private(set) var items = Variable<[Item]>([]) private(set)

    var error = PublishSubject<Error>() init(dispatcher: Dispatcher = .shared) { dispatcher.register(observer: self, key: TopAction.Item.load) { [weak self] (items: [Item]) in self?.items.value = items } dispatcher.register(observer: self, key: TopAction.Item.error) { [weak self] (error: Error) in self?.error.on(.next(error)) } dispatcher.register(observer: self, key: TopDetailAction.Item.update) { [weak self] (item: Item) in
 if let i = self?.items.value.findFirstIndex ({ $0.id == item.id }) { self?.items.value[i] = match } } } }
  7. final class TopViewController: UIViewController { @IBOutlet weak var tableView: UITableView!

    private let store = TopStore() private let disposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() self.store.error .bind { _ in // error handling } .disposed(by: self.disposeBag) self.store.items .asObservable() .bind { [weak self] _ in self?.tableView.reloadData() } .disposed(by: self.disposeBag) TopAction().loadItem() } }
  8. w VTFST\VTFS@JEతͳ^CBEHF ۩ମྫ class Badge { var notification: Int var

    match: Int var message: Int } w OPUJpDBUJPO͕૿͑ͨΒ࠶౓ࣗ෼Λऔಘ w NBUDI͕૿͑ͨΒϚονͷϦετΛߋ৽ w NFTTBHF͕૿͑ͨΒόοδΛ͚ͭͯɾɾ