Skip to content

Commit 9f45122

Browse files
author
Ville Saukkonen
committed
feat(store creator callback): provide callback for creating store
1 parent b7885cd commit 9f45122

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Or directly from unpkg
5555

5656
#### Initialization
5757

58+
There are three ways to instantiate ngRedux:
59+
60+
1. [createStoreWith](#createStoreWith)
61+
2. [provideStore](#provideStore)
62+
3. [createStore](#createStore)
63+
64+
##### createStoreWith
65+
5866
You can either pass a function or an object to `createStoreWith`.
5967

6068
With a function:
@@ -92,7 +100,9 @@ angular.module('app', [ngRedux])
92100
```
93101
In this example `reducer1` will be resolved using angular's DI after the config phase.
94102

95-
Alternatively, you can pass an already existing store to ngRedux using `provideStore`:
103+
##### provideStore
104+
105+
You can pass an already existing store to ngRedux using `provideStore`:
96106

97107
```JS
98108
import reducers from './reducers';
@@ -108,6 +118,33 @@ angular.module('app', [ngRedux])
108118
});
109119
```
110120

121+
##### createStore
122+
123+
`createStore` allows you take full control over the store creation. This is handy
124+
if you want to control the order of enhancers by your self. It takes a function
125+
that gets middlewares and enhancers from ngRedux as a parameters. Note that
126+
middlewares provided by ngRedux needs to be last ones.
127+
128+
```JS
129+
import reducers from './reducers';
130+
import { createStore, combineReducers } from 'redux';
131+
import thunk from 'redux-thunk';
132+
import ngRedux from 'ng-redux';
133+
134+
const reducer = combineReducers(reducers);
135+
136+
angular.module('app', [ngRedux])
137+
.config(($ngReduxProvider) => {
138+
$ngReduxProvider.createStore((middlewares, enhancers) => {
139+
return createStore(
140+
reducer,
141+
{},
142+
compose(applyMiddleware(thunk, ...middlewares), ...enhancers)
143+
)
144+
});
145+
});
146+
```
147+
111148
#### Usage
112149

113150
*Using controllerAs syntax*

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ declare namespace ngRedux {
109109
/* provider */
110110

111111
export interface INgReduxProvider {
112+
/**
113+
* callback for creating Redux store.
114+
*
115+
* @param storeCreator
116+
*/
117+
createStore<S = any>(storeCreator: (middlewares?: (Middleware | string)[], storeEnhancers?: Function[]) => Store<S>): void;
112118
/**
113119
* Creates Redux store.
114120
*

src/components/ngRedux.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function ngReduxProvider() {
2323
let _initialState = undefined;
2424
let _reducerIsObject = undefined;
2525
let _providedStore = undefined;
26+
let _storeCreator = undefined;
2627

2728
this.provideStore = (store, middlewares = [], storeEnhancers) => {
2829
_providedStore = store;
@@ -31,6 +32,10 @@ export default function ngReduxProvider() {
3132
_middlewares = [...middlewares, providedStoreMiddleware(store)];
3233
}
3334

35+
this.createStore = (storeCreator) => {
36+
_storeCreator = storeCreator
37+
};
38+
3439
this.createStoreWith = (reducer, middlewares, storeEnhancers, initialState) => {
3540
invariant(
3641
isFunction(reducer) || isObject(reducer),
@@ -90,11 +95,15 @@ export default function ngReduxProvider() {
9095
// digestMiddleware needs to be the last one.
9196
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope'), this.config.debounce));
9297

93-
// combine middleware into a store enhancer.
94-
const middlewares = applyMiddleware(...resolvedMiddleware);
95-
96-
// compose enhancers with middleware and create store.
97-
const store = createStore(_reducer, _initialState, compose(middlewares, ...resolvedStoreEnhancer));
98+
let store;
99+
if(_storeCreator) {
100+
store = _storeCreator(resolvedMiddleware, resolvedStoreEnhancer);
101+
} else {
102+
// combine middleware into a store enhancer.
103+
const middlewares = applyMiddleware(...resolvedMiddleware);
104+
// compose enhancers with middleware and create store.
105+
store = createStore(_reducer, _initialState, compose(middlewares, ...resolvedStoreEnhancer));
106+
}
98107

99108
const mergedStore = assign({}, store, { connect: Connector(store) });
100109

0 commit comments

Comments
 (0)