@@ -53,7 +53,7 @@ SwiftDux helps build SwiftUI-based applications around an [elm-like architecture
5353
5454The state is an immutable structure acting as the single source of truth within the application.
5555
56- Below is an example of a todo app's state. It has a root ` AppState ` as well as an ordered list of ` TodoState ` objects.
56+ Below is an example of a todo app's state. It has a root ` AppState ` as well as an ordered list of ` TodoItem ` objects.
5757
5858``` swift
5959import SwiftDux
@@ -127,7 +127,7 @@ window.rootViewController = UIHostingController(
127127## Middleware
128128SwiftDux supports middleware to extend functionality. The SwiftDuxExtras module provides two built-in middleware to get started:
129129
130- - ` PersistStateMiddleware ` Persists and restores the application state between sessions.
130+ - ` PersistStateMiddleware ` persists and restores the application state between sessions.
131131- ` PrintActionMiddleware ` prints out each dispatched action for debugging purposes.
132132
133133``` swift
@@ -251,16 +251,35 @@ struct MyView: View {
251251}
252252```
253253
254- If it's an ActionPlan that's meant to be kept alive through a publisher, then you'll want to send it as a cancellable.
254+ If it's an ActionPlan that's meant to be kept alive through a publisher, then you'll want to send it as a cancellable. The action below subscribes
255+ to the store, so it can keep a list of albums updated when the user applies different queries.
255256
256257```swift
257- struct MyView: View {
258+ extension AlbumListAction {
259+ var updateAlbumList: Action {
260+ ActionPlan< AppState> { store in
261+ store
262+ .publish { $0 .albumList .query }
263+ .debounce (for : .seconds (1 ), scheduler : RunLoop.main )
264+ .map { AlbumService.all (query : $0 ) }
265+ .switchToLatest ()
266+ .catch { Just (AlbumListAction.setError ($0 ) }
267+ .map { AlbumListAction.setAlbums ($0 ) }
268+ }
269+ }
270+ }
271+
272+ struct AlbumListContainer: ConnectableView {
258273 @Environment (\.actionDispatcher ) private var dispatch
259274 @State private var cancellable: Cancellable? = nil
275+
276+ func map (state : AppState) -> [Album]? {
277+ state.albumList .albums
278+ }
260279
261- var body: some View {
262- MyForm .onAppear {
263- cancellable = dispatch.sendAsCancellable (SecurityAction. whileAccessTokenIsValid )
280+ func body ( props : [Album]) -> some View {
281+ AlbumsList ( albums : props) .onAppear {
282+ cancellable = dispatch.sendAsCancellable (AlbumListAction. updateAlbumList )
264283 }
265284 }
266285}
@@ -269,9 +288,14 @@ struct MyView: View {
269288The above can be further simplified by using the built- in `onAppear (dispatch: )` method instead. This method not only dispatches regular actions, but it automatically handles cancellable ones. By default , the action will cancel itself when the view is destroyed.
270289
271290```swift
272- struct MyView: View {
273- var body: some View {
274- MyForm.onAppear (dispatch : SecurityAction.whileAccessTokenIsValid )
291+ struct AlbumListContainer: ConnectableView {
292+
293+ func map (state : AppState) -> [Album]? {
294+ Props (state.albumList .albums )
295+ }
296+
297+ func body (props : [Album]) -> some View {
298+ AlbumsList (albums : props).onAppear (dispatch : AlbumListAction.updateAlbumList )
275299 }
276300}
277301```
0 commit comments