2 min read

Redux

Last Updated - Platform 22.0 - SDK 17.0

Redux is a unidirection architecture where a view is updated only by the state. We use, but wrap, ReSwift with our own set of simplified structures to make it fit more naturally within the SDK.

Overview

Most feature's start by dispatching an initial action on viewDidLoad to fetch their data.

Every action is passed to the first middleware, which may handle the action and continue the action to the next middleware. Continuing, passes the action to the next middleware, and eventually to the reducer(s).

Once the last middleware continues the action the reducers create and return a new state based on the previous state. Each reducer can decide how to update the previous state based on the action and eventually return that state. States usually wrap the domain objects for the feature with loading or error properties.

Finally, the reduced state is passed to the owning view or view controller in the newState function. That view or view controller then updates it's views by mapping the state to view data and passing it to them.

Once the views are updated the cycle is complete until another action is dispatched (usually as the result of user interaction).

Store

The Redux Store wraps all the elements together and contains the current state. Views or ViewControllers can subscribe to the store to receive updates when the state changes.

State

A feature's state wraps the domain objects used by the feature as well as stateful flags for those domain objects.

The SDK provides DataState objects to wrap asynchronously fetched domain objects with loading and error information. There are further conveniences provided for mapping or reading from these objects.

Reducers

Reducers are required Redux elements that react to each action by creating and returning a new state to the feature. Use them to update the domain models within the state.

Middleware

Middleware are optional Redux elements that may observe, react, or handle each action. They do not need to be tied to a specific state allowing for generic middleware such as all SDK service middleware.

The SDK commonly has middleware per feature for analytics, navigation, translating interactions to actions, or delegating back to other features. Middleware are extremely versatile and can be used in many ways.

Actions

Redux actions are events (which may contain relevant data) that cause state updates. Often the owning view controller or view dispatches an action to their Redux store which gets observed and handled by middleware before being reduced and updating the UI.

The SDK commonly uses Swift enum cases containing relevant objects but as Action is a protocol anything can be used. To make actions more findable the SDK uses enum namespacing.