Structs

The following structs are available globally.

  • LoggerMiddleware that logs the action and state when each action is received When an action is dispatched, the LoggerMiddleware will print information related to it in the console.

    Example

    Adding LoggerMiddleware

    let store = Suas.createStore(
      reducer: ...,
      middleware: LoggerMiddleware()
    )
    

    When actions are dispatched, you get something similar to this printed to the console.

    ┌───→ Action: IncrementAction @19:15:39.419
    ├─ Prev state ► State(innerState: ["Counter": CounterExample.Counter(value: 0)])
    ├─ Action     ► IncrementAction(incrementValue: 1)
    ├─ Next state ► State(innerState: ["Counter": CounterExample.Counter(value: 1)])
    └──────────────────────────────────────────
    
    See more

    Declaration

    Swift

    public struct LoggerMiddleware: Middleware
  • Create an AsyncAction inline by passing a block to the init Check AsyncAction for more info

    SeeAlso:

    Example

    Performing an async network request

    let action = BlockAsyncAction { getState, dispatch in
    
      // Read the current state from the Store
      getState()
    
      // First dispatch some action syncrhonously
      dispatch(SomeAction(...))
    
      let session = URLSession(configuration: .default)
      // perform a dataTask
      session.dataTask(with: urlRequest) { data, response, error in
    
        if let data = data {
          // Do something with the data
           dispatch(RequestSucceeded(data: data))
    
        } else if let error = error {
    
           // Error happenend
           dispatch(RequestFaile(data: data))
        }
      }
    }
    
    store.dispatch(action: MyURLAsyncAction())
    
    See more

    Declaration

    Swift

    public struct BlockAsyncAction: AsyncAction
  • Async Middleware handles actions of type AsyncAction

    AsyncAction are not dispatched to the reducer When AsyncMiddleware intercepts an AsyncAction it does the following:

    1. Call action.execute on that action
    2. the action execute is executed which receives the getState and dispatch function as its parameters
    3. the execute calls dispatch as many times as wanted, dispatching new actions (can also disptach new AsyncAction)

    SeeAlso:

    See more

    Declaration

    Swift

    public struct AsyncMiddleware: Middleware
  • Structure that represents the store state. The store state is kept as a Dictionary with String Keys and Any Values ([String: Any])

    For example, the state with two struct looks like:

    [
      "TodoItems": TodoItems(....),
      "AppSettings": AppSettings(....)
    ]
    
    See more

    Declaration

    Swift

    public struct State
  • Subscription structure that represents a listener subscription. When adding a listener you get a subscription back. You can use this subscription to remove the listener, notify about the current state or link the listener lifecycle with an object.

    See more

    Declaration

    Swift

    public struct Subscription<StateType>
  • Subscription structure that represents a listener subscription. When adding a listener you get a subscription back. You can use this subscription to remove the listener, notify about the current state or link the listener lifecycle with an object.

    See more

    Declaration

    Swift

    public struct ActionSubscription