Last active
September 30, 2016 18:21
-
-
Save buzinas/770ae581d7f8aa5c5d8f2d02b01cbe66 to your computer and use it in GitHub Desktop.
Redux without the switch clauses (obviously this is only a draft)
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
import Inferno from 'inferno'; | |
import ButtonMenu from './button-menu'; | |
import Menu from './menu'; | |
export default function App() { | |
return ( | |
<div> | |
<ButtonMenu /> | |
<Menu /> | |
</div> | |
); | |
} |
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
import Inferno from 'inferno'; | |
import { Provider } from 'inferno-redux'; | |
import App from './app'; | |
export function Root({ store }) { | |
return ( | |
<Provider store={store}> | |
<App /> | |
</Provider> | |
); | |
} |
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
import { createStore } from 'redux'; | |
export function configureStore(initialState, ...reducers) { | |
const store = createStore(initialState, ...reducers); | |
return store; | |
} |
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
import Inferno from 'inferno'; | |
import { render } from 'inferno-dom'; | |
import { configureStore } from './configure-store'; | |
import { initialState, reducers } from './reducers'; | |
import { Root } from './components/root'; | |
const store = configureStore(initialState, ...reducers); | |
render(<Root store={store} />, document.getElementById('main')); |
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
import { menuInitialState, menuReducers } from './menu'; | |
export const initialState = { | |
menu: menuInitialState | |
}; | |
export reducers = [ | |
...menuReducers | |
]; |
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
export function createStore(initialState, ...reducers) { | |
let state = initialState; | |
const reducers = createReducers(); | |
const store = { | |
getState() { | |
return state; | |
}, | |
dispatch(action) { | |
reducers.execute(action); | |
return this; | |
}, | |
addReducer(reducer, cb) { | |
reducers.add(reducer); | |
return this; | |
}, | |
addReducers(...reducers) { | |
reducers.forEach(this.addReducer); | |
return this; | |
} | |
}; | |
store.addReducers(...reducers); | |
return store; | |
// --- | |
function createReducers() { | |
const reducers = []; | |
return { | |
add(reducer) { | |
reducers.push(reducer); | |
return this; | |
}, | |
execute(action) { | |
reducers | |
.filter(r => r.role === action.role && r.cmd === action.cmd) | |
.forEach(r => state = ({ ...state, [action.role]: r.reducer.call(null, state[action.role], action) })); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Heavily inspired by Redux and Seneca, the idea is to use reducers but without the
switch
boilerplate.