Hooks create and control execution

let execution = null;
let current = null;
let context = null;
let args = null;


const hooked = callback => {

  const details = {i: 0, stack: []};

  return function hook() {
    details.i = 0;
    execution = details;
    current = hook;
    context = this;
    args = arguments;
    return callback.apply(context, args);
  };
};

const useState = value => {
  const hook = current;
  const ctx = context;
  const rest = args;
  const {i, stack} = execution;
  execution.i++;

  if (i === stack.length) {
    stack.push(value);
  }

  return [
    stack[i],
    value => {
      stack[i] = value;
      hook.apply(ctx, rest);
    }
  ];
};

Copy the code
To take a small example 🌰
const toBeWrapped = start => {
  const [num, update] = useState(start);
  console.log(`Counting: ${num}`);
  setTimeout(update, 1000, num + 1);
};

function Counter() {
  return hooked(toBeWrapped).apply(this, arguments);
}

Counter(0);

Copy the code
React