Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created December 15, 2020 08:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WebReflection/9c2e09ef712807165ae98158236abb0c to your computer and use it in GitHub Desktop.
Save WebReflection/9c2e09ef712807165ae98158236abb0c to your computer and use it in GitHub Desktop.
function useState(value) {
const {caller} = arguments.callee;
const {arguments: a} = caller;
const {stack} = useState;
if (!stack.has(caller))
stack.set(caller, {a, i: 0, s: []});
const state = stack.get(caller);
state.a = a;
const {i, s} = state;
if (i === s.length) {
s.push([value, value => {
s[i][0] = value;
state.i = 0;
caller.apply(null, a);
}]);
}
return s[state.i++];
}
useState.stack = new WeakMap;
function any() {
const [a, updateA] = useState(0);
const [b, updateB] = useState(0);
console.log(a, b);
if (!b)
updateB(1);
}
any();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment