raj-by-example
raj-by-example copied to clipboard
A better (?) function for debugging
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?