-
Notifications
You must be signed in to change notification settings - Fork 15
/
RootDuck.ts
85 lines (83 loc) · 1.86 KB
/
RootDuck.ts
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
import { DuckMap } from "../../src";
import { takeEvery, call, put, select } from "redux-saga/effects";
import CounterDuck from "./CounterDuck";
class CounterStep2Duck extends CounterDuck {
get step() {
return 2;
}
}
class CounterStep3Duck extends CounterDuck {
get step() {
return 3;
}
}
enum Types {
"INCREMENT",
"CHILD_INCREMENT"
}
export default class MyRootDuck extends DuckMap {
get quickTypes() {
return {
...super.quickTypes,
...Types
};
}
get reducers() {
const { types } = this;
return {
...super.reducers,
total: (state = 0, action) => {
switch (action.type) {
case types.CHILD_INCREMENT:
return state + 1;
default:
return state;
}
}
};
}
get rawSelectors() {
return {
...super.rawSelectors,
total: state => state.total
};
}
get creators() {
return {
...super.creators,
increment: () => ({ type: this.types.INCREMENT })
};
}
get quickDucks() {
return {
...super.quickDucks,
counter1: CounterDuck,
counter2: CounterStep2Duck,
counter3: CounterStep3Duck
};
}
*saga() {
yield* super.saga();
const {
types,
ducks: { counter1, counter2, counter3 }
} = this;
// Increment all counters
yield takeEvery(types.INCREMENT, function*() {
yield put(counter1.creators.increment());
yield put(counter2.creators.increment());
yield put(counter3.creators.increment());
});
// Count child counters increments
yield takeEvery(
[
counter1.types.INCREMENT,
counter2.types.INCREMENT,
counter3.types.INCREMENT
],
function*() {
yield put({ type: types.CHILD_INCREMENT });
}
);
}
}