Created
February 26, 2016 02:15
-
-
Save takashi/874610c819568ed136c3 to your computer and use it in GitHub Desktop.
minimum redux implimentation
This file contains hidden or 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 {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