TarVK / model-react

A data model system to use with react hooks
https://tarvk.github.io/model-react/examples/build/
MIT License
13 stars 3 forks source link

Add debounce option to field #19

Closed TarVK closed 3 years ago

TarVK commented 4 years ago

Add a constructor option to the field that specifies a debounce delay, allowing for listeners to be called after a delay. This is mostly useful in order to not have any invariants broken when listeners are executing;

class A {
    // Invariant: b = a + 5;
    protected a = new Field(0);
    protected b = new Field(5);
    setA(value: number){
        this.a.set(value);
        // Now field a is invoking all its listeners, which may want to use this instance. 
        // But currently the invariant on which correctness of this class may rely is not met.
        this.b.set(value+5);
    }
}

By debouncing the listener invocation for 0ms by default, It would only call listeners after the synchronous changes finished. This behavior seems more intuitive to me. If your function is asynchronous, you will still need to consider that other code can execute inbetween parts, but that's always the case.

TarVK commented 3 years ago

The debouncer has been added to the standard useDataHook instead.

commit