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