Skip to content

Commit

Permalink
refactor(redux): move common methods into redux common mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Apr 16, 2019
1 parent 900edc7 commit 52b5a7a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 48 deletions.
24 changes: 0 additions & 24 deletions packages/redux/mixin.browser.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
const React = require('react');
const { compose, combineReducers, createStore } = require('redux');
const { Provider } = require('react-redux');
const {
strategies: {
sync: { override },
},
} = require('hops-mixin');
const ReduxRuntimeCommonMixin = require('./mixin.runtime-common');

const ReduxCompose = global.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

class ReduxMixin extends ReduxRuntimeCommonMixin {
constructor(config, element, { redux: options = {} } = {}) {
super(config);
this.reducers = options.reducers || {};
}

createStore() {
return createStore(
combineReducers(this.reducers),
Expand All @@ -42,18 +30,6 @@ class ReduxMixin extends ReduxRuntimeCommonMixin {
composeEnhancers(...enhancers) {
return ReduxCompose(...enhancers);
}

enhanceElement(reactElement) {
return React.createElement(
Provider,
{ store: this.getReduxStore() },
reactElement
);
}
}

ReduxMixin.strategies = {
getReduxStore: override,
};

module.exports = ReduxMixin;
12 changes: 12 additions & 0 deletions packages/redux/mixin.runtime-common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const React = require('react');
const { Provider } = require('react-redux');
const { applyMiddleware } = require('redux');
const ReduxThunkMiddleware = require('redux-thunk').default;
const {
Expand All @@ -11,6 +13,7 @@ class ReduxRuntimeCommonMixin extends Mixin {
constructor(config, element, { redux: options = {} } = {}) {
super(config);
this.middlewares = options.middlewares || [];
this.reducers = options.reducers || {};
}

getReduxMiddlewares() {
Expand All @@ -27,10 +30,19 @@ class ReduxRuntimeCommonMixin extends Mixin {

return middlewares.map(m => applyMiddleware(m));
}

enhanceElement(reactElement) {
return React.createElement(
Provider,
{ store: this.getReduxStore() },
reactElement
);
}
}

ReduxRuntimeCommonMixin.strategies = {
getReduxMiddlewares: override,
getReduxStore: override,
};

module.exports = ReduxRuntimeCommonMixin;
24 changes: 0 additions & 24 deletions packages/redux/mixin.server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
const React = require('react');
const { createStore, combineReducers, compose } = require('redux');
const { Provider } = require('react-redux');
const {
strategies: {
sync: { override },
},
} = require('hops-mixin');
const ReduxRuntimeCommonMixin = require('./mixin.runtime-common');

class ReduxMixin extends ReduxRuntimeCommonMixin {
constructor(config, element, { redux: options = {} } = {}) {
super(config);
this.reducers = options.reducers || {};
}

createStore() {
return createStore(
combineReducers(this.reducers),
Expand Down Expand Up @@ -44,18 +32,6 @@ class ReduxMixin extends ReduxRuntimeCommonMixin {
},
};
}

enhanceElement(reactElement) {
return React.createElement(
Provider,
{ store: this.getReduxStore() },
reactElement
);
}
}

ReduxMixin.strategies = {
getReduxStore: override,
};

module.exports = ReduxMixin;

0 comments on commit 52b5a7a

Please sign in to comment.