Created
May 24, 2019 03:11
-
-
Save michaelmr/76505876df9567b7c70da76506f99a31 to your computer and use it in GitHub Desktop.
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
| const tinyelm = (init, update, output, debug = console.log) => { | |
| let state = init; | |
| output(state); | |
| return (event) => { | |
| debug("dispatching event " + JSON.stringify(event)); | |
| debug("cur state " + JSON.stringify(state)); | |
| state = update(event, state); | |
| debug("output state " + JSON.stringify(state)); | |
| output(state); | |
| } | |
| }; | |
| const output = (state) => { | |
| document.getElementById("count").innerHTML = state.state; | |
| }; | |
| const update = (event, {state= 0}) => { | |
| switch (event.type) { | |
| case "INCREMENT": | |
| return {state: state + 1}; | |
| case "DECREMENT": | |
| return {state: state - 1}; | |
| default: | |
| return {state:state}; | |
| } | |
| } | |
| let dispatch= tinyelm({"state":0}, update, output); | |
| let btnPlus = document.getElementById("inc"); | |
| let btnMinus = document.getElementById("dec"); | |
| btnPlus.addEventListener("click", () => { | |
| dispatch({ type: "INCREMENT" }); | |
| }); | |
| btnMinus.addEventListener("click", () => { | |
| dispatch({ type: "DECREMENT" }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running example on JSBin https://jsbin.com/tacuvubuwe/edit?html,js,console,output
and CodePen https://codepen.io/anon/pen/GaxgXM