1 | import {
|
2 | createStore,
|
3 | applyMiddleware,
|
4 | } from 'redux';
|
5 | import makePropTypesDirective from './directive-proptypes';
|
6 | import thunk from 'redux-thunk';
|
7 |
|
8 | const ActionTypes = {
|
9 | INIT: '@@redux/INIT',
|
10 | };
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function angularSetup(app) {
|
17 | |
18 |
|
19 |
|
20 | app.provider('ngStore', function () {
|
21 | let state = {};
|
22 | let currentUpdater = currentState => currentState;
|
23 | let dispatchLayers = [thunk];
|
24 |
|
25 |
|
26 | this.setInitialState = initialState => {
|
27 | state = initialState;
|
28 | };
|
29 |
|
30 | this.setReducers = this.putUpdaters = updater => {
|
31 | currentUpdater = updater;
|
32 | };
|
33 |
|
34 | this.putLayers = layers => {
|
35 | dispatchLayers = layers;
|
36 | };
|
37 |
|
38 | this.$get = ['$timeout',
|
39 | function ($timeout) {
|
40 | function digestAngularUI() {
|
41 | return (nextLayer) => (action) => {
|
42 | let result;
|
43 | try {
|
44 | result = nextLayer(action);
|
45 | } catch (e) {
|
46 | console.error('DISPATCH ERROR >>> ', e);
|
47 | }
|
48 |
|
49 |
|
50 | $timeout(() => result);
|
51 | return result;
|
52 | };
|
53 | }
|
54 |
|
55 | dispatchLayers.unshift(digestAngularUI);
|
56 |
|
57 | const finalStore = createStore(currentUpdater, state, applyMiddleware(...dispatchLayers));
|
58 |
|
59 | return finalStore;
|
60 | }];
|
61 | });
|
62 |
|
63 |
|
64 | app.run(['ngStore', (store) => {
|
65 | store.dispatch({
|
66 | type: ActionTypes.INIT,
|
67 | });
|
68 | }]);
|
69 |
|
70 | app.directive = makePropTypesDirective(app.directive.bind(app));
|
71 |
|
72 | return app;
|
73 | }
|
74 |
|
75 | export default angularSetup;
|