Created
May 10, 2016 16:08
-
-
Save kamilio/54634403c136f2b31ed53af924fef078 to your computer and use it in GitHub Desktop.
Redux, eventBus solved with middlewares or without
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *** Middleware - custom thunk implementation (slightly diffferent though) - maybe more scalable appraoch | |
export default function createEventBusMiddleware(eventBus) { | |
return ({ dispatch, getState }) => (next) => (action) => { | |
// we could optimize further here and use rest operator to get all arguments and compose the middlewares | |
if (typeof action === 'function') { | |
return action({ dispatch, eventBus, getState }); | |
} | |
return next(action); | |
}; | |
} | |
// *** Actions | |
function duStuff(id) { | |
return function({dispatch, getState, eventBus}) { // WARNING: ES6 Destructuring | |
eventBuss.trigger('something', id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *** Redux init | |
import {createStore, applyMiddleware} from 'redux'; | |
// see here for more info https://github.com/gaearon/redux-thunk/blob/master/src/index.js#L11 | |
import { withExtraArgument as thunk} from 'redux-thunk'; | |
const eventBus = {}; | |
createStore(combinedReducers, applyMiddleware(thunk(eventBus))); | |
// *** Actions - how to use it | |
function duStuff(id) { | |
return function(dispatch, getState, eventBus) { | |
eventBuss.trigger('something', id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment