-
Notifications
You must be signed in to change notification settings - Fork 177
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
feat(lib): provide store functionality #185
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import Connector from './connector'; | |
import invariant from 'invariant'; | ||
import {createStore, applyMiddleware, compose, combineReducers} from 'redux'; | ||
import digestMiddleware from './digestMiddleware'; | ||
import wrapStore from './storeWrapper'; | ||
|
||
import curry from 'lodash.curry'; | ||
import isFunction from 'lodash.isfunction'; | ||
|
@@ -20,6 +21,11 @@ export default function ngReduxProvider() { | |
let _storeEnhancers = undefined; | ||
let _initialState = undefined; | ||
let _reducerIsObject = undefined; | ||
let _providedStore = undefined; | ||
|
||
this.provideStore = (store) => { | ||
_providedStore = store; | ||
} | ||
|
||
this.createStoreWith = (reducer, middlewares, storeEnhancers, initialState) => { | ||
invariant( | ||
|
@@ -42,45 +48,56 @@ export default function ngReduxProvider() { | |
}; | ||
|
||
this.$get = ($injector) => { | ||
const resolveMiddleware = middleware => isString(middleware) | ||
? $injector.get(middleware) | ||
: middleware; | ||
if (_providedStore) { | ||
const emptyStore = createNgReduxStore($injector, [], [], state => state, undefined); | ||
|
||
const resolvedMiddleware = map(_middlewares, resolveMiddleware); | ||
return wrapStore(_providedStore, emptyStore); | ||
} | ||
|
||
const resolveStoreEnhancer = storeEnhancer => isString(storeEnhancer) | ||
? $injector.get(storeEnhancer) | ||
: storeEnhancer; | ||
return createNgReduxStore($injector, _middlewares, _storeEnhancers, _reducer, _initialState); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the diff looks a bit weird but I basically moved the |
||
|
||
const resolvedStoreEnhancer = map(_storeEnhancers, resolveStoreEnhancer); | ||
}; | ||
|
||
if(_reducerIsObject) { | ||
const getReducerKey = key => isString(_reducer[key]) | ||
? $injector.get(_reducer[key]) | ||
: _reducer[key]; | ||
this.$get.$inject = ['$injector']; | ||
} | ||
|
||
const resolveReducerKey = (result, key) => assign({}, result, | ||
{ [key]: getReducerKey(key) } | ||
); | ||
function createNgReduxStore($injector, _middlewares, _storeEnhancers, _reducer, _initialState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By moving this function out, i'm getting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I resolved it for myself by moving |
||
const resolveMiddleware = middleware => isString(middleware) | ||
? $injector.get(middleware) | ||
: middleware; | ||
|
||
const reducersObj = Object | ||
.keys(_reducer) | ||
.reduce(resolveReducerKey, {}); | ||
const resolvedMiddleware = map(_middlewares, resolveMiddleware); | ||
|
||
_reducer = combineReducers(reducersObj); | ||
} | ||
const resolveStoreEnhancer = storeEnhancer => isString(storeEnhancer) | ||
? $injector.get(storeEnhancer) | ||
: storeEnhancer; | ||
|
||
const finalCreateStore = resolvedStoreEnhancer ? compose(...resolvedStoreEnhancer)(createStore) : createStore; | ||
const resolvedStoreEnhancer = map(_storeEnhancers, resolveStoreEnhancer); | ||
|
||
//digestMiddleware needs to be the last one. | ||
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope'))); | ||
if(_reducerIsObject) { | ||
const getReducerKey = key => isString(_reducer[key]) | ||
? $injector.get(_reducer[key]) | ||
: _reducer[key]; | ||
|
||
const store = _initialState | ||
? applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer, _initialState) | ||
: applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer); | ||
const resolveReducerKey = (result, key) => assign({}, result, | ||
{ [key]: getReducerKey(key) } | ||
); | ||
|
||
return assign({}, store, { connect: Connector(store) }); | ||
}; | ||
const reducersObj = Object | ||
.keys(_reducer) | ||
.reduce(resolveReducerKey, {}); | ||
|
||
this.$get.$inject = ['$injector']; | ||
_reducer = combineReducers(reducersObj); | ||
} | ||
|
||
const finalCreateStore = resolvedStoreEnhancer ? compose(...resolvedStoreEnhancer)(createStore) : createStore; | ||
|
||
//digestMiddleware needs to be the last one. | ||
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope'))); | ||
|
||
const store = _initialState | ||
? applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer, _initialState) | ||
: applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer); | ||
|
||
return assign({}, store, { connect: Connector(store) }); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default function wrapStore(providedStore, ngReduxStore) { | ||
const unsubscribe = providedStore | ||
.subscribe(() => { | ||
let newState = providedStore.getState(); | ||
|
||
ngReduxStore.dispatch(newState); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here's where the magic happens. The only thing I'm unsure about is cleanup. But as I understand it, Angular 1 never really "unloads" during runtime so this shouldn't be an issue. |
||
}) | ||
; | ||
|
||
return Object.assign({}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the intention to not provide |
||
providedStore, | ||
{ | ||
subscribe: ngReduxStore.subscribe | ||
}) | ||
; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pass through reducer is throwing errors for me. It's expecting it to be a well formed action object
{type: 'foobar'}
.Changed to
(state, action) => action.payload
and then changed line 6 of the store wrapper to just dispatch as:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! nice! I figured that might cause some issues.