cables-gl / cables_docs

cables documentation docs.cables.gl
https://cables.gl/docs/docs
45 stars 16 forks source link

TriggerDelay is missing an accumulation option #848

Open 60-hz opened 2 months ago

60-hz commented 2 months ago

TriggerDelay is missing a simple accumulation option for the delay.

Here is an example, adding the accumulate option as boolean, which adds delay in a queue, and outDelaying will turn FALSE after all triggered delay has been send:

const exe = op.inTrigger("exe"),
      delay = op.inValueFloat("delay", 1),
      accumulate = op.inValueBool("Accumulate Delays", false),
      cancel = op.inTriggerButton("Cancel"),
      next = op.outTrigger("next"),
      outDelaying = op.outBool("Delaying");

let lastTimeout = null;
let delayQueue = [];

cancel.onTriggered = function () {
    if (lastTimeout) clearTimeout(lastTimeout);

    // Clear all timeouts in the queue if accumulate is true
    delayQueue.forEach(timeout => clearTimeout(timeout));
    delayQueue = [];
    lastTimeout = null;

    outDelaying.set(false);
};

exe.onTriggered = function () {
    if (!accumulate.get() && lastTimeout) {
        clearTimeout(lastTimeout);
        delayQueue = [];
    }

    outDelaying.set(true);

    const timeout = setTimeout(triggerNext, delay.get() * 1000);

    if (accumulate.get()) {
        delayQueue.push(timeout);
    } else {
        lastTimeout = timeout;
    }
};

function triggerNext() {
    next.trigger();

    if (accumulate.get() && delayQueue.length > 0) {
        delayQueue.shift();  // Remove the current timeout
        if (delayQueue.length === 0) {
            outDelaying.set(false);
        }
    } else {
        lastTimeout = null;
        outDelaying.set(false);
    }
}
steam0r commented 2 months ago

hey, thanks! could you create a new op with a patched example from this?

60-hz commented 2 months ago

Yes sure, here is a minimal one: https://cables.gl/edit/EUQ5Sn

60-hz commented 2 weeks ago

@steam0r , what do you think, is it possible to add the delay accumulation option in the original TriggerDelay ?

steam0r commented 1 week ago

hey, if you want to share this op you could create a team for the changes you made to the trigger ops and make that public.

we try to keep the core ops as "minimal" as possible and have them work with other ops so things can be solved in patches or patchops instead of code, if possible.

60-hz commented 1 week ago

Thanks. That's what I usually do yes. For this example, you can't really put delays in a queue using only patch logic... is it?

steam0r commented 1 week ago

havent looked into the logic too deep but this might be possible with arraybuffers and delayedtrigger or interval