forked from reduxjs/redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.js
93 lines (85 loc) · 1.75 KB
/
reducers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
ADD_TODO,
DISPATCH_IN_MIDDLE,
GET_STATE_IN_MIDDLE,
SUBSCRIBE_IN_MIDDLE,
UNSUBSCRIBE_IN_MIDDLE,
THROW_ERROR
} from './actionTypes'
function id(state = []) {
return (
state.reduce((result, item) => (item.id > result ? item.id : result), 0) + 1
)
}
export function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [
...state,
{
id: id(state),
text: action.text
}
]
default:
return state
}
}
export function todosReverse(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [
{
id: id(state),
text: action.text
},
...state
]
default:
return state
}
}
export function dispatchInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case DISPATCH_IN_MIDDLE:
action.boundDispatchFn()
return state
default:
return state
}
}
export function getStateInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case GET_STATE_IN_MIDDLE:
action.boundGetStateFn()
return state
default:
return state
}
}
export function subscribeInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case SUBSCRIBE_IN_MIDDLE:
action.boundSubscribeFn()
return state
default:
return state
}
}
export function unsubscribeInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case UNSUBSCRIBE_IN_MIDDLE:
action.boundUnsubscribeFn()
return state
default:
return state
}
}
export function errorThrowingReducer(state = [], action) {
switch (action.type) {
case THROW_ERROR:
throw new Error()
default:
return state
}
}