Repository navigation
Expand file tree
/
Copy pathFeedback.swift
More file actions
189 lines (176 loc) · 7.63 KB
/
Copy pathFeedback.swift
File metadata and controls
189 lines (176 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import Combine
public struct Feedback<State, Event> {
let events: (_ state: AnyPublisher<State, Never>, _ output: FeedbackEventConsumer<Event>) -> Cancellable
internal init(events: @escaping (_ state: AnyPublisher<State, Never>, _ output: FeedbackEventConsumer<Event>) -> Cancellable) {
self.events = events
}
/// Creates a custom Feedback, with the complete liberty of defining the data flow.
///
/// - important: While you may respond to state changes in whatever ways you prefer, you **must** enqueue produced
/// events using the `SignalProducer.enqueue(to:)` operator to the `FeedbackEventConsumer` provided
/// to you. Otherwise, the feedback loop will not be able to pick up and process your events.
///
/// - parameters:
/// - setup: The setup closure to construct a data flow producing events in respond to changes from `state`,
/// and having them consumed by `output` using the `SignalProducer.enqueue(to:)` operator.
public static func custom(
_ setup: @escaping (
_ state: AnyPublisher<State, Never>,
_ output: FeedbackEventConsumer<Event>
) -> Cancellable
) -> Feedback<State, Event> {
return Feedback(events: setup)
}
/// Creates a Feedback which re-evaluates the given effect every time the
/// `Signal` derived from the latest state yields a new value.
///
/// If the previous effect is still alive when a new one is about to start,
/// the previous one would automatically be cancelled.
///
/// - parameters:
/// - transform: The transform which derives a `Signal` of values from the
/// latest state.
/// - effects: The side effect accepting transformed values produced by
/// `transform` and yielding events that eventually affect
/// the state.
public init<U, Effect: Publisher>(
compacting transform: @escaping (AnyPublisher<State, Never>) -> AnyPublisher<U, Never>,
effects: @escaping (U) -> Effect
) where Effect.Output == Event, Effect.Failure == Never {
self.events = { state, output in
// NOTE: `observe(on:)` should be applied on the inner producers, so
// that cancellation due to state changes would be able to
// cancel outstanding events that have already been scheduled.
transform(state)
.flatMapLatest { effects($0).enqueue(to: output) }
.sink { _ in }
}
}
/// Creates a Feedback which re-evaluates the given effect every time the
/// state changes, and the transform consequentially yields a new value
/// distinct from the last yielded value.
///
/// If the previous effect is still alive when a new one is about to start,
/// the previous one would automatically be cancelled.
///
/// - parameters:
/// - transform: The transform to apply on the state.
/// - effects: The side effect accepting transformed values produced by
/// `transform` and yielding events that eventually affect
/// the state.
public init<Control: Equatable, Effect: Publisher>(
skippingRepeated transform: @escaping (State) -> Control?,
effects: @escaping (Control) -> Effect
) where Effect.Output == Event, Effect.Failure == Never {
self.init(
compacting: {
$0.map(transform)
.removeDuplicates()
.eraseToAnyPublisher()
},
effects: {
$0.map(effects)?
.eraseToAnyPublisher() ?? Empty().eraseToAnyPublisher()
}
)
}
/// Creates a Feedback which re-evaluates the given effect every time the
/// state changes.
///
/// If the previous effect is still alive when a new one is about to start,
/// the previous one would automatically be cancelled.
///
/// - parameters:
/// - transform: The transform to apply on the state.
/// - effects: The side effect accepting transformed values produced by
/// `transform` and yielding events that eventually affect
/// the state.
public init<Control, Effect: Publisher>(
lensing transform: @escaping (State) -> Control?,
effects: @escaping (Control) -> Effect
) where Effect.Output == Event, Effect.Failure == Never {
self.init(
compacting: {
$0.map(transform).eraseToAnyPublisher()
},
effects: {
$0.map(effects)?.eraseToAnyPublisher() ?? Empty().eraseToAnyPublisher()
}
)
}
/// Creates a Feedback which re-evaluates the given effect every time the
/// given predicate passes.
///
/// If the previous effect is still alive when a new one is about to start,
/// the previous one would automatically be cancelled.
///
/// - parameters:
/// - predicate: The predicate to apply on the state.
/// - effects: The side effect accepting the state and yielding events
/// that eventually affect the state.
public init<Effect: Publisher>(
predicate: @escaping (State) -> Bool,
effects: @escaping (State) -> Effect
) where Effect.Output == Event, Effect.Failure == Never {
self.init(
compacting: { $0 },
effects: { state in
predicate(state) ? effects(state).eraseToAnyPublisher() : Empty().eraseToAnyPublisher()
}
)
}
/// Creates a Feedback which re-evaluates the given effect every time the
/// state changes.
///
/// If the previous effect is still alive when a new one is about to start,
/// the previous one would automatically be cancelled.
///
/// - parameters:
/// - effects: The side effect accepting the state and yielding events
/// that eventually affect the state.
public init<Effect: Publisher>(
effects: @escaping (State) -> Effect
) where Effect.Output == Event, Effect.Failure == Never {
self.init(compacting: { $0 }, effects: effects)
}
}
extension Feedback {
public static func pullback<LocalState, LocalEvent>(
feedback: Feedback<LocalState, LocalEvent>,
value: KeyPath<State, LocalState>,
event: @escaping (LocalEvent) -> Event
) -> Feedback<State, Event> {
return Feedback.custom { (state, consumer) -> Cancellable in
return feedback.events(
state.map(value).eraseToAnyPublisher(),
consumer.pullback(event)
)
}
}
public static func combine(_ feedbacks: Feedback<State, Event>...) -> Feedback<State, Event> {
return Feedback.custom { (state, consumer) -> Cancellable in
return feedbacks.map { (feedback) -> Cancellable in
return feedback.events(state, consumer)
}
}
}
public func mapEvent<U>(_ f: @escaping (Event) -> U) -> Feedback<State, U> {
return Feedback<State, U>.custom { (state, consumer) -> Cancellable in
self.events(state, consumer.pullback(f))
}
}
public static var input: (feedback: Feedback, observer: (Event) -> Void) {
let subject = PassthroughSubject<Event, Never>()
let feedback = Feedback.custom { (state, consumer) -> Cancellable in
subject.enqueue(to: consumer).sink(receiveValue: { _ in })
}
return (feedback, subject.send)
}
}
extension Array: Cancellable where Element == Cancellable {
public func cancel() {
for element in self {
element.cancel()
}
}
}