From 06c1ec591dc487f4dc1e538bd558c1a02751abe7 Mon Sep 17 00:00:00 2001 From: Jonas Holland Date: Tue, 9 Apr 2019 11:37:11 +0200 Subject: [PATCH] chore(redux): add common class for redux runtime mixin --- packages/redux/mixin.browser.js | 28 +++------------------ packages/redux/mixin.runtime-common.js | 35 ++++++++++++++++++++++++++ packages/redux/mixin.server.js | 29 +++------------------ 3 files changed, 41 insertions(+), 51 deletions(-) create mode 100644 packages/redux/mixin.runtime-common.js diff --git a/packages/redux/mixin.browser.js b/packages/redux/mixin.browser.js index 554b0b655..34de6bd78 100644 --- a/packages/redux/mixin.browser.js +++ b/packages/redux/mixin.browser.js @@ -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 || {}; } @@ -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); } @@ -75,7 +54,6 @@ class ReduxMixin extends Mixin { ReduxMixin.strategies = { getReduxStore: override, - getReduxMiddlewares: override, }; module.exports = ReduxMixin; diff --git a/packages/redux/mixin.runtime-common.js b/packages/redux/mixin.runtime-common.js new file mode 100644 index 000000000..2e59b6d5f --- /dev/null +++ b/packages/redux/mixin.runtime-common.js @@ -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; diff --git a/packages/redux/mixin.server.js b/packages/redux/mixin.server.js index b91edbe8e..78a824d02 100644 --- a/packages/redux/mixin.server.js +++ b/packages/redux/mixin.server.js @@ -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 || {}; } @@ -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); } @@ -78,7 +56,6 @@ class ReduxMixin extends Mixin { ReduxMixin.strategies = { getReduxStore: override, - getReduxMiddlewares: override, }; module.exports = ReduxMixin;