chrisjpatty / flume

Extract logic from your apps with a user-friendly node editor powered by React.
https://flume.dev
MIT License
1.45k stars 148 forks source link

Access nodes/ports/controls by ID #127

Open idling-mind opened 2 years ago

idling-mind commented 2 years ago

Hi, Thanks for this awesome library!

Had a question about the API. Is there any way that I can address a unique node/port/control in the editor live? I read about the context object, but I believe it's the same across all the controls and cannot be addressed to a particular node/port or control.

I wanted to mark a node if it's busy or not while executing the logic.

ps: I am not using a rootnode or rootengine

hydrusbeta commented 10 months ago

I think I have the same question. I'll explain my concrete use-case below in case that helps with deciding on the relative priority of this issue.

I am attempting to build "probe" nodes whose sole purpose is to observe data at various stages of the logic graph. For example, I want to create a "Probe Audio" node for playing back an audio signal at a specific point: probe audio node

To do this, I have created a custom Control:

Controls.custom({
    name: input.Name,
    label: input.Name,
    render: (data, onChange, _context, _redraw, _portProps, _controlData) => (
        <ProbeAudio src={data} setSrc={onChange}/>
    ),
})

Where ProbeAudio is defined like this:

import {get_base64_data_from_server} from './server_api';

export default function ProbeAudio({src, setSrc} : {src: string, setSrc: (a: string) => void}){
    return (
        <div>
            <button onClick={()=>{
                const my_id = "???" // How do I get the ID of the node that this custom Control belongs to?
                const data : string = get_base64_data_from_server(my_id)
                setSrc(data);
            }}>
                Refresh
            </button>
            <audio src={src} controls={true}/>
        </div>
    );
}

However, I don't know how to get the ID (or more precisely, the data-node-id attribute) of the Node that the custom Control belongs to. I need it in order to identify the node I want to retrieve the data for (there may be many "Audio Probe" nodes throughout the graph). I can use the nodeEditor ref to get data about all nodes (via nodeEditor.current?.getNodes()), but that doesn't help me identify which node this one is.

I am also not using the Root Engine that comes with Flume; my intention is to execute the logic graph in my own engine on a separate server and then use an API to retrieve the results.