Store

public protocol Store

Defines the interface of Stores in Swift Flow. MainStore is the default implementation of this interaface. Applications have a single store that stores the entire application state. Stores receive actions and use reducers combined with these actions, to calculate state changes. Upon every state update a store informs all of its subscribers.

  • Initializes the store with a reducer and an intial state.

    Declaration

    Swift

    init(reducer: AnyReducer, appState: StateType)
  • Initializes the store with a reducer, an initial state and a list of middleware. Middleware is applied in the order in which it is passed into this constructor.

    Declaration

    Swift

    init(reducer: AnyReducer, appState: StateType, middleware: [Middleware])
  • The current state stored in the store.

    Declaration

    Swift

    var appState: StateType { get }
  • The main dispatch function that is used by all convenience dispatch methods. This dispatch function can be extended by providing middlewares.

    Declaration

    Swift

    var dispatchFunction: DispatchFunction! { get }
  • Subscribes the provided subscriber to this store. Subscribers will receive a call to newState whenever the state in this store changes.

    Declaration

    Swift

    func subscribe(subscriber: AnyStoreSubscriber)

    Parameters

    subscriber

    Subscriber that will receive store updates

  • Unsubscribes the provided subscriber. The subscriber will no longer receive state updates from this store.

    Declaration

    Swift

    func unsubscribe(subscriber: AnyStoreSubscriber)

    Parameters

    subscriber

    Subscriber that will be unsubscribed

  • Dispatches an action. This is the simplest way to modify the stores state.

    Example of dispatching an action:

    store.dispatch( CounterAction.IncreaseCounter )
    

    Declaration

    Swift

    func dispatch(action: Action) -> Any

    Parameters

    action

    The action that is being dispatched to the store

    Return Value

    By default returns the dispatched action, but middlewares can change the return type, e.g. to return promises

  • Dispatches an action creator to the store. Action creators are functions that generate actions. They are called by the store and receive the current state of the application and a reference to the store as their input.

    Based on that input the action creator can either return an action or not. Alternatively the action creator can also perform an asynchronous operation and dispatch a new action at the end of it.

    Example of an action creator:

    func deleteNote(noteID: Int) -> ActionCreator {
       return { state, store in
           // only delete note if editing is enabled
           if (state.editingEnabled == true) {
               return NoteDataAction.DeleteNote(noteID)
           } else {
               return nil
           }
       }
    }
    

    This action creator can then be dispatched as following:

    store.dispatch( noteActionCreatore.deleteNote(3) )
    

    Declaration

    Swift

    func dispatch(actionCreator: ActionCreator) -> Any

    Return Value

    By default returns the dispatched action, but middlewares can change the return type, e.g. to return promises

  • Dispatches an async action creator to the store. An async action creator generates an action creator asynchronously.

    Declaration

    Swift

    func dispatch(asyncActionCreator: AsyncActionCreator)
  • Dispatches an action and calls the callback as soon as the action has been processed. You will receive the updated store state as part of this callback.

    Example of dispatching an action and implementing a callback:

    store.dispatch( CounterAction.IncreaseCounter ) { state in
       print("New state: \(state)")
    }
    

    Declaration

    Swift

    func dispatch(action: Action, callback: DispatchCallback?) -> Any

    Parameters

    action

    The action that is being dispatched to the store

    Return Value

    By default returns the dispatched action, but middlewares can change the return type, e.g. to return promises

  • Dispatches an action creator to the store. Action creators are functions that generate actions. They are called by the store and receive the current state of the application and a reference to the store as their input.

    Based on that input the action creator can either return an action or not. Alternatively the action creator can also perform an asynchronous operation and dispatch a new action at the end of it.

    Example of an action creator:

    func deleteNote(noteID: Int) -> ActionCreator {
       return { state, store in
           // only delete note if editing is enabled
           if (state.editingEnabled == true) {
               return NoteDataAction.DeleteNote(noteID)
           } else {
               return nil
           }
       }
    }
    

    This action creator can then be dispatched as following:

    store.dispatch( noteActionCreatore.deleteNote(3) )
    

    This overloaded version of dispatch will call the provided callback as soon as a new state has been calculated based on the dispatch action.

    Note

    If the ActionCreator does not dispatch an action, the callback block will never be called

    Declaration

    Swift

    func dispatch(actionCreator: ActionCreator, callback: DispatchCallback?) -> Any

    Return Value

    By default returns the dispatched action, but middlewares can change the return type, e.g. to return promises

  • Dispatches an async action creator to the store. An async action creator generates an action creator asynchronously. Use this method if you want to wait for the state change triggered by the asynchronously generated action creator.

    This overloaded version of dispatch calls the provided callback as soon as the asynchronoously dispatched action has caused a new state calculation.

    Note

    If the ActionCreator does not dispatch an action, the callback block will never be called

    Declaration

    Swift

    func dispatch(asyncActionCreator: AsyncActionCreator, callback: DispatchCallback?)