Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Simplifying State by partially introducing unidirectional data flow in your codebase
Search
Benedikt Terhechte
October 13, 2017
Programming
3
730
Simplifying State by partially introducing unidirectional data flow in your codebase
Benedikt Terhechte
October 13, 2017
Tweet
Share
More Decks by Benedikt Terhechte
See All by Benedikt Terhechte
NSSpain 2023: An overview of different approaches to share code across platforms
terhechte
0
230
SwiftUI & UIKit, a match made in heaven or match made in hell?
terhechte
0
50
Learnings from building Design Systems at XING
terhechte
1
350
Introduction to Swift Keypaths
terhechte
15
19k
Sharing Code between iOS and Android with Rust
terhechte
8
2.1k
Protocols with Associated Types: When to use them and how to avoid their pitfalls
terhechte
2
250
Swift on the Server
terhechte
2
270
Macoun 2016 - Swift auf dem Server
terhechte
3
250
NSSpain 2016: Developing App-Backends with Swift on the Server
terhechte
2
23k
Other Decks in Programming
See All in Programming
CQRS/ES avec Symfony, c’est (trop) bien !
jeremyfreeagent
1
640
コーンフレークから始める モデリング会話入門
ogurotakayuki
0
350
Site Reliability Engineering for GMO
pyama86
7
1k
見た目から始める生産性向上
ikumatadokoro
7
770
Ruby GitHub Packages
bkuhlmann
0
630
Code Reviews
bkuhlmann
4
890
Compose-View Interop in Practice (mDevCamp 2024)
stewemetal
0
110
PHP8.3の機能を振り返る / Review of PHP 8.3 features
seike460
PRO
1
110
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
630
Goのエラースタックトレースの歴史と今後
sonatard
6
640
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
200
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
513
39k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.5k
Building Better People: How to give real-time feedback that sticks.
wjessup
354
18k
Mobile First: as difficult as doing things right
swwweet
216
8.6k
Testing 201, or: Great Expectations
jmmastey
27
6.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
Building an army of robots
kneath
300
41k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
658
120k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
77
42k
Agile that works and the tools we love
rasmusluckow
324
20k
In The Pink: A Labor of Love
frogandcode
138
21k
Transcript
1
2
3
4
5
6
7
8
9
None
10
11
12
13
14
None
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// Model struct Cinnamon { let value: Int = 0
} enum Event { case increase } 45
/// Model struct Cinnamon { let value: Int = 0
} enum Event { case increase } func reducer(state: Cinnamon, event: Event) -> Cinnamon { var newState = state if event == .increase { newState.value += 1 } return newState } /// UI let builder = Form(state: Cinnamon(), reducer: reducer) 46
47
48
49
struct AddressBook { var contacts: [Person] var searchTerm: String var
scrollPosition: Int } 50
struct AddressBookApp { struct Data { var contacts: [Person] }
var data: Data struct UI { var searchTerm: String var scrollPosition: Int } var ui: UI } 51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
private var subscribers: [String: (State)->Void] = [:] public func subscribe(_
subscriber: @escaping (State)->Void) -> String { let token = UUID().uuidString subscribers[token] = subscriber subscriber(state) return token } 66
67
protocol FormComponent { associatedtype State func setup(with state: State) func
update(state: State) -> Void } public func subscribe<Component: FormComponent> (_ subscriber: Component) -> String where Component.State == State {... 68
69
func subscribe<Type: Equatable>( path: KeyPath<Data, Type>, action: @escaping (_ oldValue:
Type, _ newValue: Type) -> Void ) -> String struct Person { let name: String } form.subscribe(path: \Person.name) { (old, new) in ... } 70
private var history: [State] = [] func apply(_ change: (inout
State) -> Void) { states.append(state) change(&state) notifySubscribers() } func undo() { state = history.popLast() notifySubscribers() } 71
72
73
74
75
76
77
78
79
80