Is it possible to have an eventListener inside a custom action listen for component events?

  • Ben H

    24 days ago

    I have a data-provider component that houses common calculations I need to perform in my workflows. When the calculations are complete, the data-provider component fires an event "calculated_results" with the results. The data-provider component shares its context so that its calculation workflow can be triggered outside the component. Now, here is my problem. I need a way to halt my workflows until I get the data from the data-provider needed to continue the workflow. That is, I need to halt the workflow until the calculated_results event fires. I was hoping I could do this with a custom action using an eventListener (code below), but it doesn't seem to work. It doesn't seem to recognize any event called "calculated_results". Does toddle have an internal representation of this component event that I need to be using?


    function waitForEvent(args, ctx) {

    // Get parameters
    const eventToWaitFor = args.eventName;
    const timeoutMs = isNumber(args.timeoutMs) ? args.timeoutMs : 30000;

    // Handler for the event
    const handleEvent = (event) => {
    clearTimeout(timeout);
    document.removeEventListener(eventToWaitFor, handleEvent);
    ctx.triggerActionEvent('success', {
    event: eventToWaitFor,
    data: event.detail
    });
    };

    // Set up timeout
    const timeout = setTimeout(() => {
    document.removeEventListener(eventToWaitFor, handleEvent);
    ctx.triggerActionEvent('timeout', {
    error: 'Waiting for ${eventToWaitFor} timed out after ${timeoutMs}ms',
    event: eventToWaitFor
    });
    }, timeoutMs);

    // Listen for the specified event
    document.addEventListener(eventToWaitFor, handleEvent);
    }

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code — but accessible to your entire team!

Try toddle — it's free!

© Copyright 2024 toddle. All rights reserved.