-
I went through the tutorials, bubbles, and a few examples but couldn't figure this out. Is this possible to listen to a channel and update the model? |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 4 replies
-
Sure. You might want to look at how glow uses a channel to load files. https://github.com/charmbracelet/glow/blob/4dd3ba1d3c2b47a68b3ee4a7f30f97b0e5b18c78/ui/ui.go
|
Beta Was this translation helpful? Give feedback.
-
Can't wrap my head around it, unfortunately. Could you please show how this piece of code would work in the realtime example to transfer strings over channel? |
Beta Was this translation helpful? Give feedback.
-
Not sure what the exact problem is here. The sample does exactly what you where asking for. I didn't know this one but the more complex one in glow. But still they both do the same thing. In Init(), we send out two commands. One for each side of the channel.
Those two methods return a cmd each. Each command basically is an action, which once it finishes delivers a message. Message can transport data. So in this example, the listenForActivity(...) function is actually simulating any asynch call by simply waiting before sending a message onto the channel. It does this in an endless loop. So once the message is pushed into the channel its going to sleep again before another message is sent. This command is kind of misusing the ELM Architecture, as the command never actually returns a message. All it does is, that it constantly pushes out messages onto a channel.
That covers the sending part of the channel. The other side of the channel simply receives data from the channel and returns it as a message which will end up in one of the next calls to the models Update method.
All thats missing is the action on the message within the Update method. Whenever a message of the responseMsg type is received, all it does is to count the message and return the same command again by call waitForActivity again
I am not sure if this is of any help. |
Beta Was this translation helpful? Give feedback.
-
Great explanation! That definitely helped me understand it better. Heres what I'm trying to do. Lets say the channel is of type func listenForActivity(sub chan string) tea.Cmd {
return func() tea.Msg {
for {
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)+100))
sub <- "this is str"
}
}
} where the |
Beta Was this translation helpful? Give feedback.
-
case responseMsg:
m.responses++ // record external activity
return m, waitForActivity(m.sub) // wait for next event Here its just incrementing an int in the model upon a message. Whereas Im trying to read what actually came out of |
Beta Was this translation helpful? Give feedback.
-
Almost there.
So the Cmd is returning a message using a typed struct containing your data.
With the Update Message the message is received and we can easily get to the data
Sos thats all there is to it. If this all does not make sense, please consider taking a step back and start with simple examples, not using channels. Just to get the basic concepts of Command, Messages and how the Update View Cycle actually works. |
Beta Was this translation helpful? Give feedback.
-
Perfect! This is exactly what I was trying to do. Thanks for the help. |
Beta Was this translation helpful? Give feedback.
-
Welp I ran into this situation. I want to execute one pair of I can store the result data from the first pair of execution into a struct and use it for the next. But how do I do it at first? I'm not supposed to somehow stop the entire execution and restart it with different functions right? can I somehow re |
Beta Was this translation helpful? Give feedback.
-
Update on this one: we did a blog post on "Channels in Bubble Tea" specifically because it has been asked about quite a bit. |
Beta Was this translation helpful? Give feedback.
Not sure what the exact problem is here. The sample does exactly what you where asking for. I didn't know this one but the more complex one in glow. But still they both do the same thing.
In Init(), we send out two commands. One for each side of the channel.
Those two methods return a cmd each. Each command basically is an action, which once it finishes delivers a message. Message can transport data.
Once a Message is received, it is passed into the Update Method of the model, where the Model can react on the message and literally update its state aka model.
So in this example, the listenForActi…