State
The Trustlines app uses redux in combination with redux-saga to store data locally. Once data is pulled in our state, it's automatically persisted in AsyncStorage with redux-persist.
Every state entity has ist own directory in 'state' that usually containing these files:
E.g. 'state/contacts'
#
EntitiesA state entity that has an external resource as source, e.g. 'contact' which is stored in an internal db, follows a pattern:
#
State Object#
Actions (actions.js)Small functions creating actions to enable a streamlined code flow.
#
Sagas (sagas.js)Sagas take care of the data retrieval management, data normalization and enable side effects in redux.
see github.com/redux-saga/redux-saga or redux-saga.js.org to get a feeling of what redux-saga is capable of.
In the example above the saga takes care of the async call. Therefor in response to a *.requested action ( takeEvery() ) the actual data retrieval method is called ( call() ), the result is normalized and a *.succeeded action is dispatched that will trigger the reducer to save the result to the state.
As a bonus the dispatch call itself can return a promise, that is resolved once the *.succeeded action is dispatched. To enable this redux-saga-thunk is used.
#
NormalizationThe states data is normalized with 'normalizr' https://github.com/paularmstrong/normalizr to reduce redundancy.
#
Reducer (reducer.js)The reducer transfer new data into the state
#
Select (select.js)To get ( and denormalize ) data from state, use the methods from the corresponding select.js
#
PersistenceParts of the state are persistet via redux-persist to enable a better startup performance of the app.
There is also a locally running instance of Pouch DB which takes care of data saved only locally e.g. contacts
and account
data . The connection to this local database is treated like am async remote connection to enable other data adapters later.