This package contains tools for integrating reSolve with Redux .
Redux middleware used to :
- Automatically fetch a view model state and subscribe to events.
- Send a command to the server side.
Generates a standard Redux reducer using reSolve view models. It does not take any arguments as it receives required data from resolve middleware automatically.
This reducer includes handling the reSolve's merge
action.
A higher-order component (HOC), which automatically subscribes/unsubscribes to/from a view model by aggregateId.
const mapStateToProps = state => ({
...state[viewModel][aggregateId],
viewModel, // required field
aggregateId // required field
});
export default connect(mapStateToProps)(withViewModel(Component));
Generates Redux actions using a reSolve aggregate. This function uses the reSolve's sendCommand
action to pass a command from Redux to the server side. Generated actions are named as an aggregate's commands. This function takes two arguments:
-
aggregate
- reSolve aggregate -
extendActions
- actions to extend or redefine resulting actions
A plain object used to send special actions to be automatically handled by resolveMiddleware
. It implements the following functions.
-
Sends a command to the server side. It takes the object with the following required arguments:
command
aggregateId
aggregateName
payload
-
Subscribes to new server-side events. This function takes two arguments:
-
eventTypes
- an array of event types -
aggregateId
- an aggregate id
-
-
Unsubscribes from provided server-side events. This function takes two arguments:
-
eventTypes
- an array of event types -
aggregateId
- an aggregate id
-
-
Produces an action handled by a reducer which the
createViewModelsReducer
function generates. A view model state is replaced with a new state . It takes two arguments:-
viewModel
- the name of a read model whose state should be updated -
aggregateId
- an aggregate id -
state
- the state to be merged with the specified read model's existing state
-
import { createStore, applyMiddleware } from 'redux';
import { resolveMiddleware } from 'resolve-redux';
import reducer from '../reducers';
import viewModels from '../../common/view-models';
const middleware = [resolveMiddleware(viewModels)];
export default initialState => createStore(reducer, initialState, applyMiddleware(...middleware));
import { createActions } from 'resolve-redux'
import { connect, bindActionCreators } from 'redux'
import App from './components/App'
export const aggregate {
name: 'User',
commands: {
createUser: (state, { aggregateId, payload }) => ({
type: 'UserCreated',
aggregateId,
payload
})
}
}
function mapDispatchToProps(dispatch) {
actions: bindActionCreators(createActions(aggregate))
}
export default connect(() => {}, mapDispatchToProps)(App)
import { actions } from 'resolve-redux';
export function sendCommandAddTodoItem(aggregateId) {
return {
type: 'SEND_COMMAND_ADD_TODO_ITEM',
aggregateId,
aggregateName: 'TodoList',
payload: { name: 'todo-list' },
command: {
type: 'TodoListItemAdd'
}
};
}
store.dispatch(sendCommandAddTodoItem('aggregateId'));
or
store.dispatch(actions.sendCommand({
aggregateId: 'aggregateId',
aggregateName: 'TodoList',
payload: { name: 'todo-list' },
command: {
type: 'TodoListItemRemove'
}
}));