Created
April 13, 2017 12:06
-
-
Save anteriovieira/7a0397c25cdd0611466dd3433457c9a0 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/gotele
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var counter = function counter(state, action) { | |
if (state === undefined) state = 0; | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
var createStore = function createStore(reducer) { | |
var state = undefined; | |
var listeners = []; | |
var getState = function getState() { | |
return state; | |
}; | |
var dispatch = function dispatch(action) { | |
state = reducer(state, action); | |
// dispatch all listeners | |
listeners.forEach(function (listener) { | |
return listener(); | |
}); | |
}; | |
var subscribe = function subscribe(listener) { | |
listeners.push(listener); | |
// remove all listeners | |
return function () { | |
listeners = listeners.filter(function (l) { | |
return l !== listener; | |
}); | |
}; | |
}; | |
// init state | |
dispatch({}); | |
return { getState: getState, dispatch: dispatch, subscribe: subscribe }; | |
}; | |
var store = createStore(counter); | |
var render = function render() { | |
document.body.innerText = store.getState(); | |
}; | |
render(); | |
store.subscribe(render); | |
document.addEventListener('click', function () { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default : | |
return state; | |
} | |
} | |
const createStore = (reducer) => { | |
let state | |
let listeners = [] | |
const getState = () => state; | |
const dispatch = function(action){ | |
state = reducer(state, action) | |
// dispatch all listeners | |
listeners.forEach(listener => listener()) | |
} | |
const subscribe = function(listener){ | |
listeners.push(listener) | |
// remove all listeners | |
return () => { | |
listeners = listeners.filter(l => l !== listener) | |
} | |
} | |
// init state | |
dispatch({}) | |
return { getState, dispatch, subscribe } | |
} | |
const store = createStore(counter) | |
const render = () => { | |
document.body.innerText = store.getState() | |
} | |
render() | |
store.subscribe(render) | |
document.addEventListener('click', () => { | |
store.dispatch({ type: 'INCREMENT' }) | |
})</script></body> | |
</html> |
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
'use strict'; | |
var counter = function counter(state, action) { | |
if (state === undefined) state = 0; | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
var createStore = function createStore(reducer) { | |
var state = undefined; | |
var listeners = []; | |
var getState = function getState() { | |
return state; | |
}; | |
var dispatch = function dispatch(action) { | |
state = reducer(state, action); | |
// dispatch all listeners | |
listeners.forEach(function (listener) { | |
return listener(); | |
}); | |
}; | |
var subscribe = function subscribe(listener) { | |
listeners.push(listener); | |
// remove all listeners | |
return function () { | |
listeners = listeners.filter(function (l) { | |
return l !== listener; | |
}); | |
}; | |
}; | |
// init state | |
dispatch({}); | |
return { getState: getState, dispatch: dispatch, subscribe: subscribe }; | |
}; | |
var store = createStore(counter); | |
var render = function render() { | |
document.body.innerText = store.getState(); | |
}; | |
render(); | |
store.subscribe(render); | |
document.addEventListener('click', function () { | |
store.dispatch({ type: 'INCREMENT' }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment