yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.52k stars 1.42k forks source link

Broadcast message from parent to children #712

Closed wokket closed 4 years ago

wokket commented 4 years ago

Description

I'm submitting a Question

G'day guys,

I'm brand new to Yew (and pretty new to rust in general), and am having trouble getting eventing from a parent to it's children working. Hopefully this is a very simple misunderstanding on my part, as this has been very simple to throw together to this point :+1:

I have the code up here, and based on some earlier issue have written an intermediate agent to accept a 'Clear' message from the parent state component and forward a message to all the child Channel components requesting they clear their internal state.

Expected Results

I'd see a message appearing in each child Channel's update method sent from the agent.

Actual Results

The agent wire up appears to be ok,. and the parent is notifying the agent correctly. The agent logs indicate 14 subscribers (12 children + the parent + 1 magical subscriber?? not sure what's going on there) attached, with 12 (the children) being respondable.

Unfortunately the child channel logs are never hit, so the message has gone awry somewhere.

Total log output:

ClearAgent: Created... 
ClearAgent: New Child connected... 13 
Finished loading Rust wasm module 'diffcount' 
Clear Requested 
ClearAgent: New Child connected... 
ClearAgent: Notifying all 14 subscribers of clear request
ClearAgent: Responding to subscriber 0
ClearAgent: Responding to subscriber 1
ClearAgent: Responding to subscriber 2
ClearAgent: Responding to subscriber 3
ClearAgent: Responding to subscriber 4
ClearAgent: Responding to subscriber 5
ClearAgent: Responding to subscriber 6
ClearAgent: Responding to subscriber 7
ClearAgent: Responding to subscriber 8
ClearAgent: Responding to subscriber 9
ClearAgent: Responding to subscriber 10
ClearAgent: Responding to subscriber 11
ClearAgent: Responding to subscriber 12
Child Clear Complete

Context (Environment)

jstarry commented 4 years ago

@wokket in your Channel component, the bridge is getting dropped so won't stay connected (see here: https://github.com/yewstack/yew/blob/master/src/agent.rs#L324)

jstarry commented 4 years ago

Dropped here: https://github.com/wokket/diffcount-yew/blob/master/src/channel.rs#L54 Add it to your Channel struct and you should be good to go 👍

wokket commented 4 years ago

Ahh, That makes sense, and is working perfectly now. Thanks for the quick help :)