-
-
Save mendes5/6b0d9fdc2c0788da9ea246b73c152235 to your computer and use it in GitHub Desktop.
Poor mans version of S.js
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 effects = []; | |
| let isCollectingEffects = false; | |
| const data = (init) => { | |
| const deps = []; | |
| let oldValue = init; | |
| return (value) => { | |
| if (value === undefined) { | |
| if (isCollectingEffects) { | |
| effects.push(deps); | |
| } | |
| return oldValue; | |
| } else { | |
| oldValue = value; | |
| deps.forEach((cb) => cb()); | |
| } | |
| }; | |
| }; | |
| const S = (callback) => { | |
| isCollectingEffects = true; | |
| effects.splice(0); | |
| callback(); | |
| isCollectingEffects = false; | |
| effects.forEach((effect) => { | |
| effect.push(callback); | |
| }); | |
| }; | |
| const name = data("mendes"); | |
| S(() => { | |
| document.body.innerText = name(); | |
| }); | |
| S(() => { | |
| console.log(name()); | |
| }); | |
| setTimeout(() => { | |
| name("Phellipe"); | |
| }, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment