-
-
Save oslego/ff8aa52c3eff8afd3b075e989498defa to your computer and use it in GitHub Desktop.
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
const augment = callback => () => { | |
const useState = { | |
init: true, | |
value: void 0, | |
update(value) { | |
useState.value = value; | |
callback(hooks) | |
} | |
}; | |
const hooks = { | |
useState(value) { | |
if (useState.init) { | |
useState.init = false; | |
useState.value = value; | |
} | |
return [useState.value, useState.update]; | |
} | |
}; | |
callback(hooks); | |
}; | |
const counter = augment(({useState}) => { | |
const [i, update] = useState(0); | |
console.log(i); | |
setTimeout(update, 1000, i + 1); | |
}); | |
// once invoked, it should log the `i` from 0 to N | |
// each second the `i` should be incremented + 1 | |
counter(); | |
// 0 | |
// 1 | |
// 2 | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment