Skip to content

Commit ecccf1b

Browse files
committed
feat(handlers): add posibility to set a string instead of a handler
1 parent 4584ac0 commit ecccf1b

6 files changed

+2983
-32
lines changed

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,28 @@ import { build } from 'encaps';
2323

2424
export const { actions, reducer } = build()
2525
.initState(() => ({counter: 10}))
26+
/**
27+
* Functions to handle actions
28+
* A reducer will consist of them
29+
*/
2630
.handlers({
2731
increment: (state, action) => ({...state, counter: state.counter + action.payload}),
2832
decrement: (state, action) => ({...state, counter: state.counter - action.payload}),
33+
/**
34+
* a string value is a shortcut for
35+
* (state, action) => ({...state, counter: action.payload}),
36+
*/
37+
setCounter: 'counter'
2938
});
3039

3140
const initState = reducer(); // {counter: 10}
3241

3342
actions.increment(1); // {type: 'increment', payload: 1}
3443
actions.decrement(2); // {type: 'decrement', payload: 2}
44+
actions.setCounter(3); // {type: 'setCounter', payload: 3}
3545

3646
reducer(initState, actions.decrement(2)); // {counter: 8}
47+
reducer(initState, actions.setCounter(3)); // {counter: 3}
3748
```
3849

3950
### Reducer extension
@@ -268,8 +279,11 @@ interface Builder {
268279
* @returns new Builder
269280
*/
270281
handlers(
271-
/** map of action handlers */
272-
handlers: {[K: string]: (state, action: Action) => object}
282+
/**
283+
* Map of action handlers
284+
* You can set a model field name instead of a function to create a function which changes this field of the model
285+
*/
286+
handlers: {[K: string]: ((state, action: Action) => object) | string}
273287
): Builder;
274288

275289
/**

0 commit comments

Comments
 (0)