Skip to content

Commit a774dfa

Browse files
committed
readme: update for execute(on:) on transitions and system
1 parent 29a13dd commit a774dfa

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

Diff for: README.md

+51-5
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ In our example, one feedback takes care of increasing the volume and the other i
9696

9797
## Scheduling
9898

99-
Threading is very important to make a nice responsive application. A Scheduler is the Combine way of handling threading by switching portions of reactive streams on dispatch queues, or operation queues or RunLoops.
99+
Threading is very important to make a nice responsive application. A Scheduler is the Combine way of handling threading by switching portions of reactive streams on dispatch queues, operation queues or RunLoops.
100100

101-
The declarative syntax of Feedbacks allows to alter the behavior of side effects by simply applying modifiers (like you would do with SwiftUI to change the frame for instance). Modifying the scheduling of a side effect is as simple as calling the `.execute(on:)` modifier.
101+
The declarative syntax of Feedbacks allows to alter the behavior of a System by simply applying modifiers (like you would do with SwiftUI to change the frame for instance). Modifying the scheduling of a side effect is as simple as calling the `.execute(on:)` modifier.
102102

103103
```swift
104104
Feedbacks {
@@ -128,6 +128,52 @@ Feedbacks {
128128

129129
Both side effects will be executed on the background queue.
130130

131+
It is also applicable to the transitions:
132+
133+
```swift
134+
Transitions {
135+
From(VolumeState.self) { state in
136+
On(IncreaseEvent.self, transitionTo: VolumeState(value: state.value + 1))
137+
On(DecreaseEvent.self, transitionTo: VolumeState(value: state.value - 1))
138+
}
139+
}.execute(on: DispatchQueue(label: "A background queue"))
140+
```
141+
142+
or to the whole system:
143+
144+
```swift
145+
System {
146+
InitialState {
147+
VolumeState(value: 10)
148+
}
149+
150+
Feedbacks {
151+
Feedback(on: VolumeState.self, strategy: .continueOnNewState) { state -> AnyPublisher<Event, Never> in
152+
if state.value >= targetedVolume {
153+
return Empty().eraseToAnyPublisher()
154+
}
155+
156+
return Just(IncreaseEvent()).eraseToAnyPublisher()
157+
}
158+
159+
Feedback(on: VolumeState.self, strategy: .continueOnNewState) { state -> AnyPublisher<Event, Never> in
160+
if state.value <= targetedVolume {
161+
return Empty().eraseToAnyPublisher()
162+
}
163+
164+
return Just(DecreaseEvent()).eraseToAnyPublisher()
165+
}
166+
}
167+
168+
Transitions {
169+
From(VolumeState.self) { state in
170+
On(IncreaseEvent.self, transitionTo: VolumeState(value: state.value + 1))
171+
On(DecreaseEvent.self, transitionTo: VolumeState(value: state.value - 1))
172+
}
173+
}
174+
}.execute(on: DispatchQueue(label: "A background queue"))
175+
```
176+
131177
## Lifecycle
132178

133179
There are typical cases where a side effect consist of an asynchronous operation (like a network call). What happens if the very same side effect is called repeatedly, not waiting for the previous ones to end? Are the operations stacked? Are they cancelled when a new one is performed?
@@ -196,9 +242,9 @@ Here is a list of the supported modifiers:
196242
| Modifier | Action | Can be applied to |
197243
| -------------- | -------------- | -------------- |
198244
| `.disable(disabled:)`| The target won't be executed as long as the `disabled` condition is true | <ul align="left"><li>Transition</li><li>Transitions</li><li>Feedback</li></ul> |
199-
| `.execute(on:)`| The target will be executed on the scheduler | <ul align="left"><li>Feedbacks</li><li>Feedback</li></ul> |
200-
| `.onStateReceived(perform:)`| Execute the `perform` closure each time a new state is given as an input | <ul align="left"><li>Feedbacks</li><li>Feedback</li></ul> |
201-
| `.onEventEmitted(perform:)`| Execute the `perform` closure each time a new event is emitted | <ul align="left"><li>Feedbacks</li><li>Feedback</li></ul> |
245+
| `.execute(on:)`| The target will be executed on the scheduler | <ul align="left"><li>Transitions</li><li>Feedback</li><li>Feedbacks</li><li>System</li></ul> |
246+
| `.onStateReceived(perform:)`| Execute the `perform` closure each time a new state is given as an input | <ul align="left"><li>Feedback</li><li>Feedbacks</li></ul> |
247+
| `.onEventEmitted(perform:)`| Execute the `perform` closure each time a new event is emitted | <ul align="left"><li>Feedback</li><li>Feedbacks</li></ul> |
202248
| `.attach(to:)`| Refer to the "How to make systems communicate" section | <ul align="left"><li>System</li><li>UISystem</li></ul> |
203249
| `.uiSystem(viewStateFactory:)`| Refer to the "Using Feedbacks with SwiftUI and UIKit" section | <ul align="left"><li>System</li></ul> |
204250

0 commit comments

Comments
 (0)