totaljs / flow

Flow
MIT License
538 stars 121 forks source link

In a code component how do you "send" data from inside a loop #103

Closed developedsoftware closed 1 year ago

developedsoftware commented 1 year ago

For example, I want one code element to send 20 $.send('output', dataInALoop)

// IMPORTANT: methods $.send(), $.destroy() and $.throw() can be executed only once

So I dont see how this is a possible but would be a very real requirement of a code block?

petersirka commented 1 year ago

You need to create 20 messages.

for (var ....)
    instance.newmessage(data).send('output_name');

And I recommend destroying the previous message.

developedsoftware commented 1 year ago

Thanks @petersirka

How would I obtain the "output_name", is it the component id?

Screenshot 2023-05-10 at 13 33 54

How comes they can only be sent once? Just to try and provoke it I am calling $.send('output', data) 20 times and it seems to handle it? Whats the logic behind enforcing methods $.send(), $.destroy() and $.throw() can be executed only once

petersirka commented 1 year ago

Sorry for the delay. I forgot to answer.

How would I obtain the "output_name", is it the component id?

Output name means output id defined in the component declaration: https://github.com/totaljs/flowstreamcomponents/blob/main/components/chatgpt.html#L11

Whats the logic behind enforcing methods $.send(), $.destroy() and $.throw() can be executed only once

Every message has its own life cycle, but there is no complicated logic. It is recommended to send the message only once or to destroy it. If you need to resend the same message several times, create a new message (or clone an existing one). Sending the same message multiple times is not dangerous, but it can cause problems in some cases.