Skip to content

feat(lib): provide store functionality (resubmitted) #187

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 3 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
15 changes: 14 additions & 1 deletion src/components/ngRedux.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Connector from './connector';
import invariant from 'invariant';
import {createStore, applyMiddleware, compose, combineReducers} from 'redux';
import digestMiddleware from './digestMiddleware';
import providedStoreMiddleware from './providedStoreMiddleware';
import wrapStore from './storeWrapper';

import curry from 'lodash.curry';
import isFunction from 'lodash.isfunction';
Expand All @@ -20,6 +22,13 @@ export default function ngReduxProvider() {
let _storeEnhancers = undefined;
let _initialState = undefined;
let _reducerIsObject = undefined;
let _providedStore = undefined;

this.provideStore = (store) => {
_providedStore = store;
_reducer = (state, action) => action.payload;
_middlewares = [providedStoreMiddleware(_providedStore)];
}

this.createStoreWith = (reducer, middlewares, storeEnhancers, initialState) => {
invariant(
Expand Down Expand Up @@ -79,7 +88,11 @@ export default function ngReduxProvider() {
? applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer, _initialState)
: applyMiddleware(...resolvedMiddleware)(finalCreateStore)(_reducer);

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

if (_providedStore) wrapStore(_providedStore, mergedStore)

return mergedStore;
};

this.$get.$inject = ['$injector'];
Expand Down
11 changes: 11 additions & 0 deletions src/components/providedStoreMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* middleware for the empty store that ng-redux uses when a external store is provided
* Provides two cases:
* 1. NGREDUX_PASSTHROUGH, where data is coming IN to the "fake" store
* 2. all other, where actions are dispatched out, and proxied to the true store
*/
export default _providedStore => store => next => action => {
return action.type === '@@NGREDUX_PASSTHROUGH'
? next(action)
: _providedStore.dispatch(action)
}
11 changes: 11 additions & 0 deletions src/components/storeWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function wrapStore(providedStore, ngReduxStore) {
const unsubscribe = providedStore
.subscribe(() => {
let newState = providedStore.getState();
ngReduxStore.dispatch({
type: '@@NGREDUX_PASSTHROUGH',
payload: newState
});
})
;
}