flow icon indicating copy to clipboard operation
flow copied to clipboard

API that allows signal values to reset to initial values

Open abdullahtellioglu opened this issue 7 months ago • 3 comments

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

abdullahtellioglu avatar Jun 03 '25 07:06 abdullahtellioglu

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?

Legioth avatar Jun 05 '25 07:06 Legioth

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.

abdullahtellioglu avatar Jun 05 '25 08:06 abdullahtellioglu

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.

Legioth avatar Jun 05 '25 09:06 Legioth