Skip to content

Instantly share code, notes, and snippets.

@kamilio
Created May 10, 2016 16:08
Show Gist options
  • Save kamilio/54634403c136f2b31ed53af924fef078 to your computer and use it in GitHub Desktop.
Save kamilio/54634403c136f2b31ed53af924fef078 to your computer and use it in GitHub Desktop.
Redux, eventBus solved with middlewares or without
// *** 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);
}
}
// *** 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