const create_counter = (max) => {
let count = 0;
const increment = () => {
if (count >= max) {
throw state_error("current count must be less than max");
}
};
return increment;
};
const increment = create_counter(1);
// simulating two users having shared simultaneous access to the counter
const a = { increment };
const b = { increment };
a.increment(); // ok
b.increment(); // throws state error
Generated using TypeDoc
In the case of a stateful interface where the interface user does not have sole ownership of the state (i.e. the state is affected by external factors), a state error is an error caused by the current state of the system at the precise instant when the request was processed.
If, and only if, the interface user does in fact have sole ownership of the state, a user error should be used instead.