Search code examples
juce

How to trigger a ValueListener with the same value


I listen to a Value that contains the path of a file to load. I would like to load the same file, but the listener system won't react unless the path is different from the last event.

How can one get ValueListener to trigger on the same data?


Solution

  • First, create a class derived from Value::ValueSource that has its own overridden implementation of setValue() that's something like this:

    void setValue (const var& newValue) override
    {
       // base class version checks for equality first. Skip that
       // and always set the value and notify of changes.
       value = newValue;
       sendChangeMessage (false);
    }
    

    Second, when creating the Value object, pass in a pointer to a new instance of this class, like

    Value myValue(new EagerValueSource());
    

    and then use the Value object exactly as you otherwise would have, except listeners will be notified whenever myValue's setValue() method is called, regardless of whether that's actually a new value or not.