raj-by-example icon indicating copy to clipboard operation
raj-by-example copied to clipboard

A better (?) function for debugging

Open mindplay-dk opened this issue 4 years ago • 1 comments

Section 5.1 shows a tapProgram HOP that lets you add an onChange event-listener to a program.

I was pondering intergration with Redux DevTools, and found this a bit limited, since it doesn't let you observe the actual messages - only the resulting state.

I ended up with this instead:

function observe(program, onUpdate) {
  return {
    ...program,
    update: (message, state) => {
      var newState = program.update(message, state);
      onUpdate(message, newState);
      return newState;
    }
  }
}

For example, this lets you see every message and resulting state in the console:

var program = { /* ... */ };

program = observe(program, (message, state) => console.log("☑", message, state));

This is already quite useful for debugging - I feel like the reason for a change is likely going to be interesting if you're monitoring an app for changes, so maybe consider changing this is section 5.1?

mindplay-dk avatar Sep 02 '19 09:09 mindplay-dk