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

Introducing ReactKit

Introducing ReactKit

Eac0bf787b5279aca5e699ece096956e?s=128

Yasuhiro Inami

December 17, 2014
Tweet

Transcript

  1. Frameworks Yasuhiro Inami Ex-iOS Engineer #potatotips 2014-12-17 Session 203 ©

    2014 @inamiy, All rights reserved. Redistribution or public display not permitted without written permission from @inamiy. Introducing ReactKit
  2. Who are you? Yasuhiro Inami (@inamiy) LINE corp / NAVER

    Matome Ex-iOS / Java Server Engineer • Objective-C: 4 years, 26 repos • Java: 10 months, 0 repo • Swift: 6 months, 8 repos
  3. https://github.com/ReactKit

  4. Overview What is ReactKit Demo How it works • SwiftState

    • SwiftTask When to use
  5. What is ReactKit Swift Reactive Programming

  6. let signal = KVO.signal(obj1, "value") ! // REACT (obj2, "value")

    <~ signal ! XCTAssertEqual(obj1.value, "initial") XCTAssertEqual(obj2.value, "initial") ! obj1.value = "hoge" ! XCTAssertEqual(obj1.value, "hoge") XCTAssertEqual(obj2.value, "hoge") // reactive! Key-Value Observing/Coding
  7. self.signal = Notification.signal("MyNotification", obj1).map { notification -> NSString? in !

    return notification!.userInfo!["hoge"] as? NSString ! } ! // REACT (obj2, "value") <~ self.signal! NSNotification
  8. // UITextField self.textFieldSignal = self.textField?.textChangedSignal() ! (self.label, "text") <~ self.textFieldSignal!

    ! ! // UIButton self.buttonSignal = self.button?.buttonSignal { _ in return "OK" } ! (self.label, "text") <~ self.buttonSignal! Target-Action
  9. Demo ReactKitCatalog

  10. How it works The underlying frameworks

  11. None
  12. State Machine

  13. SwiftState Elegant state machine for Swift State & Event (Enum/String)

    based transition manager • Guard conditions • Wildcard state (AnyState) • Success/Error/Entry/Exit handlers with UInt8 order • Removable routes & handlers Easy Swift Syntax • Transition: .State0 => .State1 • Transition chaining: .State0 => .State1 => .State2 • Try transition: machine <- .State1 • Try transition + messaging: 
 machine <- (.State1, "GoGoGo") • Try event: machine <-! .Event1
  14. SwiftState Elegant state machine for Swift [New!] Hierarchical State Machine

  15. State Machine ↓ Promise

  16. None
  17. None
  18. SwiftTask Promise + progress + pause + cancel + retry

    Future fulfilled/rejected value-pipelining with • progress • pause/resume • cancel • [New!] try (retryable) Pure Swift (for single task) Very thin wrapper around callback-hell objects Focused on pipelining only • no concurrency programming implemented • use whatever you want inside task-definition • e.g. GCD, NSThread, NSOperation, POSIX threads
  19. Demo SwiftState & SwiftTask

  20. State Machine ↓ Promise ↓ Reactive

  21. ReactKit Core code public class Signal<T>: Task<T, T, NSError> {

    public let name: String public init(name: String = "Default", initClosure: Task<T, T, NSError>.InitClosure) { self.name = name super.init(weakified: true, initClosure: initClosure) } }
  22. That’s it!

  23. ReactKit Signal Operations // Signal Operations public extension Signal {

    public func filter(filterClosure: T -> Bool) -> Signal<T> public func map<U>(transform: T -> U) -> Signal<U> public func take(maxCount: Int) -> Signal public func take<U>(until signal: Signal<U>) -> Signal }
  24. ReactKit Signal Operations Instance Methods (ver 0.3.0) • filter(f: T

    -> Bool) • map(f: T -> U) • map(f: T -> Signal<U>) (a.k.a Rx.flatMap) • map(accumulate:accumulateClosure:) (a.k.a Rx.scan) • take(count) • take(until: Signal) • skip(count) • skip(until: Signal) • buffer(count) • buffer(trigger: Signal) • throttle(timeInterval) • debounce(timeInterval) Class Methods • any(signals)
  25. When to use … Now!

  26. When to use … Now! Use cases • Networking •

    User interface binding • …and a lot Can replace if your code uses • ReactiveCocoa • PromiseKit • Bolts-iOS • FBKVOController • …and a lot
  27. When to use … Now! [New!] Carthage 0.3 compatible •

    echo 'github "ReactKit/ReactKit" ~> 0.3.0' >> Cartfile • carthage update (--use-submodules) • Drag & drop all frameworks to embed • Enjoy ~> iOS8 Contributors wanted! • Send me GitHub-issue or pull-request • or, please star
  28. None
  29. More Information SwiftͰ༗ݶΦʔτϚτϯʢεςʔτϚγϯʣΛ࡞Δ - Qiita http://qiita.com/inamiy/items/cd218144c90926f9a134 ! Swiftʴ༗ݶΦʔτϚτϯͰPromiseΛ֦ு͢Δ - Qiita

    http://qiita.com/inamiy/items/d3579b55a3ecc28dde63 ! SwiftTaskʢPromise֦ுʣΛ࢖͏ - Qiita http://qiita.com/inamiy/items/0756339aee35849384c3 ! SwiftTask(Promise֦ு)ͰϦΞΫςΟϒϓϩάϥϛϯά - Qiita http://qiita.com/inamiy/items/d68cdb528b482ee0d8c1
  30. More Information Yasuhiro Inami Ex-iOS Engineer inamiy@gmail.com