Skip to content

Commit

Permalink
chore(redux): add common class for redux runtime mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
jhiode authored and ZauberNerd committed Apr 16, 2019
1 parent 158f204 commit 06c1ec5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 51 deletions.
28 changes: 3 additions & 25 deletions packages/redux/mixin.browser.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
const React = require('react');
const {
compose,
combineReducers,
createStore,
applyMiddleware,
} = require('redux');
const ReduxThunkMiddleware = require('redux-thunk').default;
const { compose, combineReducers, createStore } = require('redux');
const { Provider } = require('react-redux');
const {
Mixin,
strategies: {
sync: { override },
},
} = require('hops-mixin');
const ReduxRuntimeCommonMixin = require('./mixin.runtime-common');

const ReduxCompose = global.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

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

Expand All @@ -46,20 +39,6 @@ class ReduxMixin extends Mixin {
return this.store;
}

getReduxMiddlewares() {
return [
ReduxThunkMiddleware.withExtraArgument({
config: this.config,
}),
];
}

applyMiddlewares() {
const middlewares = this.middlewares || this.getReduxMiddlewares();

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

composeEnhancers(...enhancers) {
return ReduxCompose(...enhancers);
}
Expand All @@ -75,7 +54,6 @@ class ReduxMixin extends Mixin {

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

module.exports = ReduxMixin;
35 changes: 35 additions & 0 deletions packages/redux/mixin.runtime-common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { applyMiddleware } = require('redux');
const ReduxThunkMiddleware = require('redux-thunk').default;
const {
Mixin,
strategies: {
sync: { override },
},
} = require('hops-mixin');

class ReduxRuntimeCommonMixin extends Mixin {
constructor(config, element, { redux: options = {} } = {}) {
super(config);
this.middlewares = options.middlewares;
}

getReduxMiddlewares() {
return [
ReduxThunkMiddleware.withExtraArgument({
config: this.config,
}),
];
}

applyMiddlewares() {
const middlewares = this.middlewares || this.getReduxMiddlewares();

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

ReduxRuntimeCommonMixin.strategies = {
getReduxMiddlewares: override,
};

module.exports = ReduxRuntimeCommonMixin;
29 changes: 3 additions & 26 deletions packages/redux/mixin.server.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
const React = require('react');
const {
createStore,
combineReducers,
applyMiddleware,
compose,
} = require('redux');

const ReduxThunkMiddleware = require('redux-thunk').default;
const { createStore, combineReducers, compose } = require('redux');
const { Provider } = require('react-redux');
const {
Mixin,
strategies: {
sync: { override },
},
} = require('hops-mixin');
const ReduxRuntimeCommonMixin = require('./mixin.runtime-common');

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

Expand All @@ -39,20 +31,6 @@ class ReduxMixin extends Mixin {
return this.store;
}

getReduxMiddlewares() {
return [
ReduxThunkMiddleware.withExtraArgument({
config: this.config,
}),
];
}

applyMiddlewares() {
const middlewares = this.middlewares || this.getReduxMiddlewares();

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

composeEnhancers(...enhancers) {
return compose(...enhancers);
}
Expand All @@ -78,7 +56,6 @@ class ReduxMixin extends Mixin {

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

module.exports = ReduxMixin;

0 comments on commit 06c1ec5

Please sign in to comment.