1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const redux = require('redux');
|
9 |
|
10 |
|
11 | class Apemanstore {
|
12 | constructor(reducer, config) {
|
13 | let s = this;
|
14 | reducer = Apemanstore.combineReducers(reducer);
|
15 | let _reduxStore = redux.createStore(reducer);
|
16 | s.__proto__ = _reduxStore;
|
17 | Object.assign(s, {_reduxStore}, Apemanstore.methods, config);
|
18 | }
|
19 | }
|
20 |
|
21 | Object.assign(Apemanstore, {
|
22 | combineReducers(reducer) {
|
23 | if (Array.isArray(reducer)) {
|
24 | reducer = reducer.map(Apemanstore.combineReducers);
|
25 | return function combined(state, action) {
|
26 | return reducer.reduce(
|
27 | (state, reducer) => reducer(state, action),
|
28 | state
|
29 | );
|
30 | }
|
31 | }
|
32 | if (typeof reducer === 'object') {
|
33 | for (let name of Object.keys(reducer)) {
|
34 | reducer[name] = Apemanstore.combineReducers(reducer[name]);
|
35 | }
|
36 | reducer = redux.combineReducers(reducer);
|
37 | }
|
38 | return reducer;
|
39 | },
|
40 | methods: {
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | getState(name) {
|
47 | let s = this,
|
48 | state = s._reduxStore.getState();
|
49 | if (arguments.length === 0) {
|
50 | return state;
|
51 | }
|
52 | if (!state.hasOwnProperty(name)) {
|
53 | throw new Error(`Unknown state name ${name}`);
|
54 | }
|
55 | return state[name];
|
56 | },
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | dispatchAction(type, payload, meta, error){
|
66 | let s = this;
|
67 | payload = payload || null;
|
68 | meta = meta || {};
|
69 | error = error || false;
|
70 | return Promise.resolve(
|
71 | s.dispatch({type, payload, meta, error})
|
72 | );
|
73 | },
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | dispatchErrorAction(type, payload, meta){
|
82 | let s = this;
|
83 | return Promise.reject(
|
84 | s.dispatchAction(type, payload, meta, true)
|
85 | .then(action => Object.assign(new Error(), payload))
|
86 | );
|
87 | }
|
88 | }
|
89 | });
|
90 |
|
91 |
|
92 | module.exports = Apemanstore; |
\ | No newline at end of file |