Last active
February 21, 2020 05:34
-
-
Save DQNEO/617b4ef80c4672ce85b144523f2fda34 to your computer and use it in GitHub Desktop.
Reinvention of Redux
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
| <!DOCTYPE html> | |
| <!-- This is a port of "counter-vanilla" example without depending on Redux. --> | |
| <!-- Original is https://github.com/reduxjs/redux/blob/v4.0.5/examples/counter-vanilla/index.html --> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Redux-like Counter</title> | |
| </head> | |
| <body> | |
| <h1>Redux-like Counter</h1> | |
| <p>Counter: <span id="value">0</span></p> | |
| <button id="incr">+</button> | |
| <button id="decr">-</button> | |
| <button id="incrOdd">+ if odd</button> | |
| <button id="incrAsync">+ async</button> | |
| <!-- script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script --> <!-- this is real Redux --> | |
| <script> | |
| // This is a library part. | |
| // Reinvention of Redux. | |
| class Store { | |
| constructor(reducer) { | |
| this.state = 0; | |
| this.reducer = reducer; | |
| this.subscription = []; | |
| } | |
| dispatch(action) { | |
| this.state = this.reducer(this.state, action); | |
| this.subscription.forEach((v) => { | |
| v(); | |
| }) | |
| } | |
| getState() { | |
| return this.state; | |
| } | |
| subscribe(render) { | |
| this.subscription.push(render); | |
| } | |
| } | |
| var Redux = { | |
| createStore: (reducer) => { | |
| return new Store(reducer); | |
| } | |
| } | |
| </script> | |
| <script> | |
| // This is the application part. | |
| const myReducer = (state, action) => { | |
| if (typeof state === "undefined") { | |
| return 0; | |
| } | |
| switch (action.type) { | |
| case "incr": | |
| return state + 1; | |
| case "decr": | |
| return state - 1; | |
| default: | |
| return state; | |
| } | |
| }; | |
| const store = Redux.createStore(myReducer); | |
| const elm = document.getElementById("value"); | |
| const render = () => { | |
| elm.innerText = store.getState().toString(); | |
| }; | |
| store.subscribe(render); | |
| document.getElementById("incr").addEventListener("click", () => { | |
| store.dispatch({type: "incr"}); | |
| }); | |
| document.getElementById("decr").addEventListener("click", () => { | |
| store.dispatch({type: "decr"}); | |
| }); | |
| document.getElementById("incrOdd").addEventListener("click", () => { | |
| if (store.getState() % 2 !== 0) { | |
| store.dispatch({type: "incr"}); | |
| } | |
| }); | |
| document.getElementById("incrAsync").addEventListener("click", () => { | |
| setTimeout(() => { | |
| store.dispatch({type: "incr"}); | |
| }, 500); | |
| }); | |
| render(); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment