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

Can't send requests to a public agent just after creation #637

Closed daniel-shimon closed 4 years ago

daniel-shimon commented 5 years ago

Description

I'm submitting a bug report

In the main component:

impl Component for App {
    ....
    fn create(props: Self::Properties, mut link: ComponentLink<Self>) -> Self {
            let callback = link.send_back(|response| Messages::EngineMessage(response));
            let mut engine = engine::Engine::bridge(callback);
            engine.send(engine::Request::InitApp);
            App { engine }
        }
    ...
}

And in the public agent:

impl Agent for Engine {
    type Reach = Public; // <--
    ...
    fn handle(&mut self, msg: Self::Input, id: HandlerId) {
        stdweb::console!(log, format!("engine received: {:?}", msg));
    }
    ...
}

Expected Results

engine received: InitApp

Actual Results

No message is printed.

Just to be clear, messages sent to the public agent after the create function (in App's update function) work fine and are printed!

If I change engine's reach to Context or Job, the whole thing works correctly. Does anyone have any idea what's happening here?

Context (Environment)

kellytk commented 5 years ago

Related issue https://github.com/yewstack/yew/issues/402.

hgzimmerman commented 5 years ago

This is likely related to either #402 or you might be able to solve this by sending the request to the agent in mounted instead of create.

mounted is only available on the master branch right now, but it does allow for you to perform logic after the component has been mounted to the vdom. This has the effect of giving more time for the agent to initialize in its web-worker before the message is sent. That still seems race-condition prone, but its probably better then sending the message in create.

kellytk commented 5 years ago

@hgzimmerman, @daniel-shimon trying mounted as you suggest would help us begin isolating the cause and is a good idea.

serzhiio commented 5 years ago

596?

hgzimmerman commented 5 years ago

Going just by the name, that is also a very likely culprit. I hesitated to mention it because I'm unfamiliar with that PR and linking its content doesn't immediately answer questions.

As far as I can tell, that PR queues messages sent to public/private agents until the webworker/agent is initialized. This would be the ideal solution to this issue, as it doesn't rely on the race conditions still present even if mounted worked.

Assuming that synopsis is correct, when that gets merged, I would expect this bug to no longer present itself.

daniel-shimon commented 5 years ago

Ok so #596 seems to be the exact solution I was looking for, as stated in the TODO comment which is fulfilled in this PR:

// TODO Important! Implement.   
// Use a queue to collect a messages if an instance is not ready    
// and send them to an agent when it will reported readiness.

Thanks @serzhiio for this awesome PR, can't wait for it to be merged 😄