Skip to content

Instantly share code, notes, and snippets.

@takashi
Created February 26, 2016 02:15
Show Gist options
  • Select an option

  • Save takashi/874610c819568ed136c3 to your computer and use it in GitHub Desktop.

Select an option

Save takashi/874610c819568ed136c3 to your computer and use it in GitHub Desktop.
minimum redux implimentation
import {element, createApp} from 'deku'
function action() {
return {
type: 'CLICKED'
}
}
function reducer(state, action) {
switch(action.type) {
case 'CLICKED':
return state
}
}
function createStore(reducer, initialState = {}) {
let currentState = initialState
let listeners = []
let dispatch = (action) => {
currentState = reducer(currentState, action)
for(var i = 0, listener; listener = listeners[i]; i++) {
listener()
}
}
let subscribe = (func) => {
listeners.push(func)
return function unsubscribe() {
const i = listeners.indexOf(func)
listeners.splice(index, 1)
}
}
let getState = () => {
return currentState
}
return { dispatch, subscribe, getState }
}
let store = createStore(reducer)
let render = createApp(document.body, store.dispatch)
let App = {
render({ dispatch }) {
return (
<div onClick={dispatch(action())}>hello</div>
)
}
}
function main() {
render(
<App />,
store.getState()
)
}
main()
store.subscribe(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment