How to communicate with OneShotRunner

34 views Asked by At

I try to pass a calculation from Yew App to a Worker via OneShotRunner. I cannot however seem to figure out how to communicate between App and Worker. Naively I have this code:

pub enum Msg {
    SetResult(String)
}

pub struct Foo {
    input: String,
    result: String,
}

impl Component for Foo {
    type Message = Msg;
    type Properties = ();

    fn create(_ctx: &Context<Self>) -> Self {
        Self {
            input: "[]".into(),
            result: "[]".into(),
        }
    }


    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::SetResult(result) => {
                self.result = result;
                true
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let bar_task = use_oneshot_runner::<BarTask>();

        let on_bar = ctx.link().callback(|_e: MouseEvent| {
            spawn_local(async move {
                // This does not work
                let o = bar_task.run(input).await;
            });
            Msg::SetResult(o)
        });

        html! {
            <main>
                <OneshotProvider<BarTask> path="/worker.js">
                    <button onclick={on_bar}>{ "Bar" }</button>
                </OneshotProvider<BarTask>>
            </main>
        }
    }

Would be great if someone could point out how normally this needs to be connected.

0

There are 0 answers