Skip to content

feat(store creator callback): provide callback for creating store #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ Or directly from unpkg

#### Initialization

There are three ways to instantiate ngRedux:

1. [createStoreWith](#createStoreWith)
2. [provideStore](#provideStore)
3. [createStore](#createStore)

##### createStoreWith

You can either pass a function or an object to `createStoreWith`.

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

Alternatively, you can pass an already existing store to ngRedux using `provideStore`:
##### provideStore

You can pass an already existing store to ngRedux using `provideStore`:

```JS
import reducers from './reducers';
Expand All @@ -108,6 +118,33 @@ angular.module('app', [ngRedux])
});
```

##### createStore

`createStore` allows you take full control over the store creation. This is handy
if you want to control the order of enhancers by your self. It takes a function
that gets middlewares and enhancers from ngRedux as a parameters. Note that
middlewares provided by ngRedux needs to be last ones.

```JS
import reducers from './reducers';
import { createStore, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import ngRedux from 'ng-redux';

const reducer = combineReducers(reducers);

angular.module('app', [ngRedux])
.config(($ngReduxProvider) => {
$ngReduxProvider.createStore((middlewares, enhancers) => {
return createStore(
reducer,
{},
compose(applyMiddleware(thunk, ...middlewares), ...enhancers)
)
});
});
```

#### Usage

*Using controllerAs syntax*
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ declare namespace ngRedux {
/* provider */

export interface INgReduxProvider {
/**
* callback for creating Redux store.
*
* @param storeCreator
*/
createStore<S = any>(storeCreator: (middlewares?: (Middleware | string)[], storeEnhancers?: Function[]) => Store<S>): void;
/**
* Creates Redux store.
*
Expand Down
19 changes: 14 additions & 5 deletions src/components/ngRedux.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function ngReduxProvider() {
let _initialState = undefined;
let _reducerIsObject = undefined;
let _providedStore = undefined;
let _storeCreator = undefined;

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

this.createStore = (storeCreator) => {
_storeCreator = storeCreator
};

this.createStoreWith = (reducer, middlewares, storeEnhancers, initialState) => {
invariant(
isFunction(reducer) || isObject(reducer),
Expand Down Expand Up @@ -90,11 +95,15 @@ export default function ngReduxProvider() {
// digestMiddleware needs to be the last one.
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope'), this.config.debounce));

// combine middleware into a store enhancer.
const middlewares = applyMiddleware(...resolvedMiddleware);

// compose enhancers with middleware and create store.
const store = createStore(_reducer, _initialState, compose(middlewares, ...resolvedStoreEnhancer));
let store;
if(_storeCreator) {
store = _storeCreator(resolvedMiddleware, resolvedStoreEnhancer);
} else {
// combine middleware into a store enhancer.
const middlewares = applyMiddleware(...resolvedMiddleware);
// compose enhancers with middleware and create store.
store = createStore(_reducer, _initialState, compose(middlewares, ...resolvedStoreEnhancer));
}

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

Expand Down