throttle

xod/core/throttle

Throttles input changes to happen no more often than once per interval specified. Useful to limit the rate of updates.
throttle
@/throttle
Throttles input changes to happen no more often than once per interval specified. Useful to limit the rate of updates.
INgeneric t1
Value or pulse to throttle
Tnumber
IN will propagate to OUT at most once per T seconds
throttle
IN
OUT
T
OUTgeneric t1
The throttled output. Follows IN immediately if T time window has passed since the last update, otherwise conducts IN with a lag so that the time span between updates is no less than T

C++ implementation

node {
    bool hasStarted : 1;
    bool shouldEmitOnEnd : 1;

    void evaluate(Context ctx) {
        if (isSettingUp()) {
            hasStarted = false;
            shouldEmitOnEnd = false;
        }
        auto inValue = getValue<input_IN>(ctx);

        if (isInputDirty<input_IN>(ctx)) {
            if (!hasStarted) {
                hasStarted = true;
                emitValue<output_OUT>(ctx, inValue);
                TimeMs dt = getValue<input_T>(ctx) * 1000;
                setTimeout(ctx, dt);
            } else {
                shouldEmitOnEnd = true;
            }
        }

        if (isTimedOut(ctx) && shouldEmitOnEnd) {
            shouldEmitOnEnd = false;
            TimeMs dt = getValue<input_T>(ctx) * 1000;
            setTimeout(ctx, dt);
            emitValue<output_OUT>(ctx, inValue);
        } else if (isTimedOut(ctx)) {
            hasStarted = false;
        }
    }
}