Open michaelboyles opened 2 years ago
For-loops are now supported. While-loops will require more thought. This code
interface StringState {
str: string
}
const reducer = redcr((state: StringState) => {
let i = 1;
while (i <= 3) {
state.str += 'A';
i++;
}
});
Produces this:
const reducer = (state) => {
while (1 <= 3) {
state.str += 'A';
1++;
}
return state;
};
It's caused by unsophisticated local variable removal which incorrectly inlines the counter. Local variables are inlined to support destructuring the state.
Currently, using a loop in a Redcr reducer can end up mutably editing the state. For example
Will produce
It should probably work similar to how if-statements are handled, and continuously reassign the state
Could explore loop unrolling too if the number of iterations is a compile-time constant.
How often is a loop actually useful in a reducer though?