Class Store<TStoreState>
A store holds the whole state tree of your application. The only way to change the state inside it is to dispatch an action on it. Your application should only have a single store in a Redux app. As your app grows, instead of adding stores, you split the root reducer into smaller reducers independently operating on the different parts of the state tree.
The store has the following responsibilities:
- Holds application state
- Allows access to state via Get
- Allows state to be updated via Dispatch(IAction)
- Registers listeners via Subscribe<TSelected>(Selector<TStore
- Handles unregistering of listeners via the function returned by Subscribe<TSelected>(Selector<TStore
Here are some important principles you should understand about Reducers:
- Reducers are the only way to update the state.
- Reducers are pure functions that take the previous state and an action, and return the next state.
- Reducers must be pure functions. They should not mutate the state, perform side effects like API calls or routing transitions, or call non-pure functions.
- Reducers must not do asynchronous logic.
- Reducers must not call other Reducer<TState>.
- Reducers must not call Subscribe<TSelected>(Selector<TStore
- Reducers must not call Get
- Reducers must not call Dispatch(IAction)
Inherited Members
Namespace: Unity.AppUI.Redux
Assembly: Unity.AppUI.Redux.dll
Syntax
[Preserve]
public class Store<TStoreState> : IStore<TStoreState>, IStore, IDispatchable, IDisposable, IStateProvider<TStoreState>, INotifiable<TStoreState>
Type Parameters
Name | Description |
---|---|
TStoreState | The type of the store state. |
Constructors
Store(Reducer<TStoreState>, TStoreState)
Creates a Redux store that holds the complete state tree of your app.
Declaration
[Preserve]
public Store(Reducer<TStoreState> reducer, TStoreState initialState)
Parameters
Type | Name | Description |
---|---|---|
Reducer<TStoreState> | reducer | The root reducer that returns the next state tree. |
TStore |
initialState | The initial state of the store. |
Remarks
You should not use directly this constructor to create a store.
Instead, use Store.CreateStore
to create a store with optional enhancers.
This constructor should be called only by enhancers to return an enhanced store.
Fields
m_Reducer
The reducer of the store.
Declaration
protected readonly Reducer<TStoreState> m_Reducer
Field Value
Type | Description |
---|---|
Reducer<TStoreState> |
m_State
The state of the store.
Declaration
protected TStoreState m_State
Field Value
Type | Description |
---|---|
TStore |
m_SubscribeLock
The subscribe lock of the store.
Declaration
protected readonly object m_SubscribeLock
Field Value
Type | Description |
---|---|
object |
m_Subscriptions
The subscriptions of the store.
Declaration
protected readonly List<ISubscription<TStoreState>> m_Subscriptions
Field Value
Type | Description |
---|---|
List<ISubscription<TStoreState>> |
Properties
dispatcher
The dispatcher of the store.
Declaration
public Dispatcher dispatcher { get; set; }
Property Value
Type | Description |
---|---|
Dispatcher |
reducer
The reducer of the store.
Declaration
public Reducer<TStoreState> reducer { get; }
Property Value
Type | Description |
---|---|
Reducer<TStoreState> |
Methods
Dispatch(IAction)
Dispatches an action. This is the only way to trigger a state change.
Declaration
public void Dispatch(IAction action)
Parameters
Type | Name | Description |
---|---|---|
IAction | action | An object describing the change that makes up the action. |
Exceptions
Type | Condition |
---|---|
Argument |
Thrown if the reducer for the action type does not exist. |
Argument |
Thrown if the action is null. |
Dispose()
Declaration
public void Dispose()
GetState()
Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.
Declaration
public TStoreState GetState()
Returns
Type | Description |
---|---|
TStore |
The current state tree of your application. |
NotifySubscribers()
Notify the subscribers of a new state.
Declaration
public void NotifySubscribers()
Subscribe<TSelected>(Selector<TStoreState, TSelected>, Listener<TSelected>, SubscribeOptions<TSelected>)
Subscribe to a state change and listen to a specific part of the state.
Declaration
public IDisposableSubscription Subscribe<TSelected>(Selector<TStoreState, TSelected> selector, Listener<TSelected> listener, SubscribeOptions<TSelected> options = default)
Parameters
Type | Name | Description |
---|---|---|
Selector<TStoreState, TSelected> | selector | The selector to get the selected part of the state. |
Listener<TSelected> | listener | The listener to be invoked on every dispatch. |
Subscribe |
options | The options for the subscription. |
Returns
Type | Description |
---|---|
IDisposable |
A Subscription object that can be disposed. |
Type Parameters
Name | Description |
---|---|
TSelected | The type of the selected part of the state. |
Exceptions
Type | Condition |
---|---|
Argument |
Thrown if the selector or listener is null. |