API that allows signal values to reset to initial values
Describe your motivation
An API that allows users to reset Signal would be nice. Otherwise, you need to create a reset method that should update the signal value to initial value.
Describe the solution you'd like
Signal.reset(signalValue) that sets the value to provided initial value or empty if not provided
Describe alternatives you've considered
No alternative.
Additional context
Signal.resetAll() also would be nice when you want to reset everything in the state
How is mySignal.reset("foo") different from mySignal.value("foo")?
Signal.resetAll()also would be nice when you want to reset everything in the state
Everything globally for all instances in the JVM or in some specific well-defined scope?
How is mySignal.reset("foo") different from mySignal.value("foo")?
Imagine you have a reference to signals created in some scope, something like below, and you want to reset all at once.
private final List<ValueSignal<?>> signals = new ArrayList<>();
private final ValueSignal<Cell> selectedCell = new ValueSignal<>(Cell.class);
private final ValueSignal<Difficulty> difficultySignal = new ValueSignal<>(Difficulty.class);
private final ValueSignal<CellMatrix> cellMatrix = new ValueSignal<>(CellMatrix.class);
private final ValueSignal<Boolean> completed = new ValueSignal<>(false);
private final ValueSignal<Boolean> failed = new ValueSignal<>(false);
private final NumberSignal errorCountSignal = new NumberSignal(0);
public State() {
signals.add(selectedCell);
signals.add(difficultySignal);
signals.add(cellMatrix);
signals.add(completed);
signals.add(failed);
signals.add(errorCountSignal);
}
public void resetAll(){
for (ValueSignal<?> signal : signals) {
Signal.reset(signal); // this is not possible
}
}
Everything globally for all instances in the JVM or in some specific well-defined scope?
Everything globally might create unexpected interference between different states, so the scope has to be explicitly set, in my opinion.
So you suggest that every signal instance would explicitly keep a reference to its initial value? That might not work very well in combination with clustering since some instances might just "exist" without having been implicitly created.
so the scope has to be explicitly set
I suspect this will lead to so much bookkeeping for managing scopes that you could just as well do it manually based on e.g. a Map from the signal instance to the default value for that specific instance.